SQL Server: The Awesome TOP clause WITH TIES option

The SQL Server TOP clause WITH TIES option is not a new feature, but still many of database professionals don’t know about it.

This option is available with the TOP clause since long time and this is one of the my best TSQL options.

Generally, we use the TOP clause to a SELECT TOP N number of records from a Table, but using TOP WITH TIES option we can also include the duplicate or same value of records in the result.

The TOP WITH TIES option works based on defined column in the ORDER BY clause and SELECT duplicate data for that column.
We can not use TOP WITH TIES option with ORDER BY clause.

Below is a small demonstration of TOP WITH TIES clause:

SELECT TOP records with out WITH TIES option:

SELECT TOP records using WITH TIES option using EmpName in ORDER BY:

Anvesh Patel

Leave a Reply

Be the First to Comment!

Notify of