Chain multiple named steps in one query.
Section: Common Table Expressions
Multiple CTEs
sql
sql
WITH order_totals AS (
SELECT customer_id, SUM(amount) AS total_spent
FROM orders
GROUP BY customer_id
),
ranked AS (
SELECT customer_id, total_spent
FROM order_totals
)
SELECT *
FROM ranked;Explanation
Multiple CTEs are useful for staged transformations.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Common Table Expressions