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
Store
80
3
60
4
South
200
5
50
6
70
7
NULL
8
40
The following query is run:
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.
Unlock full course notes by signing up
AVG with NULL ratings
A table reviews stores product ratings in a NUMERIC column rating; a rating is NULL when the reviewer left a comment but no score.
What value does SELECT AVG(rating) FROM reviews WHERE product_id = 7; return? Give your answer to two decimal places.
Tracing WHERE and HAVING together
A table sales records individual sales in dollars:
How many rows does the query return, and what are the highest and lowest values of big_total among the returned rows? Give each answer as a whole number, with the totals in dollars.
COUNT variants with NULLs and duplicates
A university stores course enrolments in the table enrolments below, where a NULL grade means the grade has not yet been recorded.
The query SELECT COUNT(*), COUNT(grade), COUNT(DISTINCT student_id), COUNT(DISTINCT grade) FROM enrolments; is run. Enter each result as a whole number. COUNT(*) returns ________, COUNT(grade) returns ________, COUNT(DISTINCT student_id) returns ________, and COUNT(DISTINCT grade) returns ________.
Interpreting a HAVING clause with a subquery
A table employees(emp_id, dept, salary, hire_year) holds one row per employee. Consider this query:
Which of the following statements about this query are true? Select all that apply.