Syntax Cache
BlogMethodFeaturesHow It WorksBuild a Game
  1. Home
  2. SQL
  3. SQL CTEs Practice
SQL11 exercises

SQL CTEs Practice

WITH clauses, recursive CTEs, chained CTEs. Break complex queries into steps you can actually read.

Warm-up1 / 2

Can you write this from memory?

Create a CTE named 'active_users' that selects all users where status = 'active', then select all columns from it.

A 50-line SQL query with nested subqueries is a nightmare to debug. The same logic as a chain of CTEs is readable. Each WITH block is a named step you can test independently.

These exercises cover WITH syntax, chaining multiple CTEs, and recursive queries for hierarchical data. Once CTEs click, you won't go back to deeply nested subqueries.

Related SQL Topics
SQL SubqueriesSQL JoinsSQL Aggregation: GROUP BY, HAVING, COUNT, SUM, AVG

CTEs are named, readable steps. Use them to replace deeply nested subqueries and to reference the same derived table multiple times. For a quick reference, see the SQL CTEs cheat sheet.

CTEs shine when a query has multiple transformation steps. Instead of nesting subqueries three levels deep, name each step:

WITH monthly_revenue AS (
  SELECT DATE_TRUNC('month', order_date) AS month, SUM(amount) AS revenue
  FROM orders
  GROUP BY 1
),
growth AS (
  SELECT month, revenue,
    LAG(revenue) OVER (ORDER BY month) AS prev_revenue
  FROM monthly_revenue
)
SELECT month, revenue,
  ROUND(100.0 * (revenue - prev_revenue) / prev_revenue, 1) AS growth_pct
FROM growth;

This example combines CTEs with window functions — a common pattern for analytics queries. CTEs also simplify complex JOIN chains by pre-aggregating data to prevent row explosion.


Ready to practice?

Start practicing SQL CTEs with spaced repetition

PostgreSQL 12+ inlines non-recursive CTEs by default (treating them like subqueries for optimization). Add MATERIALIZED to force evaluation:

WITH MATERIALIZED expensive AS (
  SELECT * FROM large_table WHERE complex_condition
)
SELECT * FROM expensive e1 JOIN expensive e2 ON e1.id = e2.parent_id;

MySQL always materializes CTEs. SQLite inlines when possible.

When to Use SQL CTEs

  • Break complex queries into readable steps.
  • Reuse a derived dataset multiple times in a query.
  • Model hierarchies with recursive CTEs.

Check Your Understanding: SQL CTEs

Prompt

Build a recursive query to fetch category hierarchies.

What a strong answer looks like

Use WITH RECURSIVE with a base case and a UNION ALL that joins to children.

What You'll Practice: SQL CTEs

Basic WITH clause syntaxMultiple CTEs in one queryRecursive CTEs for hierarchiesCTEs vs subqueries (when to use which)CTE for code reuse in queriesMaterialized vs inline CTEs

Common SQL CTEs Pitfalls

  • CTE scope is limited to the following statement
  • Recursive CTEs need a base case
  • Some databases materialize CTEs (performance impact)

SQL CTEs FAQ

How long does a CTE live?

Only for the single statement that follows it; it is not persisted.

Are CTEs always optimized?

Not always. Some databases materialize CTEs; check your engine and profile performance.

Can you use multiple CTEs in one query?

Yes. Use a single WITH keyword followed by comma-separated CTE definitions: `WITH cte1 AS (...), cte2 AS (...) SELECT ...`. Later CTEs can reference earlier ones. This is a clean alternative to deeply nested subqueries.

SQL CTEs Syntax Quick Reference

Basic CTE
WITH active_users AS (
  SELECT * FROM users
  WHERE status = 'active'
)
SELECT * FROM active_users;
Multiple CTEs
WITH orders_2024 AS (
  SELECT * FROM orders WHERE year = 2024
),
high_value AS (
  SELECT * FROM orders_2024 WHERE amount > 1000
)
SELECT * FROM high_value;
Recursive
WITH RECURSIVE hierarchy AS (
  SELECT id, name, 1 as level FROM categories WHERE parent_id IS NULL
  UNION ALL
  SELECT c.id, c.name, h.level + 1
  FROM categories c JOIN hierarchy h ON c.parent_id = h.id
)
SELECT * FROM hierarchy;

SQL CTEs Sample Exercises

Example 1Difficulty: 2/5

Fill in the keyword to define a Common Table Expression

WITH
Example 2Difficulty: 2/5

Can a CTE defined in one query be used in a completely separate query?

no
Example 3Difficulty: 3/5

Fill in the separator between multiple CTE definitions

,

+ 8 more exercises

Quick Reference
SQL CTEs Cheat Sheet →

Copy-ready syntax examples for quick lookup

Start practicing SQL CTEs

Free daily exercises with spaced repetition. No credit card required.

← Back to SQL Syntax Practice
Syntax Cache

Build syntax muscle memory with spaced repetition.

Product

  • Pricing
  • Our Method
  • Daily Practice
  • Design Patterns
  • Interview Prep

Resources

  • Blog
  • Compare
  • Cheat Sheets
  • Vibe Coding
  • Muscle Memory

Languages

  • Python
  • JavaScript
  • TypeScript
  • Rust
  • SQL
  • GDScript

Legal

  • Terms
  • Privacy
  • Contact

© 2026 Syntax Cache

Cancel anytime in 2 clicks. Keep access until the end of your billing period.

No refunds for partial billing periods.