Screen Link:
https://app.dataquest.io/c/43/m/255/subqueries/7/practice-integrating-a-subquery-with-the-outer-query
My Code:
SELECT Major, Major_category, CAST(Sample_size AS FLOAT) / Total AS ratio
FROM recent_grads
WHERE ratio > AVG(Cast(Sample_size AS FLOAT) / Total)
What I expected to happen:
I expected it to process the comparison between ratio and the average ratio between the Sample_size
and Total
What actually happened:
(sqlite3.OperationalError) misuse of aggregate function AVG()
[SQL: SELECT Major, Major_category, CAST(Sample_size AS FLOAT) / Total AS ratio FROM recent_grads WHERE ratio > AVG(Cast(Sample_size AS FLOAT) / Total)]
(Background on this error at: http://sqlalche.me/e/13/e3q8)
I would like to understand why isn’t SQLite doing the calculation that I indicated? The formula inside AVG()
is selected, therefore SQL knows where to select the data from.