Limit with ties in PostgreSQL
PostgreSQL 13 introduced a new option for
LIMIT clause (specifically for
FETCH FIRST version) that includes additional rows in the result set if their rank is equal to the last row.
One of the following commands could be used to get the first two ones with the highest salary:
SELECT * FROM employees ORDER BY salary DESC LIMIT 2;
SELECT * FROM employees ORDER BY salary DESC FETCH FIRST 2 ROWS ONLY;
Unfortunately, they will not include all employees with salary of
To include all of them,
WITH TIES clause needs to be used:
SELECT * FROM employees ORDER BY salary DESC FETCH FIRST 2 ROWS WITH TIES;
Full example can be found on dbfidle.