Given a Persons table with id, name and age columns, which query should be used to get average age of all people who are 18 years or older?
Explanation
SELECT AVG(age) FROM persons HAVING age >= 18
is incorrect because having clause can only be used with fields specified in GROUP BY clause or aggregate operators' results.

SELECT AVG(age) FROM persons WHERE age >= 18 GROUP BY name
will return average age of people with the same name.

SELECT AVG(age) FROM persons HAVING AVG(age) >= 18
will be executed without errors, but will return a completely different ersult

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes