SQLTutorials  
Powered By PhICK1.com 

SQL EXISTS

Using a subquery to test for the existence of rows. If it Exists Operators return a row, then the outer query proceeds. If not, the outer query does not execute or return any result or row.

Below is Example for SQL EXISTS
 

Company

Sales($)

Website

Sql Traniner 2500 http://www.sqltutorials.blogspot.com
BeautyCentury 3000 http://beautycentury.blogspot.com
TravelYourself 2800 http://travelyourself.blogspot.com
Table1

 
Sales

Website

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

SQL EXISTS Statement

SELECT * FROM TABLE1 WHERE EXISTS(SELECT * FROM TABLE2)
Result

Company

Sales($)

Website

Sql Traniner 2500 http://www.sqltutorials.blogspot.com
BeautyCentury 3000 http://beautycentury.blogspot.com
TravelYourself 2800 http://travelyourself.blogspot.com



- We also can using the EXISTS in IF...ELSE Statement to checking the existence of a row or value.
- If IF...ELSE return true, it will return the result of Sql statement inside the BEGIN and END.

IF EXISTS(SELECT * FROM TABLE1 WHERE Company='TravelYourself')
BEGIN
SELECT * FROM TABLE1 WHERE Company='TravelYourself'
END

Result  
TravelYourself 2800 http://travelyourself.blogspot.com


powered by www.pHick1.com

SQLTutorials
Sql Server Related Links