A query inside a query; a CTE (WITH …) names it for readable, reusable, recursive steps.
Name an intermediate result and use it like a table. It turns one unreadable nested query into a sequence of readable steps.
It's how real analytical SQL stays maintainable, and CTEs are what interviewers hope you'll reach for.
A subquery is a SELECT nested inside another statement — in WHERE (IN (SELECT …)), in FROM as a derived table, or correlated to the outer row so it runs per row. A common table expression, written WITH name AS (…), lifts that logic out front and names it, so the main query reads top-down. CTEs can be referenced multiple times and can recurse to walk hierarchies.
Subqueries are queries within queries: they filter (WHERE … IN), derive tables (FROM), or correlate to each outer row. CTEs (WITH name AS …) give a subquery a name up front, making complex SQL readable, reusable, and — with WITH RECURSIVE — able to traverse trees and graphs.
Advanced SQL Tutorial | CTE (Common Table Expression) — Alex The Analyst, 3:43