Query

SQL Window functions

Calculate values across related rows without collapsing the result set.

Availability: Common SQL with references for SQL standard, MySQL/MariaDB, PostgreSQL, SQL Server, SQLite and Oracle Database; syntax and feature support vary by engine and version.中文

Syntax

function(...) OVER (PARTITION BY ... ORDER BY ...);

Examples

Example (standard-style syntax). Verify supported keywords in your database.

SELECT employee_id, salary, RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS salary_rank FROM employees;

SQLite-oriented variant; dialect behaviour may differ.

SELECT employee_id, salary, RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS salary_rank FROM employees;

Parameters, options and clauses

Option / clauseDescription
SELECT / expressionChoose output columns, expressions, aliases or functions.
FROM / JOINChoose source tables and combine related rows.
WHEREFilter rows before grouping; use IS NULL for null checks.
GROUP BY / HAVINGGroup rows and filter aggregate groups.
ORDER BYSort the result; ASC is commonly the default and DESC reverses it.
LIMIT / OFFSET / FETCHLimit or skip rows; syntax differs by database dialect.

Dialect references

Safety note

Review a command and its target before running it. Commands that alter files, services, accounts, disks, permissions or databases may be difficult to reverse. Test in a safe environment and keep backups.

Official reference ↗Back to directory