SQL DISTINCT Clause
The DISTINCT keyword in SQL is used to return unique values from a column or a combination of columns. It helps remove duplicates in query results, making it useful in reporting, analytics, and data cleaning.
📌 Syntax
SELECT DISTINCT column1, column2, ... FROM table_name WHERE condition;
📊 Example Table: users
id | name | age | city ---+---------+-----+--------- 1 | Alice | 22 | New York 2 | Bob | 30 | London 3 | Charlie | 18 | Sydney 4 | Diana | 27 | Toronto 5 | Ethan | 35 | Paris 6 | Frank | 28 | London 7 | Grace | 24 | Paris
🔍 Example 1: Distinct Cities
Retrieve all unique cities from the users table:
SELECT DISTINCT city FROM users;
🔍 Example 2: Distinct Names and Cities
Retrieve unique combinations of name and city:
SELECT DISTINCT name, city FROM users;
🔍 Example 3: Using DISTINCT with COUNT
Count the number of unique cities:
SELECT COUNT(DISTINCT city) AS unique_cities FROM users;
🔍 Example 4: DISTINCT with WHERE
Get unique cities where age is greater than 25:
SELECT DISTINCT city FROM users WHERE age > 25;
⚡ Best Practices
- Use
DISTINCTwhen you need only unique rows to reduce redundancy. - Combine
DISTINCTwithCOUNTto find the number of unique entries. - Avoid using DISTINCT unnecessarily on large tables as it may impact performance.
- Use
ORDER BYwith DISTINCT to sort unique results.
📝 Summary
The DISTINCT keyword is a powerful tool in SQL to eliminate duplicate data and retrieve unique values. It is often combined with aggregation functions, WHERE conditions, and ORDER BY clauses to generate clean and meaningful results from datasets. Understanding how to use DISTINCT efficiently is crucial for reporting, analytics, and data manipulation tasks.
🚀 Next Steps
Next, we will explore the BETWEEN Operator, which allows filtering rows within a specific range of values.