Standard

Window Functions / Aggregate

SELECT
    Name,
    CountryCode,
    Min(Population) over (PARTITION BY CountryCode) min_population,
    Max(Population) over (PARTITION BY CountryCode) max_population,
    AVG(Population) over p_code avg_population,
    SUM(Population) over p_code sum_population,
    COUNT(Population) over p_code count_population
FROM city
WINDOW p_code AS (PARTITION BY CountryCode)


Name CountryCode min_population max_population avg_population sum_population count_population
Oranjestad ABW 29,998 29,998 29,998.00 29,998 1
Kabul AFG 523,300 4,434,550 1,513,862.50 6,055,450 4
Mazar-e-Sharif AFG 523,300 4,434,550 1,513,862.50 6,055,450 4
Herat AFG 523,300 4,434,550 1,513,862.50 6,055,450 4
Qandahar AFG 523,300 4,434,550 1,513,862.50 6,055,450 4
Benguela AGO 255,000 2,776,168 914,935.00 4,574,675 5
Lobito AGO 255,000 2,776,168 914,935.00 4,574,675 5
Huambo AGO 255,000 2,776,168 914,935.00 4,574,675 5
Luanda AGO 255,000 2,776,168 914,935.00 4,574,675 5
...

Window functions can be written either in the SELECT clause or in a separate WINDOW clause, where the window is given an alias that can be accessed in the SELECT clause.