1. Home
  2. Data Science
  3. SQL for Data Analysis
  4. SELECT dept, AVG(salary) FROM staff WHERE AVG(salary) >…

Added 30 Nov -0001

SELECT dept, AVG(salary) FROM staff WHERE AVG(salary) > 50000 GROUP BY dept; fails. Why?

  1. AVG cannot appear in both the select list and the filter of the same query
  2. The GROUP BY clause must also list AVG(salary) explicitly so the aggregate has a name
  3. Aggregates are only permitted in the select list and in the ORDER BY clause
  4. 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

MCQSEXAM

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

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More SQL for Data Analysis MCQs

Practise all SQL for Data Analysis MCQs

Similar MCQs