← Back to SQL, page 15

sql-0295 · Medium mid

What does NTH_VALUE(salary, 2) OVER (ORDER BY salary DESC) return on the first row, and how do you get the second-highest salary on every row?

On the first row it returns NULL, because the default frame ends at the current row and holds only one row. Extend the frame to UNBOUNDED FOLLOWING so every row sees the whole ordered set. NTH_VALUE counts rows, so tied salaries count separately.

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, NTH_VALUE(salary, 2) OVER (ORDER BY salary DESC, name) AS second
FROM employees WHERE salary IS NOT NULL ORDER BY salary DESC, name LIMIT 3;
namesalarysecond
Asha120000NULL
Mei9500095000
Ravi9500095000

With the default frame, the first row's frame contains only Asha, so there is no second value. From the second row on, the frame includes Mei or Ravi, so 95000 appears.

sql
SELECT name, salary,
       NTH_VALUE(salary, 2) OVER (ORDER BY salary DESC, name ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS second
FROM employees WHERE salary IS NOT NULL ORDER BY salary DESC, name LIMIT 3;
namesalarysecond
Asha12000095000
Mei9500095000
Ravi9500095000

With the full frame, every row shows 95000. Because NTH_VALUE counts rows, the third value is also 95000 (the other tied employee). For the second distinct salary, use DENSE_RANK instead.

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, NTH_VALUE(salary, 2) OVER (ORDER BY salary DESC, name) AS second
FROM employees WHERE salary IS NOT NULL ORDER BY salary DESC, name LIMIT 3;

SELECT name, salary,
       NTH_VALUE(salary, 2) OVER (ORDER BY salary DESC, name ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS second
FROM employees WHERE salary IS NOT NULL ORDER BY salary DESC, name LIMIT 3;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • NTH_VALUE(x, n) returns the n-th row's value in the frame
  • The default frame makes early rows return NULL
  • Extend the frame to the whole partition
  • Ties count as separate rows

Common mistakes

  • Expecting NTH_VALUE to skip duplicates
  • Forgetting the frame and getting NULL on top rows

Follow-ups

  • How would you get the second distinct salary?
  • Does NTH_VALUE support FROM LAST in SQLite?

Tips

  • Explain the frame before the fix