FETCH FIRST WITH TIES Includes Peers

FETCH FIRST n ROWS WITH TIES extends the result to every row tied with the cutoff, so leaderboards stop truncating equal scores that bare LIMIT silently drops.

SqlData Integrity

Code

snippet
-- Top 3, plus anyone tied with the third place
SELECT player, points
FROM scores
ORDER BY points DESC
FETCH FIRST 3 ROWS WITH TIES;

-- Contrast: a bare LIMIT cuts tied rows off arbitrarily
SELECT player, points
FROM scores
ORDER BY points DESC
LIMIT 3;

-- Skip the winner, then fetch second place along with its ties
SELECT player, points
FROM scores
ORDER BY points DESC
OFFSET 1 ROWS
FETCH NEXT 1 ROWS WITH TIES;

How the snippet works

What runs here is SELECT — scores are the objects involved.

Concretely, the SQL combines: an ORDER BY that fixes the output order; a row cap (LIMIT) bounding the result size.

Applying it

Run the statement against the scores table; the LIMIT keeps the probe cheap while you tune the predicates.

Statements that matter

SELECT player, points
FROM scores
ORDER BY points DESC
OFFSET 1 ROWS
FETCH NEXT 1 ROWS WITH TIES;
SELECT player, points
FROM scores
ORDER BY points DESC
FETCH FIRST 3 ROWS WITH TIES;
SELECT player, points
FROM scores
ORDER BY points DESC
LIMIT 3;

Interactive Tools for This Topic

FAQ

What relations does the SQL touch?
`scores`.