Bird
Raised Fist0
MySQLquery~10 mins

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

Choose your learning style10 modes available
Concept Flow - DISTINCT for unique values
Start Query
↓
Scan Table Rows
↓
Check Each Row Value
↓
Is Value Already Seen?
Yes→Skip Row
No↓
Add Value to Result Set
↓
More Rows?
Yes→Check 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.

Practice

(1/5)
1. What does the DISTINCT keyword do in a MySQL query?
easy
A. It sorts the results in ascending order.
B. It updates values in a column.
C. It counts the number of rows in a table.
D. It returns only unique values by removing duplicates.

Solution

  1. Step 1: Understand the purpose of DISTINCT

    The DISTINCT keyword is used to filter out duplicate rows in the result set.
  2. Step 2: Compare with other options

    Sorting is done by ORDER BY, counting by COUNT(), and updating by UPDATE statement, so these are incorrect.
  3. Final Answer:

    It returns only unique values by removing duplicates. -> Option D
  4. Quick Check:

    DISTINCT = unique values [OK]
Hint: DISTINCT removes duplicates, giving unique results only [OK]
Common Mistakes:
  • Confusing DISTINCT with ORDER BY
  • Thinking DISTINCT counts rows
  • Using DISTINCT to update data
2. Which of the following is the correct syntax to select unique city names from a table called customers?
easy
A. SELECT DISTINCT city FROM customers;
B. SELECT city UNIQUE FROM customers;
C. SELECT UNIQUE city FROM customers;
D. SELECT city FROM customers DISTINCT;

Solution

  1. Step 1: Recall correct syntax for unique selection

    MySQL uses DISTINCT keyword immediately after SELECT to get unique values.
  2. Step 2: Check each option

    SELECT DISTINCT city FROM customers; uses correct syntax: SELECT DISTINCT city FROM customers; others are invalid syntax.
  3. Final Answer:

    SELECT DISTINCT city FROM customers; -> Option A
  4. Quick Check:

    Syntax for unique values = SELECT DISTINCT [OK]
Hint: Use SELECT DISTINCT column FROM table for unique values [OK]
Common Mistakes:
  • Using UNIQUE instead of DISTINCT in SELECT
  • Placing DISTINCT after FROM clause
  • Misplacing keywords causing syntax errors
3. Given the table orders with a column product_id containing values: [1, 2, 2, 3, 1, 4], what will the query SELECT DISTINCT product_id FROM orders; return?
medium
A. [1, 2, 3, 4]
B. [1, 2, 2, 3, 1, 4]
C. [2, 3, 4]
D. [1, 3, 4]

Solution

  1. Step 1: Identify duplicates in product_id

    The values are 1, 2, 2, 3, 1, 4. Duplicates are 1 and 2 repeated.
  2. Step 2: Apply DISTINCT to remove duplicates

    Removing duplicates leaves unique values: 1, 2, 3, 4.
  3. Final Answer:

    [1, 2, 3, 4] -> Option A
  4. Quick Check:

    DISTINCT removes duplicates = unique list [OK]
Hint: DISTINCT returns only one of each repeated value [OK]
Common Mistakes:
  • Expecting all original values including duplicates
  • Confusing DISTINCT with sorting
  • Ignoring repeated values in output
4. What is wrong with this query if you want unique customer names from the customers table?
SELECT DISTINCT name, age FROM customers;
Assuming some customers share the same name but have different ages.
medium
A. It will cause a syntax error.
B. It will return unique names only, ignoring age.
C. It will return unique combinations of name and age, not just names.
D. It will return duplicate names.

Solution

  1. Step 1: Understand DISTINCT with multiple columns

    DISTINCT applies to the entire row of selected columns, so it returns unique pairs of name and age.
  2. Step 2: Analyze the effect on names

    If customers share the same name but different ages, each pair is unique, so duplicates in names alone are not removed.
  3. Final Answer:

    It will return unique combinations of name and age, not just names. -> Option C
  4. Quick Check:

    DISTINCT on multiple columns = unique rows [OK]
Hint: DISTINCT on multiple columns checks all columns together [OK]
Common Mistakes:
  • Thinking DISTINCT applies to each column separately
  • Expecting only unique names when selecting multiple columns
  • Assuming syntax error for multiple columns with DISTINCT
5. You have a table sales with columns region and salesperson. Some salespersons work in multiple regions. Which query will list all unique salespersons regardless of region?
hard
A. SELECT DISTINCT region, salesperson FROM sales;
B. SELECT DISTINCT salesperson FROM sales;
C. SELECT salesperson FROM sales GROUP BY region;
D. SELECT salesperson FROM sales WHERE DISTINCT region;

Solution

  1. Step 1: Identify the goal

    We want unique salespersons only, ignoring regions.
  2. Step 2: Analyze each query

    SELECT DISTINCT region, salesperson FROM sales; returns unique pairs of region and salesperson, so duplicates by salesperson remain if regions differ. SELECT DISTINCT salesperson FROM sales; returns unique salespersons only. SELECT salesperson FROM sales GROUP BY region; groups by region, not salesperson, so duplicates remain. SELECT salesperson FROM sales WHERE DISTINCT region; is invalid syntax.
  3. Final Answer:

    SELECT DISTINCT salesperson FROM sales; -> Option B
  4. Quick Check:

    Distinct on salesperson column only = unique salespersons [OK]
Hint: Use DISTINCT on the single column you want unique values for [OK]
Common Mistakes:
  • Selecting multiple columns with DISTINCT when only one is needed
  • Using GROUP BY on wrong column
  • Writing invalid WHERE DISTINCT syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard