Using subqueries

With a relational database, you can store related data in more than one table. In addition to being able to extract data from related tables using a join, you can also extract it using a subquery. A subquery is a SELECT statement nested within the SELECT, WHERE, or HAVING clause of a parent SQL statement.

Subqueries make some queries easier to write than joins, and there are queries that cannot be written without using subqueries.

Subqueries can be categorized in different ways:

  • whether they can return one or more rows (single-row vs. multiple-row subqueries)

  • whether they are correlated or uncorrelated

  • whether they are nested within another subquery


Single-row and multiple-row subqueries
Correlated and uncorrelated subqueries
Nested subqueries
Using subqueries instead of joins
Subqueries in the WHERE clause
Subqueries in the HAVING clause
Testing subqueries
Optimizer automatic conversion of subqueries to joins