How to Use SQL DISTINCT and TOP in Same Query?
Last Updated :
23 Jul, 2025
Structured Query Language (SQL) is a computer language used to interact with relational databases. It allows us to organize, manage, and retrieve data efficiently. In this article, we will explain how to use the DISTINCT keyword and the TOP clause together in a query, explaining their purpose, usage, and providing step-by-step instructions with outputs for better clarity.
Understanding DISTINCT in SQL
The DISTINCT keyword is used to retrieve unique values from a column, eliminating duplicate rows in the result set. This keyword is only used to return distinct or we can say different values. For example, if a column contains repeated values, DISTINCT ensures that each value appears only once in the output.
Example
If a table contains the values ('John', 'Jane', 'John'), using DISTINCT will return only ('John', 'Jane').
Understanding TOP in SQL
The TOP clause is used when we want to return a specific number of records or limit the number of rows returned in the result set. It is particularly useful when we need only a subset of data. However, note that not all database systems support the TOP clause.
- In MySQL, the equivalent is
LIMIT.
- In Oracle, the equivalent is
FETCH FIRST number ROWS ONLY.
Example
If you have 100 rows and only need the first 10, we can use TOP 10 (or LIMIT 10 in MySQL).
Steps to Use SQL DISTINCT and TOP Together
In this section, we will cover how to use the DISTINCT and TOP clauses together in a SQL query to retrieve a specified number of unique records from a database table. We will walk through creating a database, inserting sample data, and applying these clauses step by step, demonstrating how to combine them for more efficient data retrieval.
Step 1: Create a Database
First, CREATE DATABASE command initializes a new database named Student. This will be the working database where our tables and data reside. We can verify its creation using a command like SHOW DATABASES in MySQL.
Query:
CREATE DATABASE Student;
Step 2: Use the Student Database
Switch to the newly created Student database. The USE statement sets the active database context, ensuring that all subsequent operations are performed within the Student database.
USE Student;
Step 3: Create and Insert Rows into the Student Table
The CREATE TABLE statement defines a table named Student with a single column name. Add some sample data into the Student table. Here, we insert names, including duplicates, to demonstrate the use of DISTINCT.
Query:
-- Create the table
CREATE TABLE Student (name VARCHAR(50));
--Insert values into the table
INSERT INTO Student VALUES ('Amit');
INSERT INTO Student VALUES ('Amit ');
INSERT INTO Student VALUES ('Aniket');
INSERT INTO Student VALUES ('Aniket');
INSERT INTO Student VALUES ('Soumya');
INSERT INTO Student VALUES ('Tridib');
Step 4: View All Rows Without Using DISTINCT
Fetch all names from the Student table to see the raw data, including duplicates.
Query:
SELECT name FROM Student ORDER BY name;
Output
| name |
|---|
| Amit |
| Amit |
| Aniket |
| Aniket |
| Soumya |
| Tridib |
Explanation:
This query retrieves all rows from the Student table, ordered alphabetically by name. Duplicate entries like 'Amit' and 'Amit ' appear because DISTINCT hasn’t been applied yet.
Step 5: View Rows Using DISTINCT
Use the DISTINCT keyword to eliminate duplicate rows.
Query:
SELECT DISTINCT name FROM Student ORDER BY name;
Output
| name |
|---|
| Amit |
| Aniket |
| Soumya |
| Tridib |
Explanation:
The DISTINCT keyword removes exact duplicates from the result set, but it considers trailing spaces as part of the value. Hence, it will give 4 names as output of the above query.
Step 6: Combine DISTINCT with TOP
Fetch the first three unique names using DISTINCT and TOP.
Query:
SELECT DISTINCT TOP 3 name FROM Student ORDER BY name;
Output
Explanation:
Here, the TOP 3 clause limits the result set to three rows after applying the DISTINCT keyword. The rows are ordered alphabetically before selection. This query demonstrates how to combine the functionalities of both clauses.
Conclusion
Using DISTINCT and TOP together in a query helps in retrieving a limited number of unique records from a dataset. While DISTINCT ensures uniqueness, TOP (or equivalent clauses like LIMIT or FETCH) restricts the number of results. Understanding how to use these clauses effectively enhances our SQL querying skills and improves data retrieval efficiency.
Explore
Basics
Queries & Operations
SQL Joins & Functions
Data Constraints & Aggregate Functions
Advanced SQL Topics
Database Design & Security