Step 1 of 11
SQL subqueries and CTEs: a query inside a query
In this lesson you practise SQL subqueries and CTEs on three small tables from a sales team: you put one query inside another, use what the inner query returns as a value, a list or a table, and name the steps of a long query with WITH. Every exercise runs here, and you check your answers as you go.
Here is the whole idea in one picture. The part in brackets is a query of its own. It runs first, its result goes where the brackets were, and only then does the outer query run. Pick a question and watch the inner result travel into the outer query.
In this lesson you'll
- answer a question too big for one query by putting a query inside another
- use an inner query as one value, as a list for IN, and as a table in FROM
- write correlated subqueries with EXISTS, and avoid the NOT IN and NULL trap
- name each step of a long query with WITH, and read any nested query from the inside out
About 85 minutes, with a short check after the first four ideas, a playground, a practice set and a final quiz.