SQL how-to
How to find the second highest (or Nth) value in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
Rank the values with DENSE_RANK() OVER (ORDER BY value DESC) and keep rank N: ties share a rank and no number is skipped. For the second highest only, MAX(value) below the overall MAX also works. LIMIT 1 OFFSET N-1 is only right on distinct values.
The task: Find the second highest value in a column, or more generally the Nth.
DENSE_RANK: the second heaviest weight
SELECT DISTINCT weight_kg
FROM (SELECT weight_kg,
DENSE_RANK() OVER (ORDER BY weight_kg DESC) AS r
FROM pets) ranked
WHERE r = 2;Result (PostgreSQL and SQLite, identical): 1 row
| weight_kg |
|---|
| 27.3 |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
DENSE_RANK() OVER (ORDER BY weight_kg DESC)Numbers the weights from the heaviest: 1, 2, 3…, equal weights sharing a number, with no gaps.
- 2
FROM (…) rankedWindow functions can't go in WHERE, so the ranking happens in a subquery first.
- 3
WHERE r = 2Keep second place.
- 4
SELECT DISTINCTIf several pets share that weight, show it once.
More examples
MAX below the MAX
Fine for second place; it gets awkward for third and beyond.
SELECT MAX(weight_kg) AS second_heaviest
FROM pets
WHERE weight_kg < (SELECT MAX(weight_kg) FROM pets);Result (PostgreSQL and SQLite, identical): 1 row
| second_heaviest |
|---|
| 27.3 |
Try it: PostgreSQL playground · SQLite playground
The OFFSET trap: ties
The second-youngest age should be 2, but three pets are 1 year old, so skipping one row lands on another 1.
SELECT age AS second_youngest_age
FROM pets
ORDER BY age
LIMIT 1 OFFSET 1;Result (PostgreSQL and SQLite, identical): 1 row
| second_youngest_age |
|---|
| 1 |
Try it: PostgreSQL playground · SQLite playground
OFFSET on distinct values works
SELECT DISTINCT age AS second_youngest_age
FROM pets
ORDER BY age
LIMIT 1 OFFSET 1;Result (PostgreSQL and SQLite, identical): 1 row
| second_youngest_age |
|---|
| 2 |
Try it: PostgreSQL playground · SQLite playground
Variations
The Nth value per group
Add PARTITION BY: the second heaviest pet of each species (species with a single pet have no second).
SELECT species, name, weight_kg
FROM (SELECT species, name, weight_kg,
DENSE_RANK() OVER (PARTITION BY species ORDER BY weight_kg DESC) AS r
FROM pets) ranked
WHERE r = 2
ORDER BY species;Result (PostgreSQL and SQLite, identical): 3 rows
| species | name | weight_kg |
|---|---|---|
| cat | Pumpkin | 5.2 |
| dog | Maple | 27.3 |
| rabbit | Clover | 2.1 |
Try it: PostgreSQL playground · SQLite playground
When there is no Nth value
The query returns no rows. Wrap it, SELECT (subquery) AS second, to get one row with NULL instead.
Common mistakes
RANKinstead ofDENSE_RANK: after a tie at 1, there is no rank 2.LIMIT 1 OFFSET 1on values with ties: it returns the second row, not the second value.Forgetting DESC: you get the second lowest instead.
PostgreSQL and SQLite
Everything on this page works the same in PostgreSQL and SQLite: each example above was run on both, and the results match.
Official documentation
Go deeper
- Guide: SQL interview questions: the core patterns
- Lesson: Ranking with OVER (Pro)
Make it stick in five minutes a day
“Ranking with OVER” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.