Python SQL SELECT DISTINCT & Row Deduplication
The `SELECT DISTINCT` clause eliminates duplicate rows from the query output, returning only unique values or combinations. `DISTINCT` operates across all projected columns simultaneously.
"If a guest list has someone's name written five times because they RSVP'd repeatedly, `SELECT DISTINCT` hands you a deduplicated list with their name appearing exactly once."
Deep Dive: How It Works
Multi-Column DISTINCT: `SELECT DISTINCT country, city FROM customers` evaluates uniqueness on the COMBINATION of country and city together.
Performance Impact: Deduplication requires sorting or building a hash set of results, adding memory and CPU overhead to large queries.
COUNT(DISTINCT col): Calculates the cardinality (number of unique non-null values) in a dataset.
Syntax Blueprint
SELECT DISTINCT column1, column2 FROM table_name;
DISTINCT evaluates across all columns listed in the SELECT clause.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Writing `SELECT DISTINCT(col1), col2` thinking DISTINCT applies only to col1.Why it happens: DISTINCT is a clause keyword modifying the entire row projection, not a function.
How to fix: To group by one column and pick one arbitrary value from another, use `GROUP BY`.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Select Unique Departments
Query distinct department names from the `staff` table.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.