DISTINCT vs GROUP BY in SQL: When to Use Each
DISTINCT and GROUP BY can sometimes produce results that look identical, which leads many learners to treat them as interchangeable. They are not. Understanding precisely what each one does — and specifically what GROUP BY can accomplish that DISTINCT fundamentally cannot — is an important, frequently tested piece of SQL conceptual knowledge that goes well beyond memorizing syntax.
Key Definitions
- DISTINCT: A SQL keyword that removes duplicate rows from a query's result, based on the exact combination of all selected columns.
- Duplicate row: A row that has the exact same values across all selected columns as another row in the result set.
- Aggregation capability: The ability to compute a summary value, like a total or average, across multiple rows within a group — something GROUP BY supports and DISTINCT does not.
What You'll Learn
- Define what DISTINCT does and how it removes duplicate rows from a result.
- Identify the specific scenario where DISTINCT and GROUP BY produce identical output.
- Explain why GROUP BY, but not DISTINCT, can compute aggregate values like SUM or COUNT per group.
- Choose correctly between DISTINCT and GROUP BY based on the actual requirement of a given query.
Detailed Explanation
DISTINCT operates on the final selected columns of a query, removing any rows that are exact duplicates of another row already in the result, based on every column listed in the SELECT statement together. If you write SELECT DISTINCT city FROM customers, and three customers share the city 'Mumbai,' DISTINCT collapses those three identical 'Mumbai' rows down to just one, giving you a simple list of unique city values.
GROUP BY, when used without any aggregate function, can produce an outwardly identical result to this same DISTINCT query — GROUP BY city alone, with just city selected, also gives you one row per unique city value, looking exactly the same as the DISTINCT version. This surface-level similarity is precisely where the confusion between the two commonly originates.
However, the two diverge completely the moment you need to compute something about each unique group, not just list the unique values themselves. DISTINCT has no mechanism at all for calculating a SUM, COUNT, or AVG per unique value — it can only deduplicate the exact rows you have selected, nothing more. GROUP BY, by contrast, is specifically designed to enable exactly this kind of per-group calculation: GROUP BY city combined with COUNT(*) tells you not just the unique cities, but how many customers exist in each one, a calculation DISTINCT is fundamentally incapable of performing.
As a practical rule: if your goal is simply to see a list of unique values with no accompanying calculation, DISTINCT is often the simpler, more direct choice syntactically. If your goal requires any kind of per-group summary — a count, a total, an average, a minimum, or a maximum — GROUP BY is required, since DISTINCT has no equivalent capability whatsoever.
Visual Summary
Draw two side-by-side query results using the same customers table. Left side labeled 'SELECT DISTINCT city' showing a simple list: Mumbai, Delhi (2 rows, no additional numbers). Right side labeled 'SELECT city, COUNT(*) FROM customers GROUP BY city' showing the same two cities but each paired with a count: Mumbai (3), Delhi (1). Caption: 'Both list unique cities, but only GROUP BY can also tell you how many customers are in each.'
Quick Reference
| Aspect | DISTINCT | GROUP BY |
|---|---|---|
| Removes duplicate rows | Yes | Yes (as a side effect of grouping) |
| Can compute SUM, COUNT, AVG per group | No | Yes |
| Typical use case | Listing unique values only | Listing unique values WITH a calculated summary |
| Can be combined with HAVING | No | Yes |
SQL Example
CREATE TABLE customers (
customer_id INT PRIMARY KEY AUTO_INCREMENT,
customer_name VARCHAR(100),
city VARCHAR(100)
);
INSERT INTO customers (customer_name, city) VALUES
('Meera Iyer', 'Mumbai'),
('Vikram Rao', 'Delhi'),
('Sana Qureshi', 'Mumbai'),
('Divya Menon', 'Mumbai');
-- DISTINCT: just a list of unique cities, no counts possible
SELECT DISTINCT city FROM customers;
-- GROUP BY: same unique cities, but with a count per city
SELECT city, COUNT(*) AS customer_count
FROM customers
GROUP BY city;
The DISTINCT query returns exactly two rows: Mumbai and Delhi, with no further information about how many customers are in each. The GROUP BY query returns the same two unique city values, but additionally computes COUNT(*) for each, showing Mumbai has 3 customers and Delhi has 1 — a calculation that DISTINCT alone has no mechanism to perform, since it can only deduplicate rows, not summarize them.
Real-World Examples
- A dropdown filter listing unique product categories on an e-commerce website typically uses DISTINCT, since it only needs the list of unique values, not a count.
- A sales dashboard showing 'number of orders per category' requires GROUP BY with COUNT, since it needs both the unique categories and a calculated total for each.
- A data cleaning script checking for unique email domains in a customer list might use DISTINCT simply to see what domains exist, without needing further calculation.
- An HR report showing 'average salary per department' requires GROUP BY with AVG, since DISTINCT alone could never compute that per-department average.
- An autocomplete search feature suggesting unique city names as a user types typically relies on a simple DISTINCT query for speed and simplicity.
Common Mistakes to Avoid
- Assuming DISTINCT and GROUP BY are always interchangeable, without recognizing GROUP BY's unique aggregation capability.
- Using GROUP BY unnecessarily when a simple DISTINCT would accomplish the same unique-value listing more directly and clearly.
- Attempting to use DISTINCT when the actual requirement includes a per-group calculation like a total or average, which DISTINCT cannot provide.
- Trying to combine DISTINCT with a HAVING clause, not realizing HAVING is designed specifically to work with GROUP BY's aggregation.
Interview Questions
Q1. When do DISTINCT and GROUP BY produce identical results?
When GROUP BY is used without any aggregate function and the SELECT list matches exactly the columns used for grouping, both DISTINCT and GROUP BY will produce the same set of unique rows, since both are effectively deduplicating based on the same column values.
Q2. What can GROUP BY do that DISTINCT fundamentally cannot?
GROUP BY can compute aggregate values like SUM, COUNT, AVG, MIN, or MAX for each unique group, in addition to listing the unique values themselves. DISTINCT has no mechanism at all for performing this kind of per-group calculation.
Q3. If you only need a list of unique values with no calculation, which is generally preferred?
DISTINCT is generally the simpler, more direct choice for this specific purpose, since it clearly communicates the intent of just deduplicating rows, without the added complexity of a GROUP BY clause that isn't being used for any aggregation.
Q4. Can DISTINCT be combined with a HAVING clause?
No, DISTINCT has no concept of groups to filter with HAVING. HAVING is specifically designed to work with GROUP BY, filtering aggregated group results, which is not a capability DISTINCT possesses.
Practice MCQs
1. DISTINCT and GROUP BY can produce identical results when:
- GROUP BY includes an aggregate function
- GROUP BY is used without any aggregate function and matches the SELECT columns
- DISTINCT is combined with HAVING
- Never, they are always different
Answer: B. GROUP BY is used without any aggregate function and matches the SELECT columns
Explanation: In this specific scenario, both DISTINCT and GROUP BY simply deduplicate rows based on the same set of column values, producing an identical result.
2. Which of the following can DISTINCT never do, unlike GROUP BY?
- Remove duplicate rows
- List unique values
- Compute a SUM or COUNT per unique value
- Work with a single column
Answer: C. Compute a SUM or COUNT per unique value
Explanation: DISTINCT can only deduplicate rows; it has no built-in mechanism to calculate an aggregate value like SUM or COUNT for each unique value, which is exactly what GROUP BY is designed to do.
3. For a report requiring 'total sales per unique product,' you should use:
- DISTINCT alone
- GROUP BY with SUM
- DISTINCT with ORDER BY
- WHERE alone
Answer: B. GROUP BY with SUM
Explanation: This report requires calculating a total per unique product, which is specifically what GROUP BY combined with SUM is designed to accomplish; DISTINCT alone cannot compute this.
4. Can DISTINCT be combined with a HAVING clause?
- Yes, always
- No, HAVING is designed specifically for GROUP BY
- Only in MySQL
- Only with COUNT(DISTINCT ...)
Answer: B. No, HAVING is designed specifically for GROUP BY
Explanation: HAVING filters aggregated groups, a concept DISTINCT has no equivalent for, since DISTINCT only deduplicates rows without forming true aggregate groups.
Quick Revision Points
- DISTINCT and GROUP BY can look identical only when GROUP BY is used without any aggregate function on matching columns.
- GROUP BY, unlike DISTINCT, can compute aggregate values (SUM, COUNT, AVG, MIN, MAX) per unique group.
- DISTINCT is the simpler, more direct choice for a pure list of unique values with no accompanying calculation.
- HAVING is exclusively a GROUP BY companion clause and cannot be meaningfully combined with DISTINCT.
Conclusion
- DISTINCT and GROUP BY overlap only in the narrow case of pure deduplication without any aggregation.
- GROUP BY's true purpose and unique strength is enabling per-group aggregate calculations, which DISTINCT cannot replicate.
- Choosing between them should be based on whether any calculation is needed alongside the unique value listing.
- This is a foundational conceptual distinction frequently tested in interviews beyond simple syntax memorization.
DISTINCT and GROUP BY can produce identical output only in the specific case where GROUP BY is used without any aggregate function on the exact same columns being selected — both are simply deduplicating rows in that scenario. Their true difference emerges the moment a per-group calculation is needed: GROUP BY can compute SUM, COUNT, AVG, MIN, or MAX for each unique group, a capability DISTINCT entirely lacks, since DISTINCT can only remove duplicate rows, never summarize them. Choosing correctly between the two comes down to a simple question: does this query need any calculation alongside its list of unique values?