Building a Nested Subquery : Subquery « Subquery « SQL Server / T-SQL Tutorial






A subquery is a SELECT query that is nested within another SELECT, INSERT, UPDATE, or DELETE statement.
A subquery can also be nested inside another subquery.
Subqueries can often be re-written into regular JOINs.
Sometimes an existence subquery can perform better than equivalent non-subquery methods.
A correlated subquery is a subquery whose results depend on the values of the outer query.


SELECT <SELECT list>
FROM   <SomeTable>
WHERE  <SomeColumn> = (
       SELECT <single column>
       FROM <SomeTable>
       WHERE <condition that results in only one row returned>)
Or:
SELECT <SELECT list>
FROM   <SomeTable>
WHERE  <SomeColumn> IN   (
           SELECT <single column>
           FROM <SomeTable>
           [WHERE <condition>])








8.1.Subquery
8.1.1.Building a Nested Subquery
8.1.2.Using Subqueries to Check for the Existence of Matches
8.1.3.Using Subqueries to Check for the Existence of Matches between two tables
8.1.4.Subqueries are SELECT statements that are nested within another T-SQL statement.
8.1.5.Subqueries and Comparison Operators: =
8.1.6.A subquery can be used with other comparison operators: <
8.1.7.Subqueries and IN Operator
8.1.8.A query that uses three subqueries
8.1.9.Doing calculation with subquery
8.1.10.Subqueries can be nested.
8.1.11.ID NOT IN (SELECT ID FROM Title)
8.1.12.Delete with subquery
8.1.13.Join and sub query
8.1.14.Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
8.1.15.subquery with >=
8.1.16.<> ALL with subquery