ITPEC FE Subject A April 2025 Question 21

Source exam: ITPEC FE Subject A April 2025Topic: Databases

ITPEC FE Subject A April 2025 — Question 21 of 60

SQL GROUP BY with HAVING — filter grouped results using an aggregate condition.

The query calculates total sales per product and keeps only those with sales ≥ $5,000:

SELECT ProductID, SUM(UnitPrice * Quantity) FROM OrderDetails GROUP BY ProductID HAVING SUM(UnitPrice * Quantity) >= 5000;

  • GROUP BY ProductID — groups rows by product
  • HAVING SUM(...) >= 5000 — filters groups after aggregation
  • GROUP BY must come before HAVING

Why not others:
- (b) HAVING without GROUP BY — aggregate has no grouping target

- (c) HAVING before GROUP BY — wrong clause order (syntax error)

- (d) WHERE with SUM(...)WHERE cannot use aggregate functions; it filters rows before grouping

Key rule: WHERE filters rows → GROUP BY groups → HAVING filters groups.

AI-generated — may contain errors

The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.

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.