Tuesday, November 13, 2007

Sub queries

A subquery is a SELECT statement that is nested within another T-SQL statement.

A sub query may be in a column list, In 'HAVING' clause, 'WHERE' clause, FROM clause, FROM clause of UPDATE statement. A sub query may also be used to create Virtual tables that can further used in a join statement.

A correlated subquery is a SELECT statement nested inside another T-SQL statement, which contains a reference to one or more columns in the outer query.


A subquery, when executed independent of the outer query, will return a result set, and is therefore not dependent on the outer query. Where as, a correlated subquery cannot be executed independently of the outer query because it uses one or more references to columns in the outer query to determine the result set returned from the correlated subquery.

This link is really good reference and has good examples on subqueries. Dont miss the conclusion at the bottom of the page. The lines above are a copy and paste from that.




No comments: