SQL DISTINCT Keyword - Eliminating Duplicates in Queries
SQL DISTINCT
Imagine you have a students table, and you're asked: "How many different classes are there?" Simply selecting the class
column will list all entries — including duplicates. This is where DISTINCT
shines. It helps you remove duplicates and see only unique values.
Syntax of DISTINCT
SELECT DISTINCT column1, column2, ...
FROM table_name;
The DISTINCT
keyword in SQL is used to remove duplicate rows from the result set. When you apply SELECT DISTINCT
to one or more columns, it ensures that only unique combinations of those column values appear in the final output.
For example, in the syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name;
SQL returns only one instance of each unique combination of column1
, column2
, etc., from table_name
. If there are multiple rows with the same values in all the specified columns, only one of those rows will appear in the result.
Sample Table – students - For Examples
Let’s work with this school dataset:
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 'Delhi'),
(2, 'Diya Iyer', '9B', 'Chennai'),
(3, 'Rohit Menon', '10A', 'Kochi'),
(4, 'Sneha Patil', '8C', 'Pune'),
(5, 'Mehul Agarwal', '9B', 'Delhi'),
(6, 'Aisha Khan', '8C', 'Hyderabad');
roll_no | name | class | city |
---|---|---|---|
1 | Aarav Sharma | 10A | Delhi |
2 | Diya Iyer | 9B | Chennai |
3 | Rohit Menon | 10A | Kochi |
4 | Sneha Patil | 8C | Pune |
5 | Mehul Agarwal | 9B | Delhi |
6 | Aisha Khan | 8C | Hyderabad |
1. DISTINCT on One Column
Let’s list all the unique classes:
Use case: A school administrator wants to identify all the unique classes available in the school to create a summary of class sections.
roll_no | name | class | city |
---|---|---|---|
1 | Aarav Sharma | 10A | Delhi |
2 | Diya Iyer | 9B | Chennai |
3 | Rohit Menon | 10A | Kochi |
4 | Sneha Patil | 8C | Pune |
5 | Mehul Agarwal | 9B | Delhi |
6 | Aisha Khan | 8C | Hyderabad |
SQL query description: SQL query to select the distinct values of the class
column from the students
table, eliminating any duplicate entries.
SELECT DISTINCT class
FROM students;
Result description: Result contains a list of unique class names that exist in the students
table.
class |
---|
10A |
9B |
8C |
2. Without DISTINCT
If we don’t use DISTINCT:
SELECT class
FROM students;
class |
---|
10A |
9B |
10A |
8C |
9B |
8C |
You can see how duplicate values clutter the output — DISTINCT solves that instantly.
3. DISTINCT on Multiple Columns
You can also find unique combinations — like each unique (class, city) pair:
Use case: A school administrator wants to identify all unique combinations of classes and cities represented in the student database for planning region-wise class distribution.
SQL query description: SQL query to select distinct pairs of class
and city
from the students
table, eliminating duplicate combinations.
SELECT DISTINCT class, city
FROM students;
Result description: Result contains a list of unique class and city combinations present in the student records.
class | city |
---|---|
10A | Delhi |
9B | Chennai |
10A | Kochi |
8C | Pune |
9B | Delhi |
8C | Hyderabad |
4. Using COUNT with DISTINCT
How many unique cities are students from?
Use case: A school administrator wants to find out how many unique cities students come from to analyze geographical diversity.
roll_no | name | class | city |
---|---|---|---|
1 | Aarav Sharma | 10A | Delhi |
2 | Diya Iyer | 9B | Chennai |
3 | Rohit Menon | 10A | Kochi |
4 | Sneha Patil | 8C | Pune |
5 | Mehul Agarwal | 9B | Delhi |
6 | Aisha Khan | 8C | Hyderabad |
SQL query description: SQL query to count the number of distinct values in the city
column from the students
table, and label the result as unique_city_count
.
SELECT COUNT(DISTINCT city) AS unique_city_count
FROM students;
Result description: Result contains a single number representing the total count of unique cities that students in the database belong to.
unique_city_count |
---|
5 |
Best Practices
- Use DISTINCT only when needed — it may slow down performance on large datasets.
- Understand column combinations — DISTINCT class and city is not the same as DISTINCT city alone.
- Prefer GROUP BY if you're also using aggregate functions (we’ll explore this in advanced topics).
Summary
The DISTINCT
keyword helps you cut through the noise — showing only what's different. Whether you're counting unique classes, listing hometowns, or generating reports with uniqueness in mind, DISTINCT keeps your data clean and purposeful.
QUIZ
Question 1:What is the main purpose of the DISTINCT keyword in SQL?
Question 2:The DISTINCT keyword affects only the first column in the SELECT statement.
Question 3:Which of the following SQL queries correctly use the DISTINCT keyword?
Question 4:What does this SQL query return?
SELECT DISTINCT class FROM students;
SELECT DISTINCT class FROM students;
Comments
Loading comments...