Beginner LevelSQL & MySQL
How does SELECT DISTINCT eliminate duplicate result rows?
❓ Interview Prompt: Explain `SELECT DISTINCT`, multi-column uniqueness evaluation, performance implications, and alternatives using `GROUP BY`.
The 'SELECT DISTINCT' clause removes duplicate rows from the query result set:
Mechanics:
•
Operates across ALL projected columns in the 'SELECT' clause to evaluate unique row combinations.
•
Requires the database engine to perform a sort or hash operation on the intermediate result set to collapse matching duplicate rows.
Code Implementation Example
sql
-- Retrieve unique list of countries from customer profiles
SELECT DISTINCT country, city
FROM customers
WHERE status = 'ACTIVE'
ORDER BY country, city;💡Selecting unique country and city combinations using DISTINCT.
🎯 Interview Key Takeaway
Use SELECT DISTINCT to filter duplicate rows, but be mindful of sorting overhead on large datasets.
#SQL#DISTINCT#Deduplication#Beginner
Back to all SQL & MySQL Questions→