SQL Fundamentals

Subqueries and Common Table Expressions

Use subqueries and common table expressions to separate query stages, control dependencies, and verify intermediate results before composing the final answer.

Intermediate14 min read
SQL Fundamentals lessonRelational data foundationsPractice

UNTIMED COACHING

Practice before the pressure

Use feedback to correct the model, not merely memorize an option position.

GUIDED PRACTICE

Practice the lesson questions

Answer normal lesson questions without a timer. Every answer includes an explanation, and incorrect answers can be tried again before continuing.

CONCEPT MODELS

See the lesson as a system

Use these visual guides to connect the key ideas before answering the questions.

Connect the roles in Subqueries and Common Table ExpressionsKeep inner query, intermediate result, outer query, and cte name as separate roles.
Connect the roles in Subqueries and Common Table ExpressionsA topic-specific model connects four distinct roles used to reason about subqueries and common table expressions.
SQL commandCommand computes one intermediate relation
Verified resultResult exposes one testable query stage
Outer queryCommand compares groups with the average
Table recordTable name exposes a temporary result
Verify Dependency checkConnect dependency check with its topic-specific inspection, expected outcome, and protected database boundary.
Verify Dependency checkA verification model for subqueries and common table expressions connects the final data decision to its check, result, and protected boundary.
Output evidenceEvidence verifies query stage dependencies
Starting checkReview output evidence
Expected resultEvery query stage has the keys and cardinality
Safety boundaryProtect sql command scope