🗄️ SQL examples

ROW_NUMBER Examples in SQL

Number rows inside each group. 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 ROW_NUMBER does

Number rows inside each group. SQL syntax can vary by database, but the pattern below is a useful starting point for reports and analysis.

Syntax or pattern

ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC)
✍️

5 practical examples

1

Latest order per customer

Number orders from newest to oldest for each customer.

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

ROW_NUMBER is the standard latest-record pattern.

2

Remove duplicate emails

Keep one row per email address.

SELECT * FROM ( SELECT c.*, ROW_NUMBER() OVER (PARTITION BY email ORDER BY updated_at DESC) AS rn FROM customers c ) x WHERE rn = 1;

Choose the ordering rule before removing duplicates.

3

Top order per region

Find the largest order in each region.

SELECT * FROM ( SELECT region, order_id, total_amount, ROW_NUMBER() OVER (PARTITION BY region ORDER BY total_amount DESC) AS rn FROM orders ) x WHERE rn = 1;

Partitioning restarts the numbering per region.

4

Number rows for export

Add a sequential row number.

SELECT ROW_NUMBER() OVER (ORDER BY order_date, order_id) AS row_no, * FROM orders;

This creates a stable row number for a report.

5

Find second purchase

Return the second order for each customer.

SELECT * FROM ( SELECT o.*, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS rn FROM orders o ) x WHERE rn = 2;

Changing the filter on rn lets you find first, second or latest events.

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.