Challenge - 5 Problems
Aggregate Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Calculate total sales and average sales per customer
Given a table sales with columns
customer_id and amount, what is the output of this query?SELECT customer_id, SUM(amount) AS total_sales, AVG(amount) AS avg_sales FROM sales GROUP BY customer_id ORDER BY customer_id;
SQL
SELECT customer_id, SUM(amount) AS total_sales, AVG(amount) AS avg_sales FROM sales GROUP BY customer_id ORDER BY customer_id;
Attempts:
2 left
💡 Hint
Remember that AVG calculates the average of the amounts per customer.
✗ Incorrect
The query groups sales by customer_id, then calculates the sum and average of amounts for each customer. Option B correctly shows total and average sales per customer.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in combining aggregates
Which option contains a syntax error when combining multiple aggregate functions in one SELECT statement?
SQL
SELECT department, COUNT(employee_id), MAX(salary) FROM employees GROUP BY department;
Attempts:
2 left
💡 Hint
GROUP BY clause must specify columns to group by.
✗ Incorrect
Option A has 'GROUP BY' without any column, which is a syntax error. Other options are syntactically valid though some may produce different results.
❓ optimization
advanced2:00remaining
Optimize query combining multiple aggregates
You want to get the total number of orders and the maximum order amount per customer from the
orders table. Which query is the most efficient?Attempts:
2 left
💡 Hint
Try to avoid subqueries or window functions if simple GROUP BY works.
✗ Incorrect
Option D uses GROUP BY with aggregates directly, which is efficient. Option D uses correlated subqueries causing performance issues. Option D uses window functions returning repeated rows. Option D adds count and max, which is logically incorrect.
🧠 Conceptual
advanced2:00remaining
Understanding NULL handling in multiple aggregates
Given a table
payments with some NULL values in the amount column, which aggregate function combination correctly counts all rows and sums only non-NULL amounts?Attempts:
2 left
💡 Hint
COUNT(column) counts only non-NULL values, COUNT(*) counts all rows.
✗ Incorrect
COUNT(*) counts all rows including those with NULL in amount. SUM(amount) sums only non-NULL amounts. Option C correctly counts all rows and sums non-NULL amounts.
🔧 Debug
expert2:00remaining
Why does this query produce incorrect results combining aggregates?
Consider this query:
Why does it produce an error or incorrect results?
SELECT department, COUNT(employee_id), AVG(salary) FROM employees;
Why does it produce an error or incorrect results?
SQL
SELECT department, COUNT(employee_id), AVG(salary) FROM employees;
Attempts:
2 left
💡 Hint
When using aggregates with other columns, those columns must be grouped.
✗ Incorrect
Selecting a non-aggregated column without GROUP BY causes SQL error or undefined behavior. Option A correctly identifies the missing GROUP BY.