๐ SQL SELECT DISTINCT โ Eliminate Duplicate Records
๐งฒ Introduction โ Why Use SELECT DISTINCT?
In many real-world databases, duplicate values are common in columns like department, country, or status. The SELECT DISTINCT statement helps you eliminate those duplicates, allowing you to see only the unique values.
This is especially useful in:
- ๐ Reporting and summaries
- ๐ Filtering unique categories or tags
- ๐ Optimizing groupings in queries
๐ฏ In this guide, you’ll learn how to:
- Use SELECT DISTINCTto return unique values
- Apply it to one or more columns
- Combine it with WHERE,ORDER BY, and functions
โ 1. Basic Syntax for SELECT DISTINCT
SELECT DISTINCT column_name
FROM table_name;
โ Retrieves unique values from a single column.
๐ 2. Example: Unique Departments
SELECT DISTINCT department
FROM employees;
โ Returns a list of all unique department names.
๐ 3. DISTINCT on Multiple Columns
SELECT DISTINCT department, role
FROM employees;
โ Returns unique combinations of department and role.
๐ 4. Using DISTINCT with WHERE Clause
SELECT DISTINCT city
FROM customers
WHERE country = 'USA';
โ Filters before applying DISTINCT.
๐ 5. Using DISTINCT with ORDER BY
SELECT DISTINCT department
FROM employees
ORDER BY department ASC;
โ You can sort the results of DISTINCT queries.
๐งฎ 6. Using DISTINCT with Functions (Not Allowed)
SELECT DISTINCT COUNT(department) FROM employees;
โ ๏ธ Invalid. Instead, use:
SELECT COUNT(DISTINCT department) FROM employees;
โ
 Use COUNT(DISTINCT column) to count unique values.
๐ Best Practices
| โ Do This | โ Avoid This | 
|---|---|
| Use DISTINCTfor meaningful de-duplication | Use it blindly without understanding data | 
| Use COUNT(DISTINCT col)for statistics | Apply DISTINCTon calculated columns without testing | 
| Sort with ORDER BYfor readability | Forgetting filters before DISTINCT | 
๐ Summary โ Recap & Next Steps
The SELECT DISTINCT clause helps you retrieve only unique rows from your result set. It’s an efficient way to eliminate duplicates and focus on non-redundant data.
๐ Key Takeaways:
- DISTINCTfilters out duplicates in query results
- Works on one or more columns
- Can be combined with WHERE,ORDER BY, and aggregation
- For counting unique values, use COUNT(DISTINCT column)
โ๏ธ Real-World Relevance:
Use SELECT DISTINCT in dashboards, filters, reports, and any app that needs a clean list of values.
โก๏ธ Up next: Learn how to filter results with SQL WHERE Clause.
โ FAQ โ SQL SELECT DISTINCT
โ What does SELECT DISTINCT do?
โ It returns only unique values from one or more columns.
โ Can I use DISTINCT on multiple columns?
โ Yes, it returns unique combinations of those columns.
โ Can I use DISTINCT with COUNT()?
โ
 Yes, use COUNT(DISTINCT column_name) to count unique entries.
โ Is DISTINCT case-sensitive?
โ
 It depends on the database collation. For case-sensitive collations, John and john are different.
โ Is SELECT DISTINCT slower?
โ It may take more time on large datasets since it removes duplicates, often requiring sorting or hashing.
Share Now :
