SQL > Advanced SQL > Subquery

A subquery is a SQL statement that has another SQL query embedded in the WHERE or the HAVING clause.

Key Takeaway: Subqueries let you use the result of one query as input to another. The inner query runs first; its result is passed to the outer query. This is a powerful pattern for filtering based on aggregated or derived data.

Syntax

The syntax for a subquery when the embedded SQL statement is part of the WHERE condition is as follows:

SELECT "column_name1"
FROM "table_name1"
WHERE "column_name2" [Comparison Operator]
(SELECT "column_name3"
FROM "table_name2"
WHERE "condition");

[Comparison Operator] could be equality operators such as =, >, <, >=, <=. It can also be a text operator such as "LIKE". The portion in the inner parentheses is considered as the "inner query," while the outer portion is considered as the "outer query."

Examples

We use the following tables for our examples.

Table Store_Information

 Store_Name  Sales  Txn_Date 
 Los Angeles  1500  Jan-05-1999 
 San Diego  250  Jan-07-1999 
 Los Angeles  300  Jan-08-1999 
 Boston  700  Jan-08-1999 

Table Geography

 Region_Name  Store_Name 
 East  Boston 
 East  New York 
 West  Los Angeles 
 West  San Diego 

Example 1: Simple subquery

To use a subquery to find the sales of all stores in the West region, we use the following SQL statement:

SELECT SUM (Sales) FROM Store_Information
WHERE Store_Name IN
(SELECT Store_Name FROM Geography
WHERE Region_Name = 'West');

Result:

SUM (Sales)
2050

In this example, instead of joining the two tables directly and then adding up only the sales amount for stores in the West region, we first use the subquery to find out which stores are in the West region, and then we sum up the sales amount for these stores.

Notice that in this example, the inner query and the outer query are independent of each other. This type of subquery is called a simple subquery.

Example 2: Correlated subquery

If the inner query is dependent on the outer query, we will have a correlated subquery. An example of a correlated subquery is shown below:

SELECT SUM (a1.Sales) FROM Store_Information a1
WHERE a1.Store_Name IN
(SELECT Store_Name FROM Geography a2
WHERE a2.Store_Name = a1.Store_Name);

Result:

SUM (Sales)
2750

Here, the inner query is used to make sure that SQL only sums up sales amount from stores that appear in both the Store_Information and the Geography tables.

Notice the WHERE clause in the inner query, where the condition involves a table from the outer query.

Frequently Asked Questions

Can a subquery return multiple rows?

Yes. When using IN or NOT IN, the subquery can return multiple rows. When using =, >, or other comparison operators, the subquery must return exactly one value (scalar subquery).

What is the difference between a subquery and a CTE?

A Common Table Expression (CTE, written with the WITH clause) is a named temporary result set that improves readability. Subqueries are inline and can be harder to read when nested deeply. CTEs are generally preferred for complex logic.

Can subqueries be used in the SELECT clause?

Yes — called a scalar subquery. For example: SELECT Store_Name, (SELECT MAX(Sales) FROM Store_Information) AS Max_Sales FROM Store_Information. The subquery must return a single value.

Are subqueries slower than JOINs?

Correlated subqueries can be slower because they execute once per row of the outer query. Simple subqueries are often optimized by the database engine. JOINs are generally more efficient for combining tables.

Next: SQL WITH

This page was last updated on March 19, 2026.




Copyright © 2026   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact