Suppose we have a 'Sales' table with columns 'Product', 'Category', and 'Revenue'. Which of the following query will correctly retrieve the various categories with total revenue greater than 50000?
Suppose we have a 'Sales' table with columns 'Product', 'Category', and 'Revenue'. Which of the following query will correctly retrieve the various categories with total revenue greater than 50000?
Solution
Total revenue per category needs an aggregate, so the rows must be grouped with GROUP BY Category. A condition on an aggregate like SUM(Revenue) cannot go in WHERE, because WHERE filters individual rows before grouping. It must go in HAVING, which filters the groups after aggregation.
So the correct query is SELECT Category FROM Sales GROUP BY Category HAVING SUM(Revenue) > 50000;
Option 1: aggregate functions are not allowed in WHERE, and there is no GROUP BY.
Option 2: WHERE cannot come after GROUP BY, and it cannot hold an aggregate condition anyway.
Option 4: this checks each single row's revenue, not the category's total, so it returns categories having at least one sale above 50000.
Memory trick: WHERE filters rows, HAVING filters groups.
Related questions:
2026: 23 May Shift 1
2026: 23 May Shift 1
2026: 24 May Shift 1
2026: 20 May Shift 1