SELECT TOP 5 WITH TIES EmpNumber,EmpName
FROM Employee
Order By EmpNumber DESC
This above query return more than five result, What is the use of \"Wi
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
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 WITH TIES *
FROM MyTable
ORDER BY ID DESC;
returns only 5 rows, as the last row (2 in this case) exists only once.
Check this query and will be more clear.
SELECT TOP 5 WITH TIES *
FROM MyTable
ORDER BY ID;
RESULT:- 1 2 3 4 5 5
SELECT TOP 5 *
FROM MyTable
ORDER BY ID;
RESULT:- 1 2 3 4 5
Suppose we a have a table named myTable with below data:
1 Geeks 10000
4 Finch 10000
2 RR 6000
3 David 16000
5 Lesley 7000
6 Watson 10000
Query: SELECT * from myTable
order by salary desc
fetch first 3 rows only;
Output: We got only first 3 rows order by Salary in Descending Order
3 David 16000
1 Geeks 10000
4 Finch 10000
Note: In the above result we got first 3 rows, ordered by Salary in Descending Order, but we have one more row with same salary i.e, the row with name Watson and Salary 10000, but it didn’t came up, because we restricted our output to first three rows only. But this is not optimal, because most of the time in live applications we will be required to display the tied rows also.
Real Life Example – Suppose we have 10 Racers running, and we have only 3 prizes i.e, first, second, third, but suppose, Racers 3 and 4 finished the race together in same time, so in this case we have a tie between 3 and 4 and that’s why both are holder of Position 3.
The WITH TIES
allows you to return more rows with values that match the last row in the limited result set. Note that WITH TIES
may cause more rows to be returned than you specify in the expression.
The selection of which the rows to return is nondeterministic.
This means that if you run the query again, without the underlying data changing, theoretically you could get a different set of three rows.
In practice, the row selection will depend on physical conditions like :
If you actually run the query multiple times, as long as those physical conditions don’t change, there’s some likelihood you will keep getting the same results. But it is critical to understand the “physical data independence” principle from the relational model, and remember that at the logical level you do not have a guarantee for repeatable results. Without ordering specification, you should consider the order as being arbitrary, resulting in a nondeterministic row selection.
Your current result is like below :
EmpNumber EmpName Ranking
11 Maria 1
23 José 2
456 Pedro 3
456 Pedro 3 --WITH TIES
Probably your table is containing duplicate rows or may have historical ones as in general the EmpNumber is unique.
From Reference
From here
Using TOP WITH TIES to include rows that match the values in the last row
If you want to use TOP WITH TIES you must use order by.
Create Table
CREATE TABLE [dbo].[Products](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ProductName] [nvarchar](50) NULL,
[Price] [float] NULL)
GO
The following illustrates the INSERT statement that inserts rows into an existing table
INSERT INTO [dbo].[Products] VALUES ('Bicycle 1' , 258.2)
INSERT INTO [dbo].[Products] VALUES ('Bicycle 2' , 265.3)
INSERT INTO [dbo].[Products] VALUES ('Bicycle 3' , 267.8)
INSERT INTO [dbo].[Products] VALUES ('Bicycle 4' , 268.9)
INSERT INTO [dbo].[Products] VALUES ('Bicycle 5' , 267.9)
INSERT INTO [dbo].[Products] VALUES ('Bicycle 6' , 267.9)
GO
then
SELECT TOP 4 WITH TIES
ProductName, Price
FROM Products
ORDER BY Price
In this example, the two expensive product has a list price of 267.9. Because the statement used TOP WITH TIES, it returned one more products whose list prices are the same as the forth one.
With my understanding of WITH TIES clause, if you want to [duplicate values], which the column specified in the Order By.