ROW_NUMBER()

SQL · Window Functions

Theory

ROW_NUMBER() gives each row a unique sequence number, 1, 2, 3 and so on, in the order set by the window ORDER BY. With PARTITION BY, numbering restarts at 1 in every partition. Ties do not matter: two employees with the same salary still get different numbers, and which of them gets the smaller number is not guaranteed unless you add a tie-breaker column.

This makes ROW_NUMBER the standard tool for 'top N per group' and for removing duplicates. For the highest-paid person in each department, number rows with PARTITION BY dept ORDER BY salary DESC and keep rn = 1. For deduplication, partition by the columns that define a duplicate (such as email), order by the latest timestamp, and keep rn = 1. Because window functions cannot appear in WHERE, you filter on rn in an outer query or CTE (Snowflake and some others also offer QUALIFY for this).

Why we use it

You often need exactly one row per group: the latest order per customer, the top earner per department, one copy of each duplicate record. ROW_NUMBER picks exactly one row (or exactly N) even when values tie, which RANK cannot promise.

When to use it

Use ROW_NUMBER when you need exactly N rows per group or a strict de-duplication. If tied rows should share a position (leaderboards), use RANK or DENSE_RANK instead.

How it works

  1. Rows split by PARTITION BY (optional)
  2. Sort each partition by the window ORDER BY
  3. Give the first row 1, then add 1 for each next row
  4. New partition starts? Yes: Reset the counter to 1. No: Keep counting, ties still get new numbers.
  5. Outer query filters, e.g. WHERE rn = 1
  6. One (or N) row per group

Syntax

ROW_NUMBER() OVER (PARTITION BY col ORDER BY col)

Example

SELECT name, department, salary,
  ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn
FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic