🗄️ SQL examples

CTE Examples in SQL

Use WITH clauses to organize complex queries. This page gives you the syntax, five practical examples, common mistakes, and copy-ready SQL you can adapt.

Updated 2026-06-125 practical examplesCopy-ready SQL

What CTE does

Use WITH clauses to organize complex queries. SQL syntax can vary by database, but the pattern below is a useful starting point for reports and analysis.

Syntax or pattern

WITH summary AS (SELECT ... ) SELECT * FROM summary;
✍️

5 practical examples

1

Create a reusable sales summary

Build a named temporary result for later filtering.

WITH sales_by_customer AS ( SELECT customer_id, SUM(total_amount) AS total_sales FROM orders GROUP BY customer_id ) SELECT * FROM sales_by_customer WHERE total_sales > 1000;

CTEs make multi-step logic easier to read.

2

Rank after summarizing

Summarize first, then rank.

WITH product_sales AS ( SELECT product_id, SUM(amount) AS sales FROM sales GROUP BY product_id ) SELECT product_id, sales, RANK() OVER (ORDER BY sales DESC) AS sales_rank FROM product_sales;

CTEs help when a window function depends on an aggregate.

3

Filter latest records

Use ROW_NUMBER inside a CTE.

WITH ranked_orders AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS rn FROM orders ) SELECT * FROM ranked_orders WHERE rn = 1;

This is a common latest-record-per-group pattern.

4

Combine several steps

Break a report query into readable stages.

WITH base AS (SELECT * FROM orders WHERE status = 'Completed'), monthly AS (SELECT DATE_TRUNC('month', order_date) AS month, SUM(total_amount) AS sales FROM base GROUP BY 1) SELECT * FROM monthly;

Named stages are easier to maintain than one giant query.

5

Debug intermediate results

Select from the CTE while building the query.

WITH filtered AS ( SELECT * FROM orders WHERE order_date >= DATE '2026-01-01' ) SELECT * FROM filtered;

A CTE can be tested before adding more logic.

Common mistakes to avoid

  • Forgetting that SQL dialects vary across PostgreSQL, SQL Server, MySQL, BigQuery and SQLite.
  • Using SELECT * in production reports when only a few columns are needed.
  • Not checking join keys, duplicate rows or NULL values before trusting results.

FAQ

Will this SQL work in every database?

The idea is portable, but function names and date syntax may vary. Check your database dialect if a function is not recognized.

Should I use this in a report query?

Yes, if the pattern matches the business question and you have checked filters, joins and row counts.

Why does my result have too many rows?

The most common reasons are duplicate join keys, missing filters or grouping at the wrong level of detail.

💡 Useful resources

Here are some ideas for you

Optional resources that may help if you are learning SQL, building reports, writing queries or improving your data workflow.

  • 📘
    SQL books for beginners

    Practice query patterns with structured examples and exercises.

    See ideas
  • 🧱
    Database design books

    Understand tables, keys, relationships and why joins behave the way they do.

    See ideas
  • ⌨️
    Mechanical keyboards

    Useful if you write queries, code and documentation for long work sessions.

    See ideas
  • 🖥️
    External monitors

    View query editor, result grid and documentation side by side.

    See ideas
  • 📒
    Developer notebooks

    Sketch table relationships, query logic and report ideas before coding.

    See ideas
  • 💡
    Desk lamps

    Keep your workspace comfortable while studying or debugging queries.

    See ideas

Some links in this section may be affiliate links. Choose only what is useful for your own work.