






DISTINCT Keyword
Eliminating Duplicates in SQL Queries
Next Topic ⮕SQL Operators - AND, OR, NOT, IN, BETWEEN, LIKE
Introduction
Imagine you have a student 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;
Sample Table – Students
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');
1. DISTINCT on One Column
Let’s list all the unique classes:
SELECT DISTINCT class FROM students;
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:
SELECT DISTINCT class, city FROM students;
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?
SELECT COUNT(DISTINCT city) AS unique_city_count
FROM students;
unique_city_count
-------------------
5
5. Be Careful: DISTINCT Applies to Entire Row
When using multiple columns, DISTINCT returns only rows where the entire combination is unique — not just one column.
Example:
SELECT DISTINCT city FROM students;
city
---------
Delhi
Chennai
Kochi
Pune
Hyderabad
6. Real-World Use Case
Let’s say the principal wants a list of cities to send school newsletters. You’d do:
SELECT DISTINCT city FROM students;
And print just those 5 cities — no duplicates, no redundancy.
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.
What’s Next?
Up next, we’ll explore Subqueries — writing queries inside queries for smarter, more powerful data retrieval.
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;