

So, using the WITH TIES option in the TOP clause, allows your result set to return any additional rows, beyond your specified limit, that has a matching value at the nth position as based on the columns specified in your ORDER BY clause.įor more details, examples, and other similar topics, visit the MSDN website where this information was obtained, or stay tuned to this blog for more details later. Excluding the WITH TIES option would make Julie rather angry with you. Or just the opposite, management whats to increase the top 10 highest paid salaries. And while I am sure Julie would thanks you, management may not. So whats the big deal? Well, if management was looking to decrease the salaries of the 10 highest paid employees in the company, and they came to you asking for who those employees were, if you excluded the ‘WITH TIES’ option, then Juile (the 11th employee in the above image) would not be included in that pay decrease because she would not have been included in the TOP 10 result set. Take a look at our results…notice that we no longer have a limit of 10 employees, we now have 11 because the 11th employee has the same rate as the 10th employee. In essence, what this query is doing is getting the TOP 10 highest paid employees, and then using the lowest rate on that TOP 10 list (which happens to be the column we are ordering our results by in our ORDER BY clause) and looking for any additional employees who make that same amount. But, did you get all of the results? Remember, the management team is looking for the TOP 10 highest paid employees, but what if there is more than one employee making the same amount? More specifically, what if there are other employees making 48.101 per hour that are not included here? Perhaps an example will explain this better.Ĭonsider the following query that uses TOP and makes use of the WITH TIES option.

Overall, and at first glance, it looks like you got the expected results, and you did. ORDER BY Rate DESC Results of a TOP 10 query without using 'WITH TIES' You might create a query that looks similar to the one below that would result in the data set below the query. Let’s say that management is looking for the top 10 highest paid employees at your company. ORDER BY does not affect this rule.”Ĭlear? Good. The returned order of tying records is arbitrary. TOP…WITH TIES can be specified only in SELECT statements, and only if an ORDER BY clause is specified. For example, if expression is set to 5 but 2 additional rows match the values of the ORDER BY columns in row 5, the result set will contain 7 rows. This may cause more rows to be returned than the value specified in expression. “Specifies that the query result set includes any additional rows that match the values in the ORDER BY column or columns in the last row returned. Accorinding to MSDN’s website, WITH TIES… Now, lets expand the TOP clause a little and use the ‘WITH TIES’ option. But, if management is looking for the job titles of the employees hired most recently, you will need to use the second query which will order the positions by the date they were hired. The first one returns the TOP 10 job titles in the HumanResources.Employee Table (Using the AdventureWorks sample Database), along with the date the people in those positions were hired. Employee GO - Select the first 10 employees hired most recently. SELECT TOP ( 10 )Title, HireDateįROM HumanResources. Take these two queries for example: - Select the first 10 random employees.

I AM LOOKING FOR THE ADVENTUREWORKSLT DATABASE CODE
This is important to realize and code for so that data returned is not only returned in a consistent fashion, but also in the correct and desired order. One very important thing to keep in mind when using TOP is that unless your query also includes an ORDER BY clause, the results of the T-SQL statement will return a randomized set of rows. The T-SQL TOP clause is a useful way to limit the number of rows returned to a data set to a specified number or percentage, but sometimes it becomes important to pay attention to the business needs when using this clause.
