Full answer
sql
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');sql
SELECT name, salary,
FIRST_VALUE(salary) OVER (ORDER BY salary) AS first_val,
LAST_VALUE(salary) OVER (ORDER BY salary) AS last_val
FROM employees
WHERE salary IS NOT NULL
ORDER BY salary, name;| name | salary | first_val | last_val |
|---|---|---|---|
| Omar | 48000 | 48000 | 48000 |
| Diego | 55000 | 48000 | 55000 |
| Sara | 62000 | 48000 | 62000 |
| Tom | 70000 | 48000 | 70000 |
| Mei | 95000 | 48000 | 95000 |
| Ravi | 95000 | 48000 | 95000 |
| Asha | 120000 | 48000 | 120000 |
FIRST_VALUE looks right: the frame always starts at the first row, the lowest salary. LAST_VALUE just echoes the current salary, because the default frame stops at the current row. For Ravi and Mei it shows 95000, which happens to be both their own salary and the last peer in their frame.
Extend the frame to the whole partition:
sql
SELECT name, salary,
LAST_VALUE(salary) OVER (ORDER BY salary
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS highest
FROM employees
WHERE salary IS NOT NULL
ORDER BY salary, name;| name | salary | highest |
|---|---|---|
| Omar | 48000 | 120000 |
| Diego | 55000 | 120000 |
| Sara | 62000 | 120000 |
| Tom | 70000 | 120000 |
| Mei | 95000 | 120000 |
| Ravi | 95000 | 120000 |
| Asha | 120000 | 120000 |
An equivalent and often clearer version avoids LAST_VALUE entirely: FIRST_VALUE(salary) OVER (ORDER BY salary DESC) or simply MAX(salary) OVER ().
The same frame issue affects NTH_VALUE. Whenever a window function's answer depends on rows after the current one, check the frame.
sql
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');
SELECT name, salary,
FIRST_VALUE(salary) OVER (ORDER BY salary) AS first_val,
LAST_VALUE(salary) OVER (ORDER BY salary) AS last_val
FROM employees
WHERE salary IS NOT NULL
ORDER BY salary, name;
SELECT name, salary,
LAST_VALUE(salary) OVER (ORDER BY salary
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS highest
FROM employees
WHERE salary IS NOT NULL
ORDER BY salary, name;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Default frame with ORDER BY ends at the current row
- LAST_VALUE returns the last row of the frame, not the partition
- Use ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
- FIRST_VALUE with reversed order or MAX() OVER () are simpler alternatives
Common mistakes
- Trusting LAST_VALUE without a frame clause
- Thinking the bug is in the data because values 'look random'
Follow-ups
- How would you show each employee's salary next to the lowest and highest in their department?
- What does NTH_VALUE(salary, 2) return with the default frame?
Tips
- Name the default frame when explaining the result