Saturday, June 9, 2007

SQL Subquery

Syntax - Subquery
SELECT Statement WHERE ColumnName [Comparison Operator] (SELECT Statement / Values)
Results of one Sql query or Sql statement as the input for another Select query, Insert Into query, Insert From query, Update query and Delete query.
You also can use the result of Subquery as a search condition for using in the IN( ) function or EXISTS operator.

Comparison Operator such as =, >, <, >=, <=, LIKE, IN


Below is example for SQL UNION


Company

Sales($)

Website

Sql Traniner2500http://www.sqltutorials.blogspot.com
BeautyCentury3000http://beautycentury.blogspot.com
TravelYourself2800http://travelyourself.blogspot.com
Table1

Sales

Website

2900http://www.sqltutorials.blogspot.com
3000http://beautycentury.blogspot.com
2800http://travelyourself.blogspot.com
Table2


SQL Subquery Statement

SELECT * FROM TABLE1 WHERE sales in (SELECT Sales FROM TABLE2)
Result
BeautyCentury3000

http://beautycentury.blogspot.com

TravelYourself2800http://travelyourself.blogspot.com



SELECT * FROM TABLE1 WHERE sales LIKE (SELECT Sales FROM TABLE2 WHERE Sales <2900)>
Result

TravelYourself2800http://travelyourself.blogspot.com


SELECT * FROM TABLE1where sales > (SELECT Sales FROM TABLE2 WHERE Sales <2900)
Result
BeautyCentury 3000http://beautycentury.blogspot.com


6 comments:

Ashley said...

WoW nice blog buddy!Actually WoW is my favorite appreciation keyword. I also like to play. Wow Gold. I was looking for info regarding that and saw your website. Its nice and very interesting.

Mind said...

Fine post

shan14 said...

Excellent definition with examples

Mind said...

wow gold kaufen

Interesting post

GuildWars2Items said...

c9 moneyexperience is a hard teacher because she gives the test first, the lesson afterwards, c9 gold the consequences of today are determined by the actions of the past. To change your future, alter your decisions today c9 online gold.

The past is gone and static. Nothing we can do will change it.scarlet blade gold, the future is before us and dynamic. Everything we do will affect it rs gold, You laugh at mescarlet blade gold for being different , but I laugh at you for being the same.

Sudhir DBAKings said...

Nice post very helpful

dbakings