What Is a Subquery?

Simply put, a subquery is a SELECT expression that you embed inside one of the clauses of a SELECT statement to form your final query statement. In this chapter, we’ll define more formally a subquery and show how to use it other than in the FROM clause.

The SQL Standard defines three types of subqueries:

  1. Row subquery—an embedded SELECT expression that returns more than one column and no more than one row

  2. Table subquery—an embedded SELECT expression that returns one or more columns and zero to many rows

  3. Scalar subquery—an embedded SELECT expression that returns only one column and no more than one row

Row Subqueries

You’ve already created queries that embed a SELECT statement in a FROM clause to let you filter rows before joining ...

Get SQL Queries for Mere Mortals: A Hands-On Guide to Data Manipulation in SQL, 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.