Learn SQL
-- subqueries.derived_any_all

Derived Tables & ANY / ALL in SQL

Shape-then-join; compare against every value

--concept

A FROM subquery (derived table) produces a named temporary relation — alias mandatory — that the outer query treats as a table: pre-aggregate, filter, then join. ANY/SOME compare against at least one value (= ANY is IN); ALL compares against every value (> ALL means above the max). NULLs in the set poison NOT IN and ALL to UNKNOWN.

--how it works
1

Derived shapes first

FROM (SELECT …) builds a named temp relation. Alias mandatory.

2

Join the shape

Outer query treats it as a table — pre-aggregated, fan-out-proof.

3

ANY is one, ALL is every

= ANY is IN. > ALL means above the max. NULLs poison ALL/NOT IN.

4

CTE when nested

Same power, better names + multi-reference. Past one level prefer WITH.

--visualization
dept_idavg_pay
Engineering83000
Sales55000
Marketing47000
Stage 1 / 1
Derived rolls 6 rows to 3 averages — join keys unique, no replication. Alias mandatory.
--example
query.sql
1SELECT d.dept_id, t.avg_pay FROM departments d JOIN (SELECT department, AVG(salary) AS avg_pay FROM employees GROUP BY department) t ON t.department = d.dept_id;
Result
dept_idavg_pay
Engineering83000
Sales55000
Marketing47000

The derived table pre-aggregates to one row per department, then joins cleanly — no fan-out. The same shape with > ALL would compare each salary against every value in the set.

--variants

Pre-aggregate then join

One row per key before widening.

query.sql
1SELECT d.dept_id, t.avg_pay FROM departments d JOIN (SELECT department, AVG(salary) AS avg_pay FROM employees GROUP BY department) t ON t.department = d.dept_id;
Result
dept_idavg_pay
Engineering83000
Sales55000
Marketing47000
1Derived table rolls six rows to three averages.
2Join keys are now unique — no replication.
3CTE form reads identically with a name.

> ALL means above max

Quantifier comparison beyond equality.

query.sql
1SELECT name FROM employees WHERE salary > ALL (SELECT salary FROM employees WHERE department = 'Sales');
Result
name
Alice
Cynthia
Farid
1Subquery yields Sales salaries (52k, 58k).
2> ALL requires beating every one — above 58k.
3Alice/Cynthia/Farid qualify; NULL in set would poison to UNKNOWN.
--key facts
Alias mandatoryderived tables need a name
ANY-one vs ALL-everythe quantifier split
Derived vs CTEsame power; CTE reads better past one level
--use cases
Fan-out-proof reporting: aggregate before widening
Threshold screens: above-max, below-min checks
--under the hood
Derived tables are scope-limited to their query; planners flatten simple ones into joins — EXPLAIN shows the merge.
ANY/ALL with NULL sets evaluate to UNKNOWN — NOT EXISTS is the safe anti-pattern.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.