Skip to main contentSkip to solution

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

✅ Correct Option: 3

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.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question