Click here to Skip to main content
16,004,574 members
Articles / Programming Languages / SQL

TOP Clause in SQL

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
30 Dec 2017MIT3 min read 5.1K  
How to use the TOP clause in SQL

In this video, we’ll show you how to use the TOP clause to return a portion of the result. This comes in handy in situations you’re required to return the top or bottom ten entries of a list.

This video is an excerpt for our SQL 101 video class. You can learn more about SQL 101 here. I would encourage you to look into the class. It is a great way to get started learning SQL.

TOP Clause – Video Transcript

Below is the transcript of the video. It has been cleaned up a bit, to make it easier to read.

In many cases, you’ll use the order by to sort your entire result set, but then maybe only want to see the top portion of the result.

To do that, you can use what’s called the TOP clause. Here, you can see I’ve selected the first and last name from the person table, and what I’d like to do is order by last name and, let’s say, select the top five people. To do that, I can use the TOP clause. I’d say select TOP 5 FirstName and LastName from Person table.

SQL
SELECT   TOP 5 FirstName, LastName
FROM     Person.Person
ORDER BY LastName

Now, this TOP 5 will affect the entire result. I’m not just saying to select the top five first names, it’s selecting the top five results from the query, where I’m selecting first name and last name from the person table where the results are ordered by last name. When I run this, you’ll see that I just get five rows, and it was the top five records from the result.

To clarify, the order is like this:

  • First run the query to get all the results…
  • Now apply the TOP clause to return just a portion…

Likewise, if I wanted to look at them, I’ll call it the bottom five rows, the way to do that would be to sort the result set in descending order, and then take the top five rows from that result.

If I do that, you’ll see where … Let me first get rid of the TOP, so it’s descending. Here we have Zillig and Zimmerman as our results. If I take the top five here and execute, you’ll see that I’m getting essentially the top five folks that were at the bottom. It’s really the bottom five results of the table.

It may sound confusing, I’m using top when I’m using when I’m talking about bottom, but the idea here is I essentially sorting the result in descending order and them skimming the top off that which in English would be the bottom of the result set.

TOP PERCENT

I could also get the top 5%, so if I wanted to not just get the top five people but the top percentage, I could type in here TOP 5 PERCENT and this will get back more than five rows, it’s going to bring back 5% of the total result set.

When I execute this, you’ll see I’m now getting back 999 rows.

This result set was very large, and this is just another way of pulling back a relative number of rows.

Maybe you are doing an audit and you need to bring back the top 5% or top 10% of rows. Again, you can type in any number you wish here, so here’s top 10% rows. I think if I go above 100, it’ll error. It will, because percentages need to between zero and one. If I take, say, TOP 0 PERCENT, I get zero rows.

This article was originally posted at https://www.essentialsql.com/top-clause-sql

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Easy Computer Academy, LLC
United States United States
Hello my name is Kris. I’m here because I am passionate about helping non-techie people to overcome their fear of learning SQL.

I know what it is like to not know where to start or whether the time spent learning is worth the effort. That is why I am here to help you to:
- Get started in an easy to follow step-by-step manner.
- Use your time wisely so you focus on what is important to learn to get the most value from your time.
- Answer your questions. Really! Just post a comment and I’ll respond. I’m here to help.

It wasn’t long ago that I was helping a colleague with some reporting. She didn’t know where to start and soon got overwhelmed and lost as she didn’t know SQL.

I felt really bad, as she was under pressure to get some summary information to her boss, the built-in reports were falling short, and to make them better would require her to know SQL. At that time that seemed impossible! It in dawned on me, it doesn’t have to be that way.

Then I discovered a way for anyone with the desire to easily learn SQL. I worked with my co-worker, started to teach her what I learned and soon she was able to write reports and answer her boss’ questions without getting stressed or ploughing hours into manipulating data in Excel.

It hasn’t always been easy. Sometimes the information seems abstract or too conceptual. In this case I’ve found out that a visual explanation is best. I really like to use diagrams or videos to explain hard-to-grasp ideas.

Having video, pictures, and text really help to reinforce the point and enable learning.

And now I want to help you get the same results.

The first step is simple, click here http://www.essentialsql.com/get-started-with-sql-server/

Comments and Discussions

 
-- There are no messages in this forum --