// dialect comparison

First N rows per group: ROW_NUMBER vs DISTINCT ON

One window-function query that runs everywhere, plus PostgreSQL's DISTINCT ON shortcut for one row per group.

Filed under Querying

The verdict

ROW_NUMBER() in a CTE is the one form that runs unchanged on all five engines and works for any N. DISTINCT ON is a neat PostgreSQL-only shortcut for N = 1; when porting, rewrite it as ROW_NUMBER() … WHERE rn = 1.

Where they differ

Number rows with ROW_NUMBER() OVER (PARTITION BY group ORDER BY …) in a subquery or CTE and keep the low numbers. The same text runs on all five engines. PostgreSQL also has DISTINCT ON for the one-row-per-group case.

How each engine writes it

SQL Server

WITH ranked AS (SELECT name, department, salary, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn FROM employees) SELECT name, department, salary FROM ranked WHERE rn <= 3;

CROSS APPLY (SELECT TOP 3 … ORDER BY salary DESC) against a departments table is the other common T-SQL form.

PostgreSQL

WITH ranked AS (SELECT name, department, salary, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn FROM employees) SELECT name, department, salary FROM ranked WHERE rn <= 3;

For one row per group, SELECT DISTINCT ON (department) department, name, salary FROM employees ORDER BY department, salary DESC is shorter. A LATERAL join is the other N-per-group form.

MySQL

WITH ranked AS (SELECT name, department, salary, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn FROM employees) SELECT name, department, salary FROM ranked WHERE rn <= 3;

CTEs and window functions need MySQL 8.0+. On 5.7, use the correlated COUNT subquery in the FAQ below.

SQLite

WITH ranked AS (SELECT name, department, salary, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn FROM employees) SELECT name, department, salary FROM ranked WHERE rn <= 3;

Window functions need SQLite 3.25+.

Oracle

WITH ranked AS (SELECT name, department, salary, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn FROM employees) SELECT name, department, salary FROM ranked WHERE rn <= 3;

Analytic functions date back to Oracle 8i. 12c+ can also use CROSS APPLY or LATERAL with FETCH FIRST 3 ROWS ONLY.

Questions that come up

What is the difference between ROW_NUMBER, RANK and DENSE_RANK here?

ROW_NUMBER returns at most N rows per group and breaks ties arbitrarily. RANK keeps ties and skips numbers (1, 1, 3); DENSE_RANK keeps ties without gaps (1, 1, 2). Both can return more than N rows when salaries tie. Use ROW_NUMBER for exactly N, RANK when everyone tied at the cut-off should be included.

How do I do this on MySQL 5.7?

Count how many rows in the same group beat each row: SELECT * FROM employees e WHERE (SELECT COUNT(*) FROM employees e2 WHERE e2.department = e.department AND e2.salary > e.salary) < 3. Ties behave like RANK, and the cost grows with the square of the group size, so index (department, salary).

Can I just use GROUP BY with MAX?

Only for N = 1, and only if you need nothing but the value. SELECT department, MAX(salary) tells you the top salary, not who earns it; adding name to the GROUP BY returns every employee instead of the top one.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.