Meritshot Tutorials

  1. Home
  2. »
  3. SQL SELECT TOP

SQL Tutorial

SQL SELECT TOP

The SELECT TOP clause is used to limit the number of rows returned by a query. It’s particularly useful when you want to retrieve only a subset of the data, such as the first few records or the top N records based on specific criteria.

Basic Syntax

Syntax:

SELECT TOP (number) column1, column2, …

FROM table_name

ORDER BY column;

  • number: Specifies the number of rows to return.
  • column: The column by which the results are ordered.

Note: The ORDER BY clause is often used in conjunction with SELECT TOP to ensure that the rows returned are the top N rows based on a specific sorting criterion.

Example 1: Retrieve Top N Records

Get the top 3 highest-paid employees from the employees table.

Example:

Get the top 3 highest-paid employees from the employees table.

SELECT TOP (3) employee_id, first_name, last_name, salary

FROM employees

ORDER BY salary DESC;

Output:

employee_id

first_name

last_name

salary

3

Priya

Verma

70000

11

Deepak

Jain

66000

1

Rajesh

Singh

65000

 

Example 2: Retrieve Top Percentage of Records

Get the top 10% of the employees based on their salary.

Example:

SELECT TOP (10) PERCENT employee_id, first_name, last_name, salary

FROM employees

ORDER BY salary DESC;

Output:

If the table has 20 employees, this query will return the top 2 employees (10% of 20) with the highest salaries.

employee_id

first_name

last_name

salary

3

Priya

Verma

70000

11

Deepak

Jain

66000

 

Example 3: Retrieve Top N Records with a Specific Condition

Get the top 5 youngest employees from the employees table who are in the ‘IT’ department.

Example:

Get the top 5 youngest employees from the employees table who are in the ‘IT’ department.

SELECT TOP (5) employee_id, first_name, last_name, age

FROM employees

WHERE department = ‘IT’

ORDER BY age ASC;

Output:

employee_id

first_name

last_name

age

8

Manish

Patel

25

9

Ananya

Kumar

28

11

Deepak

Jain

30

2

Aarti

Mehta

31

12

Meera

Patel

32

 

Tips to Remember

  1. Specify ORDER BY: Use the ORDER BY clause with SELECT TOP to ensure the rows returned are based on the desired sorting criterion.
  2. Percentage Limitation: When using a percentage, ensure that it is appropriate for the size of your dataset. For example, 10% of 5 rows will return 0.5 rows, which will be rounded to 1 row.
  3. Combining with Other Clauses: You can combine SELECT TOP with other clauses like WHERE and JOIN to refine your results.
  4. Database Compatibility: Different SQL databases have variations in syntax. For example, in MySQL, you would use LIMIT instead of TOP.

Power of SELECT TOP

The SELECT TOP clause:

  • Limits Results: Allows you to restrict the number of rows returned, making it easier to work with large datasets.
  • Focuses on Specific Data: Helps in retrieving the most relevant data, such as top performers or recent entries.
  • Optimizes Performance: Improves query performance by reducing the amount of data processed and returned.

Frequently Asked Questions

Q1: Can I use SELECT TOP without ORDER BY?
A1: It is possible, but the results might be arbitrary. Using ORDER BY ensures that the rows returned are the top N rows based on a specific criterion.

Q2: How does SELECT TOP differ from LIMIT in MySQL?
A2: LIMIT is used in MySQL to achieve similar functionality as SELECT TOP in SQL Server. The syntax is different, but the purpose is the same.

Example in MySQL:

SELECT employee_id, first_name, last_name, salary

FROM employees

ORDER BY salary DESC

LIMIT 3;

Q3: Can I combine SELECT TOP with GROUP BY?
A3: Yes, you can combine SELECT TOP with GROUP BY and ORDER BY to retrieve the top records from each group.

Example:

SELECT TOP (3) WITH TIES department, AVG(salary) AS avg_salary

FROM employees

GROUP BY department

ORDER BY avg_salary DESC;

Q4: How does SELECT TOP work with DISTINCT?
A4: SELECT TOP can be used with DISTINCT to return a specific number of unique rows.

Example:

SELECT TOP (5) DISTINCT department

FROM employees;

Q5: Can I use SELECT TOP in subqueries?
A5: Yes, you can use SELECT TOP in subqueries to limit the number of rows processed in the outer query.

Example:

SELECT employee_id, first_name, last_name

FROM (SELECT TOP (5) employee_id, first_name, last_name

      FROM employees

      ORDER BY salary DESC) AS TopSalaries;