With Ties Option
Test Data
Section titled “Test Data”CREATE TABLE #TEST(Id INT,Name VARCHAR(10))
Insert Into #Testselect 1,'A'Union AllSelect 1,'B'union allSelect 1,'C'union allSelect 2,'D'Below is the output of above table,As you can see Id Column is repeated three times..
Id Name1 A1 B1 C2 DNow Lets check the output using simple order by..
Select Top (1) Id,Name From#testOrder By Id ;Output :(Output of above query is not guaranteed to be same every time )
Id Name1 BLets run the Same query With Ties Option..
Select Top (1) With Ties Id,Name From#testOrder By IdOutput :
Id Name1 A1 B1 CAs you can see SQL Server outputs all the Rows which are tied with Order by Column. Lets see one more Example to understand this better..
Select Top (1) With Ties Id,Name From#testOrder By Id ,NameOutput:
Id Name1 AIn Summary ,when we use with Ties Option,SQL Server Outputs all the Tied rows irrespective of limit we impose