ITPEC FE Subject A April 2025 Question 21
Which of the following is the clause that should be inserted into blank A of the SQL statement below in order to display the total sales data for each product that sold for at least $5,000 from the table OrderDetails?
OrderDetails (OrderID, ProductID, UnitPrice, Quantity)
Explanation
GROUP BY ProductID, then HAVING on the sum — the condition is on a per-product total, so it must filter groups, not rows.
"Total sales for each product" means one result row per product, so the rows must be grouped with GROUP BY ProductID. "Sold for at least $5,000" is a condition on each group's SUM(, an aggregate, so it belongs in HAVING, which is evaluated after grouping.
The complete statement is SELECT ProductID, SUM(
Answer (a)
Why not others:
- •(b) — without
GROUP BYthe whole table is one group, so the sum is a single grand total, andProductIDin the select list is neither grouped nor aggregated, which is an error - •(c) —
HAVINGis written beforeGROUP BY, but SQL requiresGROUP BYfirst (the column name is also misspelledProductI) - •(d) —
WHEREfilters individual rows before grouping, so it cannot use an aggregate such asSUM, and withoutGROUP BYthere are no per-product totals
Key rule: Clause order follows evaluation order: WHERE filters rows, GROUP BY forms groups, HAVING filters groups by their aggregates.
Databases · AI-written, may contain errors
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.