The SQL Server TOP Expression returns only the first set of rows that is either a number or a percent of rows. If you want additional rows to be returned from the base result set if duplicate values exists in the qualified set of results, then use TOP..WITH TIES as shown in the example below:
USE Northwind
GO
-- TOP vs TOP WITH TIES
-- By SqlServerCurry.com
-- Using TOP Expression
SELECT TOP 3
OrderID, UnitPrice, Quantity
FROM [Order Details]
ORDER BY Quantity DESC
-- Using TOP WITH TIES
SELECT TOP 3 WITH TIES
OrderID, UnitPrice, Quantity
FROM [Order Details]
ORDER BY Quantity DESC
Note: TOP..WITH TIES expression is available in SQL Server 2005 and later versions and can be specified only in SELECT statements and only if an ORDER BY clause is specified.
2 comments:
this is a useful sql tip
Nice tip and really useful
Post a Comment