Use a CTE to structure a multi-step query.
Section: Querying, Joins, and Aggregation
Common table expression
sql
sql
WITH recent_orders AS (
SELECT * FROM orders WHERE created_at >= now() - interval '7 days'
)
SELECT customer_id, count(*)
FROM recent_orders
GROUP BY customer_id;Explanation
See summary for usage details.
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 Querying, Joins, and Aggregation