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;
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);
Remember that SUM adds all amounts for each product_id.
The query groups rows by product_id and sums the amount for each group. For product 1: 100 + 200 = 300, for product 2: 150 + 100 = 250, for product 3: 50.
Consider a table Expenses with a column cost that contains some NULL values. What does the SUM(cost) function do with NULL values?
Think about how SQL handles NULL in aggregate functions.
The SUM function ignores NULL values and sums only the non-NULL values. It does not treat NULL as zero or count them.
Which of the following SQL queries will cause a syntax error?
Check the GROUP BY clause syntax carefully.
Option D has an incomplete GROUP BY clause without specifying any column, causing a syntax error.
Given a table Transactions with columns category and amount, which query is more efficient to find categories with total amount greater than 1000?
Think about when filtering on aggregate results happens.
Filtering on aggregate results like SUM(amount) must be done with HAVING, not WHERE. Option A is correct and efficient.
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;
Consider how SUM behaves when all values are NULL.
If all values are NULL, SUM returns NULL, not zero or an error.