🧠 How to Use the DISTINCT Keyword in SQL: Remove Duplicates with Ease

When working with databases, there are times when you need only unique values from a column or a combination of columns. This is where the DISTINCT keyword in SQL becomes incredibly helpful. Whether you’re trying to find all unique countries in a customer table or avoid duplicate entries in reports, DISTINCT is the way to go.

In this article, we’ll walk through what DISTINCT is, how it works, when to use it, and some common mistakes to avoid β€” all in a clear and simple way that’s perfect for new learners.


πŸ“˜ What is the DISTINCT Keyword in SQL?

The DISTINCT keyword is used in SQL to remove duplicate rows from the results of a SELECT query. It ensures that the data returned contains only unique rows.

By default, SQL returns all records, including duplicates. DISTINCT filters out repeated rows based on the selected columns.


🧾 Basic Syntax

SELECT DISTINCT column1, column2, ...
FROM table_name;
  • column1, column2: The columns you want to retrieve unique values from.
  • table_name: The name of the table you’re querying.

πŸ§ͺ Example Table: students

idnamecourse
1AliceMath
2BobScience
3AliceMath
4DianaHistory
5EvanMath

βœ… Example 1: Select All Courses (Including Duplicates)

SELECT course
FROM students;

Result:

course
Math
Science
Math
History
Math

πŸ” Example 2: Select Unique Courses Using DISTINCT

SELECT DISTINCT course
FROM students;

Result:

course
Math
Science
History

Here, the duplicates for β€œMath” are removed, and only unique course names are returned.


πŸ§‘β€πŸŽ“ Example 3: Select Unique Names and Courses

SELECT DISTINCT name, course
FROM students;

Result:

namecourse
AliceMath
BobScience
DianaHistory
EvanMath

Even though Alice appears twice in the table, the combination of name and course appears only once here.


πŸ’‘ Key Points to Remember

  • DISTINCT applies to the entire row based on the selected columns.
  • It works with one or more columns.
  • It removes duplicate rows, not individual values.

πŸ›  Practical Use Cases

βœ… 1. List All Unique Departments

SELECT DISTINCT department
FROM employees;

Useful in HR dashboards to display department filters.


βœ… 2. Count Unique Customers

SELECT COUNT(DISTINCT customer_id)
FROM orders;

This returns the number of unique customers who placed orders.


βœ… 3. Remove Duplicates in Reports

Imagine generating a monthly report that lists all products sold β€” using DISTINCT ensures no product appears twice.

SELECT DISTINCT product_name
FROM sales;

🧯 Common Mistakes with DISTINCT

❌ Using DISTINCT on the Wrong Columns

If you select additional columns that vary between rows, the duplicates won’t be removed as expected.

-- This won't remove duplicates if timestamp or other columns differ
SELECT DISTINCT customer_id, order_time
FROM orders;

❌ Thinking It Works Like GROUP BY

DISTINCT removes duplicates but doesn’t perform aggregation like GROUP BY. If you need totals or averages, use GROUP BY.


πŸ†š DISTINCT vs. GROUP BY

FeatureDISTINCTGROUP BY
Removes Duplicatesβœ… Yesβœ… Yes
Aggregation❌ Noβœ… Yes (e.g., COUNT, SUM)
UsageSimple uniqueness checkGrouping with functions

πŸ“ˆ Performance Considerations

DISTINCT can be resource-intensive on large datasets, especially if:

  • You’re selecting many columns.
  • The table has millions of rows.
  • There are no indexes on the columns used.

πŸ’‘ Tip: Use DISTINCT only when necessary and avoid overusing it in subqueries or complex joins.


πŸ”„ Combine DISTINCT with Other Clauses

πŸ‘‰ With WHERE Clause

SELECT DISTINCT course
FROM students
WHERE name = 'Alice';

This filters records before applying DISTINCT.


πŸ‘‰ With ORDER BY

SELECT DISTINCT course
FROM students
ORDER BY course ASC;

This sorts the distinct results alphabetically.


πŸ‘‰ With Joins

SELECT DISTINCT c.customer_name
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id;

This retrieves unique customers who have placed at least one order.


🧠 Summary Table

ClauseDescription
DISTINCTRemoves duplicate rows from the result set
Works onOne or more columns
Can combine withWHERE, ORDER BY, JOIN, COUNT()
CautionCan impact performance on large tables

βœ… Final Thoughts

The DISTINCT keyword in SQL is an incredibly useful tool when you want to remove duplicate records from your query results. Whether you’re looking to show a clean list of products, avoid repetition in reports, or simply count unique users, DISTINCT has your back.

It’s easy to use, beginner-friendly, and can be combined with other clauses like WHERE, ORDER BY, and even COUNT() to make your SQL queries more powerful and efficient.

Start experimenting with DISTINCT in your own queries today β€” your reports and dashboards will look much cleaner!