SELECT DISTINCT in SQL Explained with Examples – Remove Duplicate Rows
SELECT DISTINCT in SQL is a keyword that removes duplicate rows from your query results, returning only unique values. Whether you need to identify unique customers, list distinct product categories, or understand how many different values exist in a column, SELECT DISTINCT is one of the first SQL tools you will reach for. This tutorial covers the complete SQL DISTINCT syntax, real-world examples, COUNT DISTINCT, the key difference between DISTINCT vs GROUP BY, NULL handling, performance tips, common mistakes, and the most common SQL interview questions on DISTINCT.
SQL SELECT DISTINCT Syntax
The basic syntax for SELECT DISTINCT in SQL is:
SELECT DISTINCT column_name
FROM table_name;
To use SELECT DISTINCT with multiple columns:
SELECT DISTINCT column1, column2
FROM table_name;
DISTINCT always appears immediately after SELECT. It applies to all selected columns — not just the first one.
What is SELECT DISTINCT in SQL?
SELECT DISTINCT removes duplicate rows from the result set, returning only unique combinations of the selected columns. Without DISTINCT, all matching rows including duplicates are returned. For example, if a students table has 300 rows but only 3 unique classes (8, 9, 10), SELECT DISTINCT class returns only 3 rows.
Real-Life Examples of SQL DISTINCT
Find all unique classes in a school
SELECT DISTINCT class FROM students;
Find all payment methods used in orders
SELECT DISTINCT payment_method FROM orders;
SELECT DISTINCT with Multiple Columns
When DISTINCT is used with multiple columns, SQL treats each unique combination as one row. Two rows are only considered duplicates if every selected column has the same value.
SELECT DISTINCT class, section FROM students;
Result: (10, A), (10, B), (9, A) are all treated as separate unique rows — even though class 10 appears multiple times.
COUNT DISTINCT in SQL
COUNT(DISTINCT column) counts the number of unique non-NULL values in a column. This is widely used in analytics dashboards and reporting.
SELECT COUNT(DISTINCT country) FROM customers;
This returns the number of unique countries — not the total number of customers. NULLs are ignored by COUNT(DISTINCT).
SELECT DISTINCT vs GROUP BY
Both DISTINCT and GROUP BY can remove duplicates from query results, but they serve different purposes:
- SELECT DISTINCT: Use when you only need a list of unique values — no aggregation needed.
- GROUP BY: Use when you need to aggregate data per group (COUNT, SUM, AVG, MAX, MIN).
-- DISTINCT: just unique values
SELECT DISTINCT country FROM customers;
-- GROUP BY: unique values + aggregation
SELECT country, COUNT(*) AS total
FROM customers GROUP BY country;
Is SELECT DISTINCT Slow?
DISTINCT is not inherently slow, but it does add overhead. The database must compare all rows to remove duplicates — typically via sorting or hashing. On large tables without indexes, this can be significant. Performance tips:
- Add an index on the column used with DISTINCT
- Filter rows with WHERE before applying DISTINCT
- Select only necessary columns — avoid SELECT DISTINCT *
- Consider GROUP BY on an indexed column as an alternative
Does DISTINCT Remove NULL Values?
DISTINCT does not remove NULLs — it keeps exactly one NULL. Multiple NULLs in a column are treated as duplicates of each other, so only one NULL appears in the result. To exclude NULLs entirely, add WHERE column IS NOT NULL.
SELECT DISTINCT city FROM customers WHERE city IS NOT NULL;
Common Mistakes with SELECT DISTINCT
- DISTINCT applies to all selected columns — not just the first one.
- DISTINCT does not modify the table — it only affects the query result.
- Avoid DISTINCT on primary key columns — they are already unique, so DISTINCT adds unnecessary overhead.
- DISTINCT filters output, not input — use WHERE to filter rows before deduplication.
SQL DISTINCT Interview Questions
- What does SELECT DISTINCT do in SQL?
- It removes duplicate rows from query results, returning only unique combinations of the selected columns.
- What is the difference between DISTINCT and GROUP BY?
- DISTINCT returns unique values without aggregation. GROUP BY groups rows so you can apply aggregate functions. When you need counts or sums per unique value, use GROUP BY.
- How does DISTINCT handle NULL values?
- Multiple NULLs are treated as duplicates — DISTINCT keeps exactly one NULL. To exclude NULLs, add WHERE column IS NOT NULL.
- How do you count unique values in SQL?
- Use COUNT(DISTINCT column_name). For example: SELECT COUNT(DISTINCT country) FROM customers;
- Can DISTINCT improve performance?
- No — DISTINCT adds overhead. It requires sorting or hashing all rows. Use indexes and WHERE filters to optimize queries that use DISTINCT.