// window reference

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.

카테고리
창
Returns
n/a (version gate)
In this category
70 of 79

구문

ROW_NUMBER() OVER (PARTITION BY g ORDER BY d)  -- MySQL 8.0+

엔진 지원

엔진Support메모
SQL Server Not built in No built-in equivalent.
PostgreSQL Not built in No built-in equivalent.
MySQL 예 8.0+; nothing in 5.7
SQLite Not built in No built-in equivalent.
Oracle Not built in No built-in equivalent.

예시

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;

알아 두면 좋은 정보

  • 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.

Cheat sheets for the engines that have it: MySQL.

// fewer lookups

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.