DISTINCT for unique values in MySQL - Time & Space Complexity
We want to understand how the time to find unique values changes as the data grows.
How does using DISTINCT affect the work the database does?
Analyze the time complexity of the following code snippet.
SELECT DISTINCT city
FROM customers;
This query finds all unique city names from the customers table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each row in the customers table to check the city value.
- How many times: Once for each row in the table.
As the number of rows grows, the database must look at more city values to find unique ones.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks to find unique cities |
| 100 | About 100 checks to find unique cities |
| 1000 | About 1000 checks to find unique cities |
Pattern observation: The work grows roughly in direct proportion to the number of rows.
Time Complexity: O(n)
This means the time to find unique cities grows linearly as the number of rows increases.
[X] Wrong: "DISTINCT instantly returns unique values without checking all rows."
[OK] Correct: The database must look at each row to know if the city is new or already seen, so it checks all rows.
Understanding how DISTINCT works helps you explain query performance clearly and shows you know how databases handle data.
"What if we added an index on the city column? How would the time complexity change?"
Practice
DISTINCT keyword do in a MySQL query?Solution
Step 1: Understand the purpose of DISTINCT
The DISTINCT keyword is used to filter out duplicate rows in the result set.Step 2: Compare with other options
Sorting is done by ORDER BY, counting by COUNT(), and updating by UPDATE statement, so these are incorrect.Final Answer:
It returns only unique values by removing duplicates. -> Option DQuick Check:
DISTINCT = unique values [OK]
- Confusing DISTINCT with ORDER BY
- Thinking DISTINCT counts rows
- Using DISTINCT to update data
customers?Solution
Step 1: Recall correct syntax for unique selection
MySQL uses DISTINCT keyword immediately after SELECT to get unique values.Step 2: Check each option
SELECT DISTINCT city FROM customers; uses correct syntax: SELECT DISTINCT city FROM customers; others are invalid syntax.Final Answer:
SELECT DISTINCT city FROM customers; -> Option AQuick Check:
Syntax for unique values = SELECT DISTINCT [OK]
- Using UNIQUE instead of DISTINCT in SELECT
- Placing DISTINCT after FROM clause
- Misplacing keywords causing syntax errors
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?Solution
Step 1: Identify duplicates in product_id
The values are 1, 2, 2, 3, 1, 4. Duplicates are 1 and 2 repeated.Step 2: Apply DISTINCT to remove duplicates
Removing duplicates leaves unique values: 1, 2, 3, 4.Final Answer:
[1, 2, 3, 4] -> Option AQuick Check:
DISTINCT removes duplicates = unique list [OK]
- Expecting all original values including duplicates
- Confusing DISTINCT with sorting
- Ignoring repeated values in output
customers table?SELECT DISTINCT name, age FROM customers;Assuming some customers share the same name but have different ages.
Solution
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.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.Final Answer:
It will return unique combinations of name and age, not just names. -> Option CQuick Check:
DISTINCT on multiple columns = unique rows [OK]
- Thinking DISTINCT applies to each column separately
- Expecting only unique names when selecting multiple columns
- Assuming syntax error for multiple columns with DISTINCT
sales with columns region and salesperson. Some salespersons work in multiple regions. Which query will list all unique salespersons regardless of region?Solution
Step 1: Identify the goal
We want unique salespersons only, ignoring regions.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.Final Answer:
SELECT DISTINCT salesperson FROM sales; -> Option BQuick Check:
Distinct on salesperson column only = unique salespersons [OK]
- Selecting multiple columns with DISTINCT when only one is needed
- Using GROUP BY on wrong column
- Writing invalid WHERE DISTINCT syntax
Ready to prove it?
Pass the test and this pattern moves unknown
