Bird
Raised Fist0
MySQLquery~5 mins

DISTINCT for unique values in MySQL - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the SQL keyword DISTINCT do?
It removes duplicate rows from the result set, showing only unique values.
Click to reveal answer
beginner
How would you select unique city names from a table called Customers?
Use SELECT DISTINCT city FROM Customers; to get only unique city names.
Click to reveal answer
intermediate
Can DISTINCT be used on multiple columns?
Yes, it returns unique combinations of the specified columns.
Click to reveal answer
beginner
What happens if you use DISTINCT on a column with all unique values?
The result will be the same as a normal SELECT because all values are already unique.
Click to reveal answer
intermediate
Is DISTINCT case sensitive when filtering unique values?
It depends on the database collation settings, but usually it is case insensitive in MySQL by default.
Click to reveal answer
What does SELECT DISTINCT do in SQL?
ACounts the number of rows
BDeletes duplicate rows from the table
CSorts the rows alphabetically
DReturns only unique rows
Which query returns unique combinations of city and state from Customers?
ASELECT DISTINCT city, state FROM Customers;
BSELECT city, state WHERE DISTINCT FROM Customers;
CSELECT UNIQUE city, state FROM Customers;
DSELECT city, state FROM Customers;
If a column has all unique values, what effect does DISTINCT have?
ARemoves all rows
BDuplicates the rows
CReturns the same result as SELECT
DReturns an error
Can DISTINCT be used on multiple columns at once?
AYes, it returns unique combinations of those columns
BNo, only one column at a time
CYes, but only if columns are numeric
DNo, it causes a syntax error
Is DISTINCT case sensitive in MySQL by default?
AYes, always case sensitive
BNo, usually case insensitive depending on collation
CIt depends on the SELECT statement
DIt causes an error if case differs
Explain how the DISTINCT keyword works in SQL and give an example query.
Think about how you would find unique items in a list.
You got /3 concepts.
    Describe what happens when you use DISTINCT on multiple columns in a SELECT statement.
    Consider how two columns together create a unique pair.
    You got /3 concepts.

      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