0
0
MySQLquery~20 mins

SUM function in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SUM Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Calculate total sales for each product

Given a table sales with columns product_id and amount, what is the output of this query?

SELECT product_id, SUM(amount) AS total_sales FROM sales GROUP BY product_id ORDER BY product_id;
MySQL
CREATE TABLE sales (product_id INT, amount DECIMAL(10,2));
INSERT INTO sales VALUES (1, 10.00), (1, 15.50), (2, 7.25), (2, 12.75), (3, 20.00);
A[{"product_id":1,"total_sales":25.50},{"product_id":2,"total_sales":20.00}]
B[{"product_id":1,"total_sales":10.00},{"product_id":1,"total_sales":15.50},{"product_id":2,"total_sales":7.25},{"product_id":2,"total_sales":12.75},{"product_id":3,"total_sales":20.00}]
C[{"product_id":1,"total_sales":25.50},{"product_id":2,"total_sales":20.00},{"product_id":3,"total_sales":20.00}]
D[{"product_id":1,"total_sales":35.50},{"product_id":2,"total_sales":20.00},{"product_id":3,"total_sales":20.00}]
Attempts:
2 left
💡 Hint

Remember that SUM() adds values per group defined by GROUP BY.

🧠 Conceptual
intermediate
1:30remaining
Understanding SUM with NULL values

What will be the result of the following query if the amount column contains NULL values?

SELECT SUM(amount) FROM sales;
MySQL
CREATE TABLE sales (amount DECIMAL(10,2));
INSERT INTO sales VALUES (10.00), (NULL), (20.00);
A30.00
BNULL
C10.00
D0
Attempts:
2 left
💡 Hint

Think about how SUM() treats NULL values in aggregation.

📝 Syntax
advanced
1:00remaining
Identify the syntax error in SUM usage

Which option contains a syntax error when trying to calculate the total amount?

SELECT SUM(amount) FROM sales;
ASELECT SUM(amount) total FROM sales;
BSELECT SUM(amount) FROM sales;
CSELECT SUM(amount) AS total FROM sales;
DSELECT SUM amount FROM sales;
Attempts:
2 left
💡 Hint

Check the correct syntax for calling functions in SQL.

optimization
advanced
1:30remaining
Optimizing SUM with WHERE clause

Which query efficiently calculates the total sales amount only for product_id 1?

MySQL
CREATE TABLE sales (product_id INT, amount DECIMAL(10,2));
INSERT INTO sales VALUES (1, 10.00), (1, 15.50), (2, 7.25);
ASELECT SUM(amount) FROM sales WHERE amount > 0;
BSELECT SUM(amount) FROM sales WHERE product_id = 1;
CSELECT SUM(amount) FROM sales;
DSELECT SUM(amount) FROM sales GROUP BY product_id HAVING product_id = 1;
Attempts:
2 left
💡 Hint

Think about filtering rows before aggregation for efficiency.

🔧 Debug
expert
2:00remaining
Why does this SUM query return NULL?

Given the table orders with column price, why does this query return NULL?

SELECT SUM(price) FROM orders WHERE price < 0;
MySQL
CREATE TABLE orders (price DECIMAL(10,2));
INSERT INTO orders VALUES (10.00), (20.00), (30.00);
ABecause there are no rows where price is less than 0, SUM returns NULL.
BBecause SUM cannot handle decimal values and returns NULL.
CBecause the WHERE clause syntax is incorrect, causing NULL result.
DBecause the table is empty, SUM returns NULL.
Attempts:
2 left
💡 Hint

Consider what happens when no rows match the WHERE condition.