I am new to SQL and doing the learning via datacamp. I was wondering if you can achieve the same result with 'HAVING' as with a nested 'WHERE' clause.
Related: SQL - having VS where I understand that HAVING is used with aggregate functions such as min, max, ..
How could I rewrite the following with HAVING?:
SELECT *
FROM populations
WHERE year = 2015 AND life_expectancy >(
SELECT AVG(life_expectancy)*1.15
FROM populations
);
Suppose I have 6 columns in the table 'populations': A (character), B (character), C (Character), D (number, i.e. life_expectancy as in the example), year and E (number).
I tried the following:
SELECT *
FROM populations
WHERE year = 2015
GROUP BY A, B, C, year
HAVING life_expectancy > AVG(life_expectancy)*1.15;
However, this returns an empty table. I realise it's not a reproducible example, but perhaps a general explanation as to why it might not be working would suffice :)