skip to content

Subqueries and CTEs

Scalar, row and table subqueries, correlated versus uncorrelated evaluation, EXISTS/IN/ANY/ALL, WITH clauses including recursive CTEs, and CTE versus derived table versus view. Interviewers ask because naming the steps of a hard query — and knowing that a correlated subquery may run per row — is what separates readable SQL from slow SQL.

part ofSQLoverview, primer and where to startread it →
on this pageshow

questions

page 2 of 2

A nightly job suddenly fails with "more than one row returned by a subquery" — how do you fix it?

level: seniorimportance: should knowfreq 40%

basics

~20 s

The query did not change; the data did. A lookup assumed to be unique gained a duplicate. Isolate the subquery, find the duplicated key, then pick the intended semantics — set membership, a tighter unique predicate, or a deliberate aggregate — rather than silencing the error.

open as a page

Using a recursive CTE over a multi-level bill of materials, how do you total each component's required quantity?

level: seniorimportance: should knowfreq 32%

basics

~20 s

Multiply quantities down each path inside the recursive member — the child's quantity times the parent row's accumulated quantity — then GROUP BY component and SUM in the outer query, so a component reached by several paths is totalled correctly.

open as a page

How would you refactor a query with four levels of nested subqueries into stacked CTEs?

level: seniorimportance: should knowfreq 50%

basics

~20 s

Work from the innermost subquery outward: lift each level into its own named CTE in the WITH list, in dependency order, each selecting from the previous one. The result set must be unchanged; only the shape and the names are new.

open as a page

If a WITH clause defines a name that also exists as a table, which one does the query read?

level: middleimportance: nice to knowfreq 28%

basics

~20 s

The CTE wins inside that statement: references in the main query resolve to the WITH definition, not the base table or view of the same name. The shadowing ends with the statement, so the next statement sees the schema object again.

open as a page

What do EXISTS, IN, ANY and ALL return when the subquery returns no rows?

level: middleimportance: nice to knowfreq 28%

basics

~10 s

Over an empty subquery: EXISTS is FALSE and NOT EXISTS TRUE; IN and any ANY predicate are FALSE; NOT IN and any ALL predicate are TRUE, vacuously. None of them is UNKNOWN.

open as a page

What is not allowed inside the recursive member of a WITH RECURSIVE query?

level: middleimportance: nice to knowfreq 30%

basics

~20 s

The recursive member may reference the CTE's own name only once, in its FROM clause, and not inside a subquery. Aggregates, window functions, GROUP BY, HAVING and DISTINCT over the recursive reference are rejected, as is putting it on the null-supplying side of an outer join.

open as a page

What does the optional column list in WITH totals (mth, revenue) AS (...) do?

level: middleimportance: nice to knowfreq 30%

basics

~20 s

It renames the CTE's output columns positionally, so the CTE exposes those names instead of whatever the inner SELECT produced. The list must name exactly as many columns as the query returns, or the statement fails.

open as a page

showing 31–37 of 37