How to Use ROW_NUMBER() in MySQL

Galaxy Glossary

How do I generate row numbers in MySQL?

Generates a sequential number for each row, ordered and optionally partitioned, enabling easy ranking, pagination, and de-duplication.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

What does ROW_NUMBER() do in MySQL?

ROW_NUMBER() returns a unique, consecutive integer for every row in the result set. The counter restarts whenever the optional PARTITION BY clause changes, and the sequence follows the ORDER BY expression inside the window.

Why would I need ROW_NUMBER()?

Use it for pagination, selecting the first product per category, removing duplicates while keeping the newest record, or assigning a stable rank without session variables.

How do I write the ROW_NUMBER() syntax?

Place ROW_NUMBER() in the SELECT list, add an OVER() clause, include an ORDER BY for deterministic results, and optionally a PARTITION BY to restart counting within logical groups.

When should I use PARTITION BY?

Partition when you want numbering to restart per customer, per product, or any other business dimension—e.g., order sequencing per customer.

How do I filter by row numbers?

Wrap the query in a CTE or derived table, then add a WHERE clause on the generated column (e.g., row_num = 1) to keep only the desired rows.

Best practices for ROW_NUMBER()

Always specify ORDER BY. Index the ordering columns for performance. Use meaningful aliases like row_num. Keep window functions in a subquery when further filtering.

Practical ROW_NUMBER() example

WITH ordered_orders AS (
SELECT
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS row_num,
id, customer_id, order_date, total_amount
FROM Orders)
SELECT *
FROM ordered_orders
WHERE row_num = 1;

This returns each customer's earliest order.

Common mistakes and fixes

Skipping ORDER BY makes row numbers nondeterministic—always provide it. Forgetting PARTITION BY when needed causes numbering to run globally; declare it to restart counts logically.

Why How to Use ROW_NUMBER() in MySQL is important

How to Use ROW_NUMBER() in MySQL Example Usage


SELECT
  ROW_NUMBER() OVER (PARTITION BY o.customer_id ORDER BY o.order_date) AS order_sequence,
  o.id,
  o.customer_id,
  o.order_date,
  o.total_amount
FROM Orders o;

How to Use ROW_NUMBER() in MySQL Syntax


SELECT
  ROW_NUMBER() OVER (
      [PARTITION BY partition_expression]
      ORDER BY sort_expression [ASC|DESC]
  ) AS row_num
FROM table_name;

Common Mistakes

Frequently Asked Questions (FAQs)

Can I use ROW_NUMBER() without PARTITION BY?

Yes. MySQL will number the entire result set once. Add PARTITION BY only when you need separate sequences per group.

Is ROW_NUMBER() available in all MySQL versions?

The function is supported from MySQL 8.0 onward. Earlier versions require user-defined variables to emulate the behavior.

How do I paginate with ROW_NUMBER()?

Wrap the query in a CTE or subquery, then filter by row_num BETWEEN (page-1)*page_size+1 AND page*page_size.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.