MOO WebTech
Subqueries & CTEpracticeadvanced

CTEs Practice

Practice session: building and chaining CTEs, rewriting subqueries, recursive patterns.

80 minL37

Overview

Students practise writing CTEs to solve multi-step analytical problems. Tasks start with simple single-CTE rewrites and progress to chained CTEs and a recursive example.

Lesson Plan

TimeActivity
0–10 minRecap: CTE syntax and chaining
10–20 minWalk through first task together
20–60 minHands-on work: students solve tasks independently
60–75 minReview solutions, discuss readability improvements
75–80 minWrap-up

Key Concepts

  • Naming intermediate results to avoid deeply nested subqueries
  • Each CTE visible only within the same WITH block
  • Debugging CTEs: run each CTE SELECT independently
  • When a CTE improves clarity vs when a simple JOIN is better
  • Recursive CTE anatomy: anchor member, recursive member, UNION ALL

✏️Practice

Tasks will be provided during the lesson.