SQL Tutorial

SQL SELECT DISTINCT Statement – Remove Duplicate Rows with Examples (2026-27)

SQL SELECT DISTINCT statement is used to return only unique (different) values from a specified column or set of columns in a table. When you query a table, the result might include duplicate rows — DISTINCT filters them out so each row returned is unique. In this beginner-friendly guide, you will learn the SQL DISTINCT syntax with real examples — including how to use COUNT DISTINCT, DISTINCT with multiple columns, and how it compares to GROUP BY.



✅ Why Use DISTINCT?

✅ When you query a table, the result might include duplicate rows.

✅ Using DISTINCT filters out duplicate rows so each row returned is unique.

💡 Real-world example: If your Employees table has 50 employees across 4 departments, SELECT Department FROM Employees returns 50 rows. SELECT DISTINCT Department FROM Employees returns just 4 unique department names.

✅ Basic Syntax

SELECT DISTINCT column1, column2, ...
FROM table_name;

SELECT:- Keyword to specify the columns you want unique values from.

DISTINCT:- Filters out duplicate rows from the result set.

FROM:- Specifies the table to retrieve data from.

💡 Key Point: DISTINCT is a read-only operation — it does not change your data, it only changes what is returned to you.

Sample Employees Table:-

Employees
EmployeeIDNameDepartment
1PramodIT
2PoojaHR
3RajeshIT
4RupeshSales
5AnitaHR
6SureshProduction

✅ Example – SELECT DISTINCT Department

SELECT Query:-

SELECT DISTINCT Department
FROM Employees;

Result:-

Result
Department
IT
HR
Sales
Production

Even though IT and HR appeared multiple times in the table, DISTINCT returns each department only once.


✅ Example – SELECT DISTINCT Employee Names

SELECT Query:-

SELECT DISTINCT Name FROM Employees;

Result:-

Result
Name
Pramod
Pooja
Rajesh
Rupesh
Anita
Suresh

✅ Example – DISTINCT with Multiple Columns

When DISTINCT is used with multiple columns, it returns unique combinations of all those columns together.

SELECT Query:-

SELECT DISTINCT Name, Department FROM Employees;

Result:-

Result
NameDepartment
PramodIT
PoojaHR
RajeshIT
RupeshSales
AnitaHR
SureshProduction
ℹ️ Note: Each Name+Department combination is unique here, so all rows appear. DISTINCT only removes rows where ALL selected column values are identical.

✅ Example – COUNT DISTINCT

You can combine COUNT() with DISTINCT to count the number of unique values in a column.

SELECT Query:-

SELECT COUNT(DISTINCT Department) AS UniqueDepartments
FROM Employees;

Result:-

Result
UniqueDepartments
4
💡 Tip: COUNT(DISTINCT column) is very useful in reporting queries — for example, finding how many unique customers placed orders, or how many unique products were sold.

✅ DISTINCT vs GROUP BY

Both can return unique values, but they serve different purposes:

FeatureSELECT DISTINCTGROUP BY
PurposeRemove duplicate rowsGroup rows for aggregation
Works with aggregates?No (use COUNT DISTINCT instead)Yes (COUNT, SUM, AVG, etc.)
PerformanceFaster for simple deduplicationBetter for aggregated reports
Use caseList unique departmentsCount employees per department

✅ Key Points

✅ DISTINCT applies to all columns listed after SELECT.

✅ It returns unique combinations of values across those columns.

✅ NULL values are treated as equal — multiple NULLs are reduced to one in the result.

✅ DISTINCT can slow down queries on large tables — ensure your columns are indexed.


✅ Common SQL SELECT DISTINCT Mistakes to Avoid

❌ Mistake 1: Expecting DISTINCT to apply to only one column
SELECT DISTINCT Name, Department FROM Employees; returns unique Name+Department combinations, not just unique Names. If you want only unique Names, list only that column.
❌ Mistake 2: Using DISTINCT with ORDER BY on a non-selected column
Most databases do not allow ordering by a column that is not in the SELECT list when using DISTINCT. Always include ordered columns in your SELECT.
❌ Mistake 3: Overusing DISTINCT to fix bad data
If your data has too many duplicates, DISTINCT is a band-aid. The real fix is cleaning the data or fixing the logic that creates duplicates in the first place.
⚠️ Performance tip: SELECT DISTINCT on large tables without an index forces a full table scan. Always check query execution plans and add indexes on DISTINCT columns where needed.

✅ Frequently Asked Questions (FAQ)

What does SQL SELECT DISTINCT do?
SQL SELECT DISTINCT removes duplicate rows from the result set and returns only unique values from one or more columns. For example, if 10 employees are in the IT department, SELECT DISTINCT Department returns IT only once.
When should I use SELECT DISTINCT?
Use SELECT DISTINCT when you want to retrieve only unique records — for example, listing all unique cities your customers are from, or all unique product categories in your orders table.
Is SELECT DISTINCT slow?
SELECT DISTINCT can be slower on large datasets because the database must compare and filter duplicate rows. Performance improves significantly with proper indexing on the queried columns.
Can I use SELECT DISTINCT with multiple columns?
Yes. When used with multiple columns, DISTINCT returns unique combinations of all those columns together. For example, SELECT DISTINCT Name, Department returns each unique Name + Department pair — not just unique Names.
What is the difference between SELECT DISTINCT and GROUP BY?
Both can return unique values, but GROUP BY is used with aggregate functions like COUNT(), SUM(), and AVG(). Use DISTINCT for simple deduplication, and GROUP BY when you need aggregated results per group.
How do I count unique values in SQL?
Use COUNT with DISTINCT: SELECT COUNT(DISTINCT Department) FROM Employees; — this returns the number of unique departments, not the total number of rows.

✍️ About the Author: Pramod Behera

Pramod Behera is a SAP and SQL educator with 10+ years of experience in enterprise software and database systems. He founded LearnToSAP.com to help beginners learn SAP and SQL concepts through clear, practical, real-world examples. His tutorials have helped thousands of students and IT professionals across India and beyond.