MySQL 8.0 window functions
Window functions require MySQL 8.0 or newer: ROW_NUMBER, RANK, LAG, ROWS/RANGE frames and the WINDOW clause all arrived in the 8.0 series.
- Categoria
- Janela
- Returns
n/a (version gate)- In this category
- 70 of 79
Sintaxe
ROW_NUMBER() OVER (PARTITION BY g ORDER BY d) -- MySQL 8.0+
Suporte a mecanismos
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | Not built in | No built-in equivalent. |
| MySQL | Sim | 8.0+; nothing in 5.7 |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Not built in | No built-in equivalent. |
Exemplos
MySQL
SELECT department, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn,
SUM(salary) OVER (PARTITION BY department ORDER BY hire_date) AS running_payroll
FROM employees;
Bom saber
- Still missing in MySQL 8: GROUPS frames, EXCLUDE, FILTER, NULLS FIRST/LAST, IGNORE NULLS, NTH_VALUE ... FROM LAST, DISTINCT in window aggregates and GROUP_CONCAT as a window function. Several of these parse and then fail with 'This version of MySQL doesn't yet support'. RANGE frames with INTERVAL offsets do work.
- The 5.7-era workaround, user variables (@rownum := @rownum + 1), relies on evaluation order MySQL never guaranteed, and setting variables inside expressions is deprecated since 8.0.13. Replace it with ROW_NUMBER() when you upgrade.
- MariaDB split from MySQL before 8.0 and ships window functions from 10.2; 'MySQL-compatible' does not mean the same version gate.
Questions about MySQL 8.0 Window Functions
Do window functions work on MySQL 5.7?
No, 5.7 errors on any OVER clause. The user-variable emulation depends on evaluation order MySQL does not guarantee; correlated subqueries or self-joins are slower but correct.
Which window features does MySQL 8.0 lack?
GROUPS frames, EXCLUDE, FILTER, NULLS FIRST/LAST, IGNORE NULLS, FROM LAST, DISTINCT inside window aggregates and GROUP_CONCAT over windows. Ranking, LAG/LEAD, FIRST/LAST/NTH_VALUE, ROWS and RANGE frames (including INTERVAL offsets) and the WINDOW clause all work.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: MySQL.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.