0
0
MySQLquery~20 mins

JOIN with aggregate functions in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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
intermediate
2: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;
A[{"customer_id": 1, "customer_name": "Alice", "total_spent": 100}, {"customer_id": 2, "customer_name": "Bob", "total_spent": 150}]
B[{"customer_id": 1, "customer_name": "Alice", "total_spent": 400}, {"customer_id": 2, "customer_name": "Bob", "total_spent": 150}]
C[{"customer_id": 1, "customer_name": "Alice", "total_spent": 250}, {"customer_id": 2, "customer_name": "Bob", "total_spent": null}]
D[{"customer_id": 1, "customer_name": "Alice", "total_spent": 250}, {"customer_id": 2, "customer_name": "Bob", "total_spent": 150}]
Attempts:
2 left
💡 Hint
Use JOIN to connect customers with their orders, then SUM the order amounts grouped by customer.
query_result
intermediate
2: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;
A[{"product_id": 101, "product_name": "Pen", "order_count": 3}, {"product_id": 102, "product_name": "Notebook", "order_count": 2}]
B[{"product_id": 101, "product_name": "Pen", "order_count": 2}, {"product_id": 102, "product_name": "Notebook", "order_count": 2}]
C[{"product_id": 101, "product_name": "Pen", "order_count": 3}, {"product_id": 102, "product_name": "Notebook", "order_count": 0}]
D[{"product_id": 101, "product_name": "Pen", "order_count": null}, {"product_id": 102, "product_name": "Notebook", "order_count": 2}]
Attempts:
2 left
💡 Hint
Count the order IDs for each product after joining the tables.
📝 Syntax
advanced
2: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;
ASELECT 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, s.amount;
BSELECT 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;
CSELECT 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.name, e.employee_id;
DSELECT 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.
optimization
advanced
2: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?
ASELECT c.customer_id, AVG(o.amount) AS avg_amount FROM Customers c JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id;
BSELECT customer_id, AVG(amount) AS avg_amount FROM Orders GROUP BY customer_id;
CSELECT c.customer_id, (SELECT AVG(amount) FROM Orders WHERE customer_id = c.customer_id) AS avg_amount FROM Customers c;
DSELECT c.customer_id, SUM(o.amount)/COUNT(o.order_id) AS avg_amount FROM Customers c JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id;
Attempts:
2 left
💡 Hint
Avoid unnecessary JOINs when data is only in one table.
🧠 Conceptual
expert
3: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?
ASELECT c.customer_id, IFNULL(SUM(o.amount), 0) AS total_spent FROM Customers c RIGHT JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id;
BSELECT c.customer_id, SUM(o.amount) AS total_spent FROM Customers c JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id;
CSELECT c.customer_id, COALESCE(SUM(o.amount), 0) AS total_spent FROM Customers c LEFT JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id;
DSELECT c.customer_id, SUM(o.amount) AS total_spent FROM Customers c LEFT JOIN Orders o ON c.customer_id = o.customer_id WHERE o.amount IS NOT NULL GROUP BY c.customer_id;
Attempts:
2 left
💡 Hint
Use LEFT JOIN to include all customers and COALESCE to replace NULL sums with zero.