SQL SELECT TOP Clause Online Tutorial

SQL SELECT TOP Results

Some times you just need use SQL SELECT TOP to reduce the number of rows shown in your result. This is handy for troubleshooting of for showing a summary, such as the first in a list.

Here you can use use the SQL SELECT TOP keyword show a limited number of rows from the top of your result.

For example to only show the first ten people from the Person table use the following command:

SELECT TOP 10 LastName, FirstName FROM Person.Person ORDER BY LastName
/* Answer */
SELECT   TOP 10 LastName, FirstName
FROM     Person.Person
ORDER BY LastName

You can also use SQL SELECT TOP to show the “bottom ten” results by ordering the result in descending order like so!

SELECT TOP 10 LastName, FirstName FROM Person.Person ORDER BY LastName DESC
/* Answer */
SELECT   TOP 10 LastName, FirstName
FROM     Person.Person
ORDER BY LastName

Keep in mind that “Top” mean the first rows in the result set, not those with the greatest value. That’s why we need to sort…

So far we see how to use TOP to find the top 10 items in a list. However, you can also use TOP to find the TOP percentage as well.

SELECT TOP PERCENT

To find the Top 5 percent of Employees with available vacation hours, we could write a query like so:

SELECT TOP 5 PERCENT NationalIDNumber,JobTitle, VacationHours FROM HumanResources.Employee ORDER BY VacationHours Desc
/* Answer */
SELECT TOP 5 PERCENT NationalIDNumber,JobTitle, VacationHours
FROM HumanResources.Employee
ORDER BY VacationHours Desc

What query would you write to get the to 10 employees with remaining vacation hours?

/* Write your Answer here! */
/* Answer */
SELECT TOP 10 NationalIDNumber,JobTitle, VacationHours
FROM HumanResources.Employee
ORDER BY VacationHours Desc

Additional SELECT TOP Resources

If you’re looking to learn more about the SQL TOP clause, then I would recommend you read our article Top Clause In SQL.

To learn more about functions, such as UPPER, check out our article Introduction to Common String Functions.

Looking for a super SQL book? Check out SQL Queries for Mere Mortals.

>