Understanding Subqueries

The first thing that we want to do is explain what a subquery really is. A subquery is a SELECT query that returns a single value and is nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside another subquery. A subquery can be used anywhere an expression is allowed.

Note

The INSERT, UPDATE, and DELETE statements will be covered in tomorrow's lesson.

A subquery is a normal T-SQL query that is nested inside another query, using parentheses.

You may have a subquery nesting up to 32 levels deep. Thirty-two levels are more than enough for most queries that you will ever work with.

In prior versions of Microsoft SQL Server, you could have a maximum of 16 tables in a SELECT statement. This limit included the ...

Get Sams Teach Yourself Transact-SQL in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.