Full answer
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');The query as written:
SELECT dept_id, name, salary FROM employees WHERE ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC) = 1; -- error on purposeError: misuse of window function ROW_NUMBER()SQLite rejects the query as a misuse of the window function. WHERE decides which rows exist before any window is evaluated, so the row numbers are not available yet. PostgreSQL rejects it too, saying window functions are not allowed in WHERE (not run here).
A corrected version:
SELECT dept_id, name, salary FROM (
SELECT dept_id, name, salary, ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC, id) AS rn
FROM employees WHERE dept_id IS NOT NULL
) WHERE rn = 1 ORDER BY dept_id;| dept_id | name | salary |
|---|---|---|
| 1 | Asha | 120000 |
| 2 | Tom | 70000 |
| 3 | Diego | 55000 |
The inner query numbers rows per department; the outer query keeps the first. Asha leads Engineering, Tom leads Sales and Diego leads Support. The id tiebreaker keeps the choice stable if two people share the top salary; use RANK instead when all tied top earners should be listed. Some engines (for example DuckDB and Snowflake) offer a QUALIFY clause for this pattern, but SQLite and PostgreSQL do not.
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');
-- error on purpose
SELECT dept_id, name, salary FROM employees WHERE ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC) = 1;
SELECT dept_id, name, salary FROM (
SELECT dept_id, name, salary, ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC, id) AS rn
FROM employees WHERE dept_id IS NOT NULL
) WHERE rn = 1 ORDER BY dept_id;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Window functions run after WHERE
- Filter window results in an outer query
- Add a tiebreaker or use RANK for ties
Common mistakes
- Trying HAVING instead, which also runs before windows
- Omitting a tiebreaker
Follow-ups
- How would you return the top two per department?
- What does QUALIFY do in engines that support it?
Tips
- Recall the logical clause order