Bird
Raised Fist0
SQLquery~10 mins

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

Choose your learning style10 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.

Practice

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

Solution

  1. Step 1: Understand the purpose of DISTINCT

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

    Sorting is done by ORDER BY, counting by COUNT(), and updating by UPDATE statement, none of which are related to DISTINCT.
  3. Final Answer:

    It returns only unique rows by removing duplicates. -> Option B
  4. Quick Check:

    DISTINCT = unique rows [OK]
Hint: DISTINCT removes duplicates, showing only unique rows [OK]
Common Mistakes:
  • Confusing DISTINCT with ORDER BY
  • Thinking DISTINCT counts rows
  • Assuming DISTINCT updates 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 UNIQUE city FROM Customers;
C. SELECT city UNIQUE FROM Customers;
D. SELECT city FROM Customers DISTINCT;

Solution

  1. Step 1: Recall correct DISTINCT syntax

    The correct syntax to get unique values is SELECT DISTINCT column_name FROM table_name.
  2. Step 2: Check each option

    SELECT DISTINCT city FROM Customers; matches the correct syntax. The other options use incorrect keyword placement or invalid keywords.
  3. Final Answer:

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

    Correct syntax uses DISTINCT after SELECT [OK]
Hint: Use SELECT DISTINCT column FROM table for unique values [OK]
Common Mistakes:
  • Using UNIQUE instead of DISTINCT
  • Placing DISTINCT after FROM
  • Writing keywords in wrong order
3. Given the table Orders with a column Product containing values: ['Pen', 'Pen', 'Book', 'Notebook', 'Book'], what will the query SELECT DISTINCT Product FROM Orders; return?
medium
A. Pen, Book, Notebook
B. Pen, Book
C. Pen, Pen, Book, Notebook, Book
D. Notebook

Solution

  1. Step 1: Identify duplicates in Product column

    The values are Pen (twice), Book (twice), and Notebook (once).
  2. Step 2: Apply DISTINCT to remove duplicates

    DISTINCT returns each unique value once: Pen, Book, Notebook.
  3. Final Answer:

    Pen, Book, Notebook -> Option A
  4. Quick Check:

    DISTINCT removes duplicates, leaving unique values [OK]
Hint: DISTINCT returns each unique value once [OK]
Common Mistakes:
  • Listing duplicates as separate rows
  • Missing some unique values
  • Confusing DISTINCT with COUNT
4. What is wrong with this query to get unique customer names from Customers table?
SELECT DISTINCT name, age FROM Customers;
Assuming you want unique names only.
medium
A. The query needs GROUP BY instead of DISTINCT.
B. DISTINCT cannot be used with multiple columns.
C. DISTINCT should be replaced with UNIQUE.
D. DISTINCT applies to all selected columns, so duplicates in name may remain if age differs.

Solution

  1. Step 1: Understand how DISTINCT works with multiple columns

    DISTINCT treats the combination of all selected columns as one unit, so rows are unique only if all columns differ.
  2. Step 2: Analyze the effect on name uniqueness

    If the same name appears with different ages, both rows appear because the combination is unique, not just the name.
  3. Final Answer:

    DISTINCT applies to all selected columns, so duplicates in name may remain if age differs. -> Option D
  4. Quick Check:

    DISTINCT on multiple columns = unique row combos [OK]
Hint: DISTINCT on multiple columns checks combined uniqueness [OK]
Common Mistakes:
  • Thinking DISTINCT applies to each column separately
  • Using UNIQUE keyword instead of DISTINCT
  • Confusing DISTINCT with GROUP BY
5. You have a table Sales with columns Region and SalesPerson. You want to find all unique salespersons regardless of region. Which query gives the correct result?
hard
A. SELECT SalesPerson FROM Sales GROUP BY Region;
B. SELECT DISTINCT Region, SalesPerson FROM Sales;
C. SELECT DISTINCT SalesPerson FROM Sales;
D. SELECT UNIQUE SalesPerson FROM Sales;

Solution

  1. Step 1: Identify the goal

    You want unique salespersons regardless of region, so only SalesPerson column matters.
  2. Step 2: Choose query that returns unique SalesPerson values

    SELECT DISTINCT SalesPerson FROM Sales returns unique salespersons. SELECT DISTINCT Region, SalesPerson FROM Sales; returns unique pairs of Region and SalesPerson, which may repeat salespersons. SELECT SalesPerson FROM Sales GROUP BY Region; groups by Region, not SalesPerson, so it won't give unique salespersons. SELECT UNIQUE SalesPerson FROM Sales; uses UNIQUE which is invalid syntax.
  3. Final Answer:

    SELECT DISTINCT SalesPerson FROM Sales; -> Option C
  4. Quick Check:

    Use DISTINCT on the column you want unique values for [OK]
Hint: DISTINCT only on needed column for unique values [OK]
Common Mistakes:
  • Selecting multiple columns with DISTINCT causing duplicates
  • Using GROUP BY on wrong column
  • Using UNIQUE keyword instead of DISTINCT
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