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.
On This Page
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;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
- Open the Subqueries and CTEs chapter in the SQL for Files Learn SQL course.
- Load the team_members and projects sample tables.
- Solve the challenges, including the NOT IN trap and a correlated subquery.
- 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.
Related guides
SQL Execution Order Explained: Why WHERE, GROUP BY, HAVING, SELECT, and LIMIT Behave Differently
SQL is written in one order but understood in another; learning the logical order explains many beginner surprises.
SQL NULL Explained: How Missing Values Work and Why They Matter
NULL is SQL's way of saying a value is missing or unknown — and it behaves differently from almost every other value beginners expect.
SQL JOINs Explained: INNER, LEFT, FULL OUTER, SELF, and CROSS JOIN
JOINs bring related tables back together: matched rows, missing matches, hierarchies, and all-combination grids all use different JOIN patterns.