0
0
SQLquery~20 mins

Combining multiple aggregates in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Aggregate Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A[{"customer_id": 1, "total_sales": 300, "avg_sales": 300.0}, {"customer_id": 2, "total_sales": 450, "avg_sales": 450.0}]
B[{"customer_id": 1, "total_sales": 300, "avg_sales": 150.0}, {"customer_id": 2, "total_sales": 450, "avg_sales": 225.0}]
C[{"customer_id": 1, "total_sales": 150, "avg_sales": 75.0}, {"customer_id": 2, "total_sales": 225, "avg_sales": 112.5}]
D[{"customer_id": 1, "total_sales": 300, "avg_sales": null}, {"customer_id": 2, "total_sales": 450, "avg_sales": null}]
Attempts:
2 left
💡 Hint
Remember that AVG calculates the average of the amounts per customer.
📝 Syntax
intermediate
2: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;
ASELECT department, COUNT(employee_id), MAX(salary) FROM employees GROUP BY;
BSELECT department, COUNT(employee_id), MAX(salary) FROM employees;
CSELECT department, COUNT(employee_id), MAX(salary) FROM employees GROUP BY department, salary;
DSELECT department, COUNT(employee_id), MAX(salary) FROM employees GROUP BY department;
Attempts:
2 left
💡 Hint
GROUP BY clause must specify columns to group by.
optimization
advanced
2: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?
ASELECT customer_id, COUNT(order_id) + MAX(amount) FROM orders GROUP BY customer_id;
BSELECT customer_id, (SELECT COUNT(*) FROM orders o2 WHERE o2.customer_id = o1.customer_id), (SELECT MAX(amount) FROM orders o3 WHERE o3.customer_id = o1.customer_id) FROM orders o1 GROUP BY customer_id;
CSELECT customer_id, COUNT(order_id) OVER (PARTITION BY customer_id), MAX(amount) OVER (PARTITION BY customer_id) FROM orders;
DSELECT customer_id, COUNT(order_id), MAX(amount) FROM orders GROUP BY customer_id;
Attempts:
2 left
💡 Hint
Try to avoid subqueries or window functions if simple GROUP BY works.
🧠 Conceptual
advanced
2: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?
ASELECT COUNT(amount), SUM(amount) FROM payments;
BSELECT COUNT(amount), SUM(COALESCE(amount, 0)) FROM payments;
CSELECT COUNT(*), SUM(amount) FROM payments;
DSELECT COUNT(*), SUM(COALESCE(amount, 0)) FROM payments;
Attempts:
2 left
💡 Hint
COUNT(column) counts only non-NULL values, COUNT(*) counts all rows.
🔧 Debug
expert
2:00remaining
Why does this query produce incorrect results combining aggregates?
Consider this query:
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;
ABecause department is not in GROUP BY clause, causing aggregation error.
BBecause COUNT(employee_id) cannot be used with AVG(salary) together.
CBecause the query needs a HAVING clause to filter groups.
DBecause AVG(salary) requires DISTINCT keyword to work.
Attempts:
2 left
💡 Hint
When using aggregates with other columns, those columns must be grouped.