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
SQL
SQL Quiz
Start Quiz

or Read more about SQL Quiz

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback