find highest height in sql with row_number and subquery
Thu Apr 21 2022 06:56:31 GMT+0000 (Coordinated Universal Time)
Saved by
@Bambibo9799
SELECT
-- Pull in region and calculate avg tallest height
region,
AVG(height) AS avg_tallest,
-- Calculate region percent of world gdp
SUM(gdp)/SUM(SUM(gdp)) OVER () AS perc_world_gdp
FROM countries AS c
JOIN
(SELECT
-- Pull in country_id and height
country_id,
height,
-- Number the height of each country athletes
ROW_NUMBER() OVER (PARTITION BY country_id ORDER BY height DESC) AS row_num
FROM winter_games AS w
JOIN athletes AS a ON w.athlete_id = a.id
GROUP BY country_id, height
-- Alias as subquery
ORDER BY country_id, height DESC) AS subquery
ON c.id = subquery.country_id
-- Join to country_stats
JOIN country_stats AS cs
ON cs.country_id = c.id
-- Only include the tallest height for each country
WHERE row_num = 1
GROUP BY region;
content_copyCOPY
Comments