Practice mode DBMS and SQL

Why does SELECT dept FROM Emp WHERE AVG(salary) > 5000 GROUP BY dept fail, while the same condition written in HAVING works?

Correct answer A. WHERE filters individual rows before groups and aggregates exist

Explanation

Logically, SQL processes FROM, then WHERE on single rows, then GROUP BY, then HAVING on groups, then SELECT and ORDER BY. When WHERE runs, there are no groups yet, so an aggregate such as AVG(salary) has nothing to work on and is not allowed there. HAVING runs after grouping, so it can test aggregates. HAVING comes after WHERE, not before.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.