What is the use of WITH TIES keyword in SELECT statement in SQL Queries?

From TOP (Transact-SQL)

Used when you want to return two or more rows that tie for last place
in the limited results set.

Note the example

SQL Fiddle DEMO

We have a table with 6 entires 1 to 4 and 5 twice.

Running

SELECT TOP 5 WITH TIES *
FROM MyTable 
ORDER BY ID;

returns 6 rows, as the last row is tied (exists more than once.)

Where as

SELECT TOP 5 *
FROM MyTable 
ORDER BY ID;

returns only 5 rows, as the last row (2 in this case) exists only once.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)