Challenge - 5 Problems
Master of JOIN with aggregate functions
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Calculate total sales per customer
Given two tables Customers and Orders, what is the total amount spent by each customer? The
Orders table has a column amount representing the order value. Use a JOIN and aggregate function to find the total spent per customer.MySQL
SELECT c.customer_id, c.customer_name, SUM(o.amount) AS total_spent FROM Customers c JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id, c.customer_name;
Attempts:
2 left
💡 Hint
Use JOIN to connect customers with their orders, then SUM the order amounts grouped by customer.
✗ Incorrect
The query joins Customers and Orders on customer_id, then sums the amounts per customer. Alice has orders totaling 250, Bob has 150.
❓ query_result
intermediate2:00remaining
Find the number of orders per product
Using tables Products and OrderDetails, find how many orders include each product. Use JOIN and COUNT aggregate function.
MySQL
SELECT p.product_id, p.product_name, COUNT(od.order_id) AS order_count FROM Products p JOIN OrderDetails od ON p.product_id = od.product_id GROUP BY p.product_id, p.product_name;
Attempts:
2 left
💡 Hint
Count the order IDs for each product after joining the tables.
✗ Incorrect
The JOIN connects products with order details. Counting order_id per product shows how many orders include that product.
📝 Syntax
advanced2:00remaining
Identify the syntax error in JOIN with aggregate
Which option contains a syntax error in this query that joins
Employees and Sales and calculates total sales per employee?MySQL
SELECT e.employee_id, e.name, SUM(s.amount) AS total_sales FROM Employees e JOIN Sales s ON e.employee_id = s.employee_id GROUP BY e.employee_id, e.name;
Attempts:
2 left
💡 Hint
GROUP BY must include all non-aggregated selected columns from the first table.
✗ Incorrect
Option A groups by s.amount which is aggregated, causing a syntax error. GROUP BY should only include e.employee_id and e.name.
❓ optimization
advanced2:00remaining
Optimize query with JOIN and aggregate
Which query is the most efficient to find the average order amount per customer from
Customers and Orders tables?Attempts:
2 left
💡 Hint
Avoid unnecessary JOINs when data is only in one table.
✗ Incorrect
Option B queries only Orders and groups by customer_id, which is faster than joining with Customers if only customer_id and average amount are needed.
🧠 Conceptual
expert3:00remaining
Understanding JOIN with aggregate and NULL values
Consider
Customers and Orders tables. Some customers have no orders. Which query correctly lists all customers with their total order amount, showing 0 for customers without orders?Attempts:
2 left
💡 Hint
Use LEFT JOIN to include all customers and COALESCE to replace NULL sums with zero.
✗ Incorrect
LEFT JOIN includes all customers even without orders. SUM returns NULL if no matching rows, so COALESCE replaces NULL with 0.