How to use ROW_NUMBER() in MariaDB

Galaxy Glossary

How do I assign sequential row numbers in MariaDB?

ROW_NUMBER() assigns a unique sequential integer to each row within a query’s result set or a specified partition.

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

Table of Contents

What does ROW_NUMBER() do in MariaDB?

ROW_NUMBER() returns an ascending integer starting at 1 for every row in the result set. When used with PARTITION BY, the count restarts for each partition.

What is the syntax for ROW_NUMBER()?

The window function’s general form is:

ROW_NUMBER() OVER ( [PARTITION BY expr_list] [ORDER BY sort_list] )

PARTITION BY groups rows before numbering, while ORDER BY defines numbering order.

How to generate simple row numbers?

SELECT
ROW_NUMBER() OVER (ORDER BY id) AS rn,
id, name, price
FROM Products;

This query labels every product sequentially by primary key.

How to reset row numbers per customer?

SELECT
customer_id,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS order_seq,
id AS order_id, order_date, total_amount
FROM Orders;

Each customer’s orders start at 1, ordered by date.

How to identify the first product in each order?

SELECT
oi.order_id,
oi.product_id,
oi.quantity,
ROW_NUMBER() OVER (PARTITION BY oi.order_id ORDER BY oi.id) AS item_seq
FROM OrderItems oi
HAVING item_seq = 1;

The HAVING clause filters to the first row per order.

Best practices when using ROW_NUMBER()

Always add an ORDER BY inside OVER to guarantee deterministic numbering. Use indexed columns to keep the operation fast on large datasets.

What common mistakes should I avoid?

Omitting ORDER BY yields an indeterminate sequence. Forgetting PARTITION BY when needed causes numbering to continue across groups.

Why How to use ROW_NUMBER() in MariaDB is important

How to use ROW_NUMBER() in MariaDB Example Usage


-- Top 3 most expensive products per order
WITH ranked_items AS (
  SELECT 
      order_id,
      product_id,
      price * quantity AS item_total,
      ROW_NUMBER() OVER (PARTITION BY order_id ORDER BY price * quantity DESC) AS price_rank
  FROM OrderItems oi
  JOIN Products p ON p.id = oi.product_id
)
SELECT order_id, product_id, item_total
FROM ranked_items
WHERE price_rank <= 3;

How to use ROW_NUMBER() in MariaDB Syntax


ROW_NUMBER() OVER (
    [PARTITION BY partition_expression [, ...]]
    [ORDER BY sort_expression [ASC|DESC] [, ...]]
)

-- Example in an ecommerce context
SELECT 
    ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS order_rank,
    id AS order_id,
    customer_id,
    order_date,
    total_amount
FROM Orders;

Common Mistakes

Frequently Asked Questions (FAQs)

Can I filter directly on ROW_NUMBER()?

Yes. You can wrap the window function in a CTE or subquery and apply WHERE or HAVING to the generated column, e.g., WHERE rn = 1.

Does ROW_NUMBER() require an index?

Not technically, but using indexed columns in ORDER BY improves performance, especially on large tables.

How is ROW_NUMBER() different from RANK()?

ROW_NUMBER() assigns unique integers with no gaps. RANK() skips numbers when ties occur, leaving gaps in the sequence.

Want to learn about other SQL terms?

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