0
0
MySQLquery~10 mins

DISTINCT for unique values in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - DISTINCT for unique values
Start Query
Scan Table Rows
Check Each Row Value
Is Value Already Seen?
YesSkip Row
No
Add Value to Result Set
More Rows?
YesCheck Each Row Value
No
Return Unique Values
The DISTINCT keyword scans each row, keeps only unique values, and skips duplicates to return a list of unique results.
Execution Sample
MySQL
SELECT DISTINCT city FROM customers;
This query returns a list of unique city names from the customers table.
Execution Table
StepRow Value (city)Already Seen?ActionResult Set
1New YorkNoAdd to result[New York]
2Los AngelesNoAdd to result[New York, Los Angeles]
3New YorkYesSkip row[New York, Los Angeles]
4ChicagoNoAdd to result[New York, Los Angeles, Chicago]
5Los AngelesYesSkip row[New York, Los Angeles, Chicago]
6HoustonNoAdd to result[New York, Los Angeles, Chicago, Houston]
7ChicagoYesSkip row[New York, Los Angeles, Chicago, Houston]
End--No more rows[New York, Los Angeles, Chicago, Houston]
💡 All rows processed; duplicates skipped; unique cities returned.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
Result Set[][New York][New York, Los Angeles][New York, Los Angeles][New York, Los Angeles, Chicago][New York, Los Angeles, Chicago][New York, Los Angeles, Chicago, Houston][New York, Los Angeles, Chicago, Houston][New York, Los Angeles, Chicago, Houston]
Key Moments - 2 Insights
Why does the query skip some rows even though they exist in the table?
Rows with city values already in the result set are skipped to avoid duplicates, as shown in steps 3, 5, and 7 in the execution_table.
Does DISTINCT change the order of the results?
DISTINCT keeps the order of first occurrences as they appear in the table scan, so the first time a city appears it is added, preserving that order.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Result Set after step 4?
A[New York, Los Angeles, Chicago]
B[New York, Los Angeles]
C[New York, Chicago]
D[Los Angeles, Chicago]
💡 Hint
Check the Result Set column at step 4 in the execution_table.
At which step does the city 'Houston' get added to the Result Set?
AStep 5
BStep 6
CStep 7
DStep 3
💡 Hint
Look for 'Houston' in the Row Value column and see the corresponding action.
If the city 'Miami' was added as a new row after step 7, what would happen?
AThe query would stop immediately.
BIt would be skipped because it's a duplicate.
CIt would be added to the Result Set.
DIt would replace an existing city in the Result Set.
💡 Hint
DISTINCT adds new unique values to the Result Set as shown in the execution_table.
Concept Snapshot
DISTINCT keyword returns unique values from a column.
Syntax: SELECT DISTINCT column FROM table;
It scans rows, adds unseen values to results, skips duplicates.
Order of first occurrences is preserved.
Useful to find unique entries without repeats.
Full Transcript
The DISTINCT keyword in SQL helps to get unique values from a column. When you run a query like SELECT DISTINCT city FROM customers, the database looks at each city in the customers table one by one. If it sees a city for the first time, it adds it to the result list. If the city has already been added before, it skips that row. This way, the final output contains only unique city names without any duplicates. The order of cities in the result matches the order they first appear in the table. This process continues until all rows are checked, then the unique list is returned.