0
0
SQLquery~10 mins

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

Choose your learning style9 modes available
Concept Flow - DISTINCT for unique values
Start Query
Read Table Data
Apply DISTINCT
Remove Duplicate Rows
Return Unique Rows
End Query
The query reads data from a table, applies DISTINCT to remove duplicates, and returns only unique rows.
Execution Sample
SQL
SELECT DISTINCT city FROM customers;
This query selects unique city names from the customers table, removing duplicates.
Execution Table
StepActionInput DataResulting Data
1Read all city values from customers[New York, Boston, New York, Chicago, Boston][New York, Boston, New York, Chicago, Boston]
2Apply DISTINCT to remove duplicates[New York, Boston, New York, Chicago, Boston][New York, Boston, Chicago]
3Return unique city list[New York, Boston, Chicago][New York, Boston, Chicago]
4End query--
💡 All duplicate city names removed, only unique cities returned
Variable Tracker
VariableStartAfter Step 1After Step 2Final
city_listempty[New York, Boston, New York, Chicago, Boston][New York, Boston, Chicago][New York, Boston, Chicago]
Key Moments - 2 Insights
Why does DISTINCT remove duplicate rows but not change the order of unique values?
DISTINCT scans all rows and keeps only the first occurrence of each unique value, preserving their order as they appear in the data (see execution_table step 2).
Does DISTINCT affect all columns or just the selected columns?
DISTINCT applies to the entire row of selected columns. Here, since only 'city' is selected, duplicates are removed based on city values only (execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the city_list after step 2?
A[New York, Boston, Chicago]
B[New York, Boston, New York, Chicago, Boston]
C[Boston, Chicago]
D[Chicago]
💡 Hint
Check the 'Resulting Data' column in step 2 of execution_table
At which step does the query remove duplicate city names?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column describing when DISTINCT is applied
If the query selected two columns, city and state, how would DISTINCT behave?
ARemove duplicates based on city only
BReturn all rows without removing duplicates
CRemove duplicates based on both city and state together
DRemove duplicates based on state only
💡 Hint
DISTINCT applies to all selected columns as a combined row (see key_moments explanation)
Concept Snapshot
DISTINCT keyword removes duplicate rows from query results.
Syntax: SELECT DISTINCT column_name FROM table;
It returns only unique values of the selected columns.
Duplicates are removed based on all selected columns combined.
Useful to find unique entries in a column or set of columns.
Full Transcript
This visual execution trace shows how the SQL DISTINCT keyword works. The query reads all city values from the customers table, including duplicates. Then DISTINCT is applied to remove duplicate city names, keeping only unique cities. The final result returns the unique city list. The variable city_list changes from all cities to only unique cities after DISTINCT is applied. Key moments clarify that DISTINCT removes duplicates based on all selected columns combined and preserves the order of first occurrences. The visual quiz tests understanding of when duplicates are removed and how DISTINCT behaves with multiple columns.