sqlcmd.net validated sql reference
intermediate pagination SQL Server PostgreSQL

Include All Tied Rows When Limiting Results With WITH TIES

Add `WITH TIES` when limiting rows to also return every row that shares the same ORDER BY value as the last included row, preventing arbitrary tie-breaking at the result boundary.

Docker-validated Not currently validation-green

Return the top 3 scores but include all players tied at the boundary

Without WITH TIES, FETCH FIRST 3 ROWS ONLY (or TOP 3) would return Alice (95), Bob (90), and Carol (90) — leaving Eve off even though she also scored 90. With WITH TIES, the engine compares each candidate row's ORDER BY value against the last kept row. The third kept row is Carol at score 90. Eve also has score 90, so she ties on the ORDER BY key and is included, giving four rows total. Dave's score of 85 is lower, so he is excluded. The secondary ORDER BY player ASC ensures a deterministic order within the tied group: Bob, Carol, Eve appear alphabetically. SQL Server places WITH TIES after TOP n in the select list, while PostgreSQL appends WITH TIES to the FETCH FIRST n ROWS clause at the end of the query.

SQL Server
Engine-specific syntax
Setup
CREATE TABLE scores (player VARCHAR(20), score INT);

INSERT INTO
  scores
VALUES
  ('Alice', 95),
  ('Bob', 90),
  ('Carol', 90),
  ('Dave', 85),
  ('Eve', 90);
SQL
SELECT
  TOP 3
WITH
  TIES player,
  score
FROM
  scores
ORDER BY
  score DESC,
  player ASC;
playerscore
Alice95
Bob90
Carol90
Eve90
PostgreSQL
Engine-specific syntax
Setup
CREATE TABLE scores (player VARCHAR(20), score INT);

INSERT INTO
  scores
VALUES
  ('Alice', 95),
  ('Bob', 90),
  ('Carol', 90),
  ('Dave', 85),
  ('Eve', 90);
SQL
SELECT
  player,
  score
FROM
  scores
ORDER BY
  score DESC,
  player ASC
FETCH FIRST
  3 ROWS
WITH
  TIES;
playerscore
Alice95
Bob90
Carol90
Eve90

SQL Server uses TOP n WITH TIES in the select list. PostgreSQL uses FETCH FIRST n ROWS WITH TIES at the end of the query. MySQL, MariaDB, and SQLite have no direct equivalent.

Where this command helps.

  • returning a top-N leaderboard where tied scores must all appear rather than cutting off mid-tie
  • paginating ranked results where the boundary row must not split a group of equal values

What the command is doing.

FETCH FIRST n ROWS ONLY stops exactly at n rows, cutting ties at the boundary arbitrarily. WITH TIES extends that: if the nth row shares its ORDER BY key value with rows ranked n+1, n+2, and so on, those additional rows are also returned. The result set is never smaller than n (assuming enough rows exist), but may be larger. An ORDER BY clause is required — WITH TIES is meaningless without a defined sort order. PostgreSQL 13+ supports FETCH FIRST n ROWS WITH TIES. SQL Server achieves the same behavior with TOP n WITH TIES in the select list — SQL Server's OFFSET ... FETCH syntax does not support WITH TIES. Oracle 12c+ uses the same FETCH FIRST n ROWS WITH TIES syntax as PostgreSQL. MySQL and MariaDB do not support WITH TIES; the equivalent requires a RANK() window function in a subquery and filtering where rank is less than or equal to the desired cutoff.