DISTINCT in SQL
DISTINCT removes duplicate rows from a query's output. It operates on the complete set of output columns, treating two rows as duplicates only when every column value matches.
Before this SELECT and Column Expressions, FROM and Table References
Builds toward DISTINCT ON
DISTINCT removes duplicate rows from a query's output.
SQL returns every matching row by default, duplicates included. If 1,000 customers all live in the US and you select country, you get 1,000 rows all saying "US." That's not useful when you want a list of unique values.
DISTINCT collapses duplicates so each distinct value appears exactly once. It answers a question analysts ask constantly: what unique values actually exist in this column?
You're exploring a table you haven't worked with before. You want to know what statuses exist in the orders table, what countries are represented in the customers table, what product categories are in the catalog. DISTINCT turns a column full of repeated values into a clean enumeration:
SELECT DISTINCT country
FROM customersTen thousand customers, 40 countries: you get 40 rows back. One per unique value.
DISTINCT operates on the entire row, not just one column. When you select multiple columns, it returns one row per unique combination:
A country where some customers are active and others aren't shows up twice — once for the active combination, once for the inactive. Those two are different rows, so both survive deduplication.
The one thing that trips people up: adding a column to the SELECT list almost always increases the row count.
SELECT DISTINCT country returns 40 rows. SELECT DISTINCT country, city returns many more — one row per unique country/city pair. The deduplication scope widens every time you add a column. DISTINCT doesn't deduplicate on one column while ignoring the others. It deduplicates on everything you selected.
You run SELECT DISTINCT country FROM customers and get 40 rows. Then you change it to SELECT DISTINCT country, city. How many rows do you expect?
9 DISTINCT practice problems
Write a query to return each country represented in the customer base, with no duplicates.
Write a query to return each status value that appears in the orders table, with no duplicates.
Write a query to return each office location represented in the departments data, with no duplicates.
Write a query to return each customer ID that appears in the orders data, with no duplicates.
Write a query to return each unique job title held by an employee, with no duplicates.
Write a query to return each unique pairing of country and active status from the customer base.
Write a query to return each unique pairing of plan and country from the users table.
Write a query to return one row per unique city value.
Write a query to return each unique stock-value level, with no duplicates.
These problems are part of the DISTINCT lesson in SQLMaxx, with instant grading and a worked solution on each.
Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.
That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.
Real problems. Not toy examples.
615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.
Write a query. Know if it's right in one second.
No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.
Stop guessing whether you actually know it.
SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.
Learn it once. Keep it for good.
Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.
Practice, feedback, mastery, review. That's the loop that turns reading into real skill.
Start freeNo account, no credit card. Start solving in under a minute.