A table employees(emp_id, dept, salary, hire_year) holds one row per employee. Consider this query:
SELECT dept, AVG(salary) AS avg_sal FROM employees WHERE hire_year >= 2020 GROUP BY dept HAVING AVG(salary) > (SELECT AVG(salary) FROM employees);
Which of the following statements about this query are true? Select all that apply.
Replacing the WHERE condition with HAVING hire_year >= 2020 is invalid in standard SQL.
Each avg_sal value is the average over all of that department's employees, whatever their hire year.
A department whose employees were all hired before 2020 cannot appear in the result.
Rewriting the comparison as WHERE AVG(salary) > (...) would return the same departments.
The subquery averages the salaries of every employee, including those hired before 2020.
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:
The following query is run:
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 ________.
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.
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.