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;| name | salary | second |
|---|---|---|
| Asha | 120000 | NULL |
| Mei | 95000 | 95000 |
| Ravi | 95000 | 95000 |
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;| name | salary | second |
|---|---|---|
| Asha | 120000 | 95000 |
| Mei | 95000 | 95000 |
| Ravi | 95000 | 95000 |
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