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.
Code
-- 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
SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.cURL to fetch ConverterTranslate a curl command into equivalent JavaScript fetch() code, handling the request method, headers, JSON or raw body, and basic auth.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.
FAQ
- What relations does the SQL touch?
- `scores`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.