Day 32: Using DISTINCT in MySQL
Sarasa Jyothsna Kamireddi
Aspiring Python Developer | Machine Learning Enthusiast | Experienced in Reliability Engineering
Today, let us explore the DISTINCT keyword in MySQL, which helps in retrieving unique values from a column. It's a simple but powerful way to remove duplicates from query results!
?? Practical Examples:
1. Retrieving Uniques Cities from users table
SELECT DISTINCT city FROM users;
2. Finding Unique Job Titles in an Organization
SELECT DISTINCT job_title FROM employees;
3. Selecting Unique Combinations of Two Columns
SELECT DISTINCT department, job_title FROM employees;
4. Counting Unique Values Using DISTINCT with COUNT
SELECT COUNT(DISTINCT city) AS unique_cities FROM users;
? DISTINCT applies to the entire row – If multiple columns are selected, it filters unique combinations of those columns.
? DISTINCT is different from GROUP BY – GROUP BY is used with aggregation functions, while DISTINCT simply removes duplicates.
? Can affect query performance – Using DISTINCT on large datasets may be slower, so use it wisely!
Do you use DISTINCT in your SQL queries? Let’s discuss!
#100DaysOfCode #MySQL #SQL #Database #DataAnalysis #Learning #Python #BackendDevelopment