Using DISTINCT with Aggregate Functions
Using DISTINCT with Aggregate Functions: Deduplication Rules
When calculating business metrics, you often need to differentiate between the raw volume of events and the number of unique entities involved. For example, a customer may place 10 orders in a single week. If you calculate total orders, you want to count all 10. But if you want to know how many distinct customers placed orders, you must deduplicate before counting. In SQL, this is achieved by embedding DISTINCT inside aggregate functions.
1. Syntax of DISTINCT Inside Aggregates
2. COUNT(*) vs COUNT(col) vs COUNT(DISTINCT col)
Let us observe how each query evaluates against the table above:
3. Combining Multiple DISTINCT Aggregates in One Query
MySQL allows executing multiple distinct aggregate calculations simultaneously:
4. Can You Use DISTINCT with SUM() or AVG()?
Yes, although it is less common:
SUM(DISTINCT salary): Sums each unique salary value only once. If three engineers earn ₹80,000, it only includes ₹80,000 once in the sum.AVG(DISTINCT salary): Calculates the average of unique salary values, ignoring frequency.
5. Best Practices & Common Pitfalls
- High Memory Overhead of
COUNT(DISTINCT): CalculatingCOUNT(DISTINCT col)requires MySQL to maintain a temporary hash set or B-Tree in memory to track all previously seen values. Across 100 million rows with high cardinality, this can consume significant server memory. COUNT(DISTINCT col1, col2): MySQL uniquely supports counting distinct combinations of multiple columns:
Multiple Choice Questions
1. In a table with 5 rows where a column contains values [10, 10, 20, 20, NULL], what does COUNT(DISTINCT column) return?
A. 5 B. 4 C. 2 D. 3 Answer: C Explanation: COUNT(DISTINCT col) deduplicates the values to {10, 20} and ignores the NULL, returning a count of 2.
2. What is the fundamental difference between COUNT(*) and COUNT(DISTINCT user_id)?
A. COUNT() only counts numbers; COUNT(DISTINCT) only counts text B. COUNT() returns the total physical row count; COUNT(DISTINCT user_id) counts only unique, non-null user IDs C. COUNT() is deprecated in MySQL 8.0 D. Both always return identical results Answer: B Explanation: `COUNT() tallies all rows, whereas COUNT(DISTINCT user_id) tallies only unique non-null occurrences of user_id`.
3. Does MySQL permit counting distinct combinations across multiple columns in a single function call (e.g., COUNT(DISTINCT col1, col2))?
A. No, only one column is permitted B. Yes, MySQL natively supports multi-column distinct counts C. Only if both columns are foreign keys D. Only in stored procedures Answer: B Explanation: MySQL supports multi-column arguments in COUNT(DISTINCT col1, col2), counting unique pairwise combinations where neither column is null.
4. What will SUM(DISTINCT val) return for the dataset [100, 100, 200, 300]?
A. 700 B. 600 C. 300 D. 200 Answer: B Explanation: SUM(DISTINCT val) deduplicates the values to {100, 200, 300} before summing, resulting in 100 + 200 + 300 = 600.
5. Why can executing COUNT(DISTINCT) on a column with 50 million unique values be slow?
A. The server must reboot B. The query engine must track and deduplicate all distinct keys in an in-memory or on-disk hash structure C. MySQL disables B-Tree indexes D. DISTINCT converts integers to strings Answer: B Explanation: Deduplication across high-cardinality datasets requires building and probing large in-memory hash sets or temporary tables, consuming significant RAM and CPU.
Database Backups with mysqldump
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Filtering Groups with HAVING vs WHERE | Database Backups with mysqldump |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.