🔍 SQL DISTINCT – Eliminate Duplicates From Your Query Results

Want to remove duplicate values from your SQL queries?
Use the SQL DISTINCT keyword to return only unique rows in your result set.
It’s perfect for filtering out redundancy and simplifying your data.


đź§ľ What is SQL DISTINCT?

The DISTINCT keyword in SQL is used with the SELECT statement to return only unique (non-duplicate) values from one or more columns.

It helps:

  • Simplify results
  • Clean data
  • Analyze unique entries like locations, departments, or customer names

📚 SQL DISTINCT Syntax

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

âś… Example: Get Unique Job Titles

Assume a table employees with many employees having the same job title. Let’s get a list of unique job titles only:

SELECT DISTINCT job_title
FROM employees;

🟢 This query returns a list of job titles without duplicates.


đź§  How SQL DISTINCT Works

SQL compares entire rows if multiple columns are selected. A row is considered duplicate only when all selected column values are the same.


🔢 Example: DISTINCT on Multiple Columns

SELECT DISTINCT department, job_title
FROM employees;

📌 This query returns unique combinations of department and job_title.

So if “HR – Manager” and “HR – Analyst” both exist, both are kept. But multiple “HR – Manager” entries? Only one is shown.


⚠️ Important Notes

  • DISTINCT applies to all columns selected, not just the first one.
  • Sorting is not guaranteed unless you explicitly use ORDER BY.
  • Can impact performance on large datasets—use wisely.

đź§Ş Real-World Use Case

Use Case: You’re analyzing sales data and want to count how many unique customers placed orders.

SELECT COUNT(DISTINCT customer_id)
FROM orders;

This returns the number of unique customers in your orders table.


âť“ Practice Challenge

Question: How would you retrieve all unique cities where your customers live?

SELECT DISTINCT city 
FROM customers;

📝 Final Thoughts

The SQL DISTINCT keyword is a simple yet powerful tool to:

  • Clean up redundant data
  • Perform unique value analysis
  • Enhance data quality and performance

🎯 Whether you’re working with product SKUs, customer IDs, or email addresses, DISTINCT helps you focus only on what matters.