← Back to SQL, page 7

sql-0121 · Hard senior

Why does LAST_VALUE(salary) OVER (ORDER BY salary) return the current row's salary instead of the highest salary?

With ORDER BY and no frame clause, the frame ends at the current row (and its peers). LAST_VALUE takes the last row of that frame, which is the current row. Extend the frame with ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, or use FIRST_VALUE with the opposite sort order.

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;
namesalaryfirst_vallast_val
Omar480004800048000
Diego550004800055000
Sara620004800062000
Tom700004800070000
Mei950004800095000
Ravi950004800095000
Asha12000048000120000

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;
namesalaryhighest
Omar48000120000
Diego55000120000
Sara62000120000
Tom70000120000
Mei95000120000
Ravi95000120000
Asha120000120000

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