๐Ÿ“„ SQL Basics โ€“ Core Queries & Clauses
Estimated reading: 3 minutes 47 views

๐Ÿ” 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 DISTINCT to 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 DISTINCT for meaningful de-duplicationUse it blindly without understanding data
Use COUNT(DISTINCT col) for statisticsApply DISTINCT on calculated columns without testing
Sort with ORDER BY for readabilityForgetting 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:

  • DISTINCT filters 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ” SQL SELECT DISTINCT

Or Copy Link

CONTENTS
Scroll to Top