I’ve used SELECT TOP (x) many, many times. After learning how to use SELECT, it’s something I quickly learned to use and remains something which I continue to use often.
I’ve also used TOP (x) with UPDATE and DELETE statements to limit the number of rows updated or deleted in a statement. I’ve never had to use SELECT TOP WITH TIES though.
I’ve glanced through the documentation in the past and thought – “must learn that one day” but never had the time or need to. I guess some things you learn how to use only when you have to.
So this post is dedicated to my learning experience of using SELECT TOP WITH TIES 🙂
What does SELECT TOP WITH TIES do?
Let’s take an example. Say you wanted to retrieve the first 5 rows from a table and you ordered by a specific column. So you get back 10 rows ordered by your column.
Now apply SELECT TOP (5) WITH TIES and any row which has a column value that matches the last row’s column value as defined by the ORDER BY clause will also be returned in the results. So you get more rows than what is specified in TOP (x).
Let’s do an example 🙂
[sourcecode language=’sql’]
CREATE TABLE FamousTransformers
(ID SMALLINT PRIMARY KEY IDENTITY(1,1),
FirstName VARCHAR(100) NOT NULL,
LastName VARCHAR(100) NOT NULL,
FavouriteColour VARCHAR(50) NOT NULL);
–populate with some data (6 rows to demonstrate this working)
INSERT INTO FamousTransformers(FirstName, LastName, FavouriteColour)
VALUES(‘Clark’,’Kent’,’Blue’);
INSERT INTO FamousTransformers(FirstName, LastName, FavouriteColour)
VALUES(‘Bruce’,’Wayne’,’Black’);
INSERT INTO FamousTransformers(FirstName, LastName, FavouriteColour)
VALUES(‘Peter’,’Parker’,’Red’);
INSERT INTO FamousTransformers(FirstName, LastName, FavouriteColour)
VALUES(‘Optimus’,’Prime’,’Red’);
INSERT INTO FamousTransformers(FirstName, LastName, FavouriteColour)
VALUES(‘David’,’Banner’,’Green’);
INSERT INTO FamousTransformers(FirstName, LastName, FavouriteColour)
VALUES(‘Rodimus’,’Prime’,’Orange’);
–select using normal TOP(x)
SELECT TOP (5) FirstName, LastName, FavouriteColour
FROM FamousTransformers
ORDER BY FavouriteColour
[/sourcecode]
Returns….
FirstName LastName FavouriteColour -------------------- -------------------- -------------------- Bruce Wayne Black Clark Kent Blue David Banner Green Rodimus Prime Orange Optimus Prime Red (5 row(s) affected)
[sourcecode language=’sql’]
–now execute using WITH TIES
SELECT TOP (5) WITH TIES FirstName, LastName, FavouriteColour
FROM FamousTransformers
ORDER BY FavouriteColour
[/sourcecode]
FirstName LastName FavouriteColour -------------------- -------------------- -------------------- Bruce Wayne Black Clark Kent Blue David Banner Green Rodimus Prime Orange Peter Parker Red Optimus Prime Red (6 row(s) affected)
So you can see the extra row appearing there where “Optimus Prime” is displayed because he has the same favourite colour as “Peter Parker”. The FavouriteColour column is the key here as defined in the ORDER BY statement.
Actual use cases for SELECT TOP WITH TIES
I can think of one straight off the top of my head. If you had a league table of top performing sales people and wanted to bring back the top three performers, what if two sales people were tied for 3rd and 4th place on revenue? No way to differentiate them, they should both be returned in the results and share 3rd place.
And finally….
You must use ORDER BY and TOP clauses together if you want to use WITH TIES.
Humberto says
Hi Andy,
You have really nice posts. They’re helping me to improve my career as DBA. I wanted to make an out-of-context question, what font are you using for the T-SQL code and its results? It’s really cool and would like to use it. I’ve been looking around and monospace is what I found. Thanks for sharing.
Andy Hayes says
Hey Humberto, thanks for your comments. I’m glad you are finding the posts here useful.
To be honest I don’t know what font is being used there as its produced by the formatting plugin which I use on the site. It will be somewhere in the CSS but I am not sure where! 🙂
Ali Khan says
It looks like Time New Roman