SELECT dept, AVG(salary) FROM staff WHERE AVG(salary) > 50000 GROUP BY dept; fails. Why?
A
AVG cannot appear in both the select list and the filter of the same query
B
The GROUP BY clause must also list AVG(salary) explicitly so the aggregate has a name
C
Aggregates are only permitted in the select list and in the ORDER BY clause
D
WHERE runs before grouping, so no aggregate exists yet; HAVING must filter instead
Correct answer
Correct answer
D. WHERE runs before grouping, so no aggregate exists yet; HAVING must filter instead
16 views
⚔️
Challenge a friend on SQL for Data Analysis
Same MCQs, timed, live leaderboard. Send the link on WhatsApp – no sign-up needed.
Create challenge
Explanation
Logical evaluation order is FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, so no aggregate exists when WHERE runs and none may be referenced there. HAVING is applied after grouping and may use AVG(salary). The same order explains why an output alias works in ORDER BY but not in WHERE in most engines.
Reference
Asked in: NTS, PPSC IT, Interviews
Doubts & discussionNot sure why the answer is D? Ask here and get an explanation.
Ask a doubt about this MCQ
‹ Previous MCQ SELECT COUNT(CASE WHEN status = 'late' THEN 1 END) AS late FROM loans; is executed. How does the result differ from COUNT(*)? Next MCQ › Table a has 12 rows and table b has 7 rows. SELECT * FROM a CROSS JOIN b; is run. How many rows come back?