0
0
SQLquery~20 mins

SUM function in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SUM Function Mastery
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;
SQL
CREATE TABLE Sales (product_id INT, amount DECIMAL(10,2));
INSERT INTO Sales VALUES (1, 100.00), (2, 150.00), (1, 200.00), (3, 50.00), (2, 100.00);
A[{"product_id":1,"total_sales":100.00},{"product_id":2,"total_sales":150.00},{"product_id":3,"total_sales":50.00}]
B[{"product_id":1,"total_sales":300.00},{"product_id":2,"total_sales":250.00},{"product_id":3,"total_sales":50.00}]
C[{"product_id":1,"total_sales":200.00},{"product_id":2,"total_sales":100.00},{"product_id":3,"total_sales":50.00}]
D[{"product_id":1,"total_sales":350.00},{"product_id":2,"total_sales":250.00},{"product_id":3,"total_sales":50.00}]
Attempts:
2 left
💡 Hint

Remember that SUM adds all amounts for each product_id.

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

Consider a table Expenses with a column cost that contains some NULL values. What does the SUM(cost) function do with NULL values?

AIt treats NULL as zero and includes it in the sum.
BIt returns NULL if any NULL value exists in the column.
CIt ignores NULL values and sums only non-NULL values.
DIt counts NULL values as one and adds them to the sum.
Attempts:
2 left
💡 Hint

Think about how SQL handles NULL in aggregate functions.

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

Which of the following SQL queries will cause a syntax error?

ASELECT SUM(amount) FROM Orders WHERE amount > 100;
BSELECT SUM(amount) AS total_amount FROM Orders GROUP BY customer_id;
CSELECT customer_id, SUM(amount) FROM Orders GROUP BY customer_id;
DSELECT SUM(amount) FROM Orders GROUP BY;
Attempts:
2 left
💡 Hint

Check the GROUP BY clause syntax carefully.

optimization
advanced
2:00remaining
Optimizing SUM with WHERE vs HAVING

Given a table Transactions with columns category and amount, which query is more efficient to find categories with total amount greater than 1000?

ASELECT category, SUM(amount) FROM Transactions GROUP BY category HAVING SUM(amount) > 1000;
BSELECT category, SUM(amount) FROM Transactions WHERE SUM(amount) > 1000 GROUP BY category;
CSELECT category, SUM(amount) FROM Transactions WHERE amount > 1000 GROUP BY category;
DSELECT category, SUM(amount) FROM Transactions GROUP BY category WHERE SUM(amount) > 1000;
Attempts:
2 left
💡 Hint

Think about when filtering on aggregate results happens.

🔧 Debug
expert
1:30remaining
Why does this SUM query return NULL?

Given a table Payments with a column value that contains only NULL values, what will be the result of this query?

SELECT SUM(value) FROM Payments;
ANULL
BEmpty result set
CAn error is raised
D0
Attempts:
2 left
💡 Hint

Consider how SUM behaves when all values are NULL.