I'm trying to figure out which country has the population that is closest to the global average country population.
I have the following relation;
Column | Type | Modifiers
------------+-----------------------+-----------
name | character varying(35) | not null
code | character varying(4) | not null
capital | character varying(35) |
province | character varying(35) |
area | numeric |
population | numeric |
I tried doing this, where I simply select the name, the population for the country, getting the average country population and just compare it with a where clause. Finally I would just put a limit 1 to get the desired result. Oddly enough, I couldn't do this, what am I doing wrong? I'm using PostgreSQL.
select name, population, avg(population) as gac
from country
where population <= avg(population);
The error; ERROR: aggregates not allowed in WHERE clause LINE 1: ...pulation) as gac from country where population <= avg(popula...