SQL Select Top

      No Comments on SQL Select Top

When working with large tables it is probably a good idea to return only a few records as returning a large number of records can impact the performance of the database system.

With SELECT TOP clause we can specify the number of records to return.

Note: Not all database systems support the SELECT TOP clause. Examples below only relate to SQL Server database environments.

Sample Data

We will work with Customers table for this tutorial. If you haven’t created the customers yet, the DDL to create the table as well as Insert statements for sample data can be found on Tables and Data post.

When run SELECT * from Customers query, it returns all rows of the table:

Results:

Example 1 – Select Top 5 Rows

The query below will return all the columns of the table but only top 5 rows. The “*” keyword let’s the query know that you want to return all columns :

Results:

Example 2 – TOP 5 Rows for Selected Columns

What if you wanted to only see selected columns in the result set – instead of all the columns in a table. Well in that case you just list the columns in your query:

Results:

Leave a Reply

Your email address will not be published. Required fields are marked *