Grouping by two columns with a HAVING filter

A retailer records sales in the table orders below. A NULL amount marks an order whose payment has not yet been captured. Amounts are in whole dollars.


order_id

region

channel

amount

1

North

Web

120

2

North

Store

80

3

North

Web

60

4

South

Web

200

5

South

Store

50

6

South

Store

70

7

North

Store

NULL

8

South

Web

40


The following query is run:


SQL
SELECT region, channel, COUNT(*) AS n, SUM(amount) AS total
FROM orders
GROUP BY region, channel
HAVING COUNT(amount) >= 2
ORDER BY total DESC;


Give, as whole numbers, the number of groups that GROUP BY forms before HAVING is applied, the number of rows the query returns, the value of total in the first result row in dollars, and the value of total in the last result row in dollars.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Advanced Queries Questions: