Solve “Deduplicate events with ROW_NUMBER”

Free. Runs in your browser, no install.

SQL · Window Functions

What is a window function?

Mediumwindow functionsoveranalytics

Interview question

What is a window function?

How to answer

The short answer

A performs calculations across related rows without collapsing them.

Example:

SQL
SELECT
    employee,
    department,
    salary,
    AVG(salary) OVER (PARTITION BY department) AS dept_avg
FROM employees;

Every employee remains a row.

Compare:

SQL
GROUP BY department

which collapses multiple employees into one department row.

Difference

> GROUP BY reduces rows. > Window functions keep rows.

Lock it in with a real drill

You remember an answer longer once you have used it. Try a hands-on problem on this concept.

Open related drill