Learn SQL

SQL Subqueries vs CTEs: When to Use WITH, IN, EXISTS, and Correlated Subqueries

Subqueries nest one question inside another; CTEs give each step a name. Learn which form to use, and the NULL trap that makes NOT IN return nothing.

Published 2026-10-29·Updated 2026-10-29

Subqueries answer a question inside a question

Who earns more than the average? To answer that, SQL first needs the average, then the comparison. You cannot write WHERE salary > AVG(salary), because WHERE filters single rows before any aggregation runs. A subquery computes the average as its own small query.

SELECT name, salary
FROM team_members
WHERE salary > (SELECT AVG(salary) FROM team_members)
ORDER BY salary DESC;

A subquery that returns exactly one value is a scalar subquery and can go anywhere a single value fits. These examples come from the Subqueries and CTEs chapter in SQL for Files, a browser SQL editor that runs DuckDB locally, so you can try them on sample data without installing anything.

IN and EXISTS test for related rows

A subquery that returns a list works with IN. EXISTS asks whether the inner query returns at least one row, and its inner query can refer to the current outer row.

-- Members with at least one project
SELECT m.name
FROM team_members AS m
WHERE EXISTS (
  SELECT 1 FROM projects AS p WHERE p.member_id = m.id
);

The NOT IN NULL trap

To find rows without a match, NOT IN looks natural. But if the subquery returns even one NULL, x NOT IN (1, 2, NULL) can never be true, and the query returns no rows at all. NOT EXISTS does not have this problem.

-- Members without any project, safe even when member_id has NULLs
SELECT m.name
FROM team_members AS m
WHERE NOT EXISTS (
  SELECT 1 FROM projects AS p WHERE p.member_id = m.id
)
ORDER BY m.name;

Rule of thumb

For has-no-match questions, prefer NOT EXISTS or a LEFT JOIN with IS NULL. Use NOT IN only when the subquery column can never be NULL.

CTEs name each step with WITH

A common table expression (CTE) is a named subquery defined at the top of a query. It does not add new power, but it makes multi-step logic read from top to bottom, and later steps can reuse earlier ones.

WITH team_stats AS (
  SELECT team, AVG(salary) AS avg_salary
  FROM team_members
  GROUP BY team
)
SELECT team, avg_salary
FROM team_stats
WHERE avg_salary > 70000
ORDER BY avg_salary DESC;

Correlated subqueries compare each row with its own group

A correlated subquery refers to the outer row, so it is evaluated per row. It is a direct way to compare each person with the average of their own team. A CTE plus a join expresses the same idea and is often easier to debug.

SELECT m.name, m.team, m.salary
FROM team_members AS m
WHERE m.salary > (
  SELECT AVG(t.salary)
  FROM team_members AS t
  WHERE t.team = m.team
)
ORDER BY m.name;

Subquery or CTE: how to choose

  • One value or a short list used once: an inline subquery is fine.
  • Several steps, or a result you reuse: name the steps with WITH.
  • Has-no-match questions: NOT EXISTS instead of NOT IN.
  • Filtering on window function results: compute them in a CTE, then filter in the outer query.

Practice subqueries and CTEs

  1. Open the Subqueries and CTEs chapter in the SQL for Files Learn SQL course.
  2. Load the team_members and projects sample tables.
  3. Solve the challenges, including the NOT IN trap and a correlated subquery.
  4. Revisit the Execution Order chapter to see why window functions need a CTE before you filter them.

Continue in the editor

Open SQL for Files to add your own CSV, JSON, or Parquet files and try these examples locally in your browser.