cswork / libraryOpen practice workspace

Coding questions · LinkedIn

Find top countries by population per continent

This question evaluates data manipulation competency in SQL/Python, specifically testing aggregation, ranking, deterministic tie-breaking, and percentage calculations across grouped data to identify top countries by population per continent.

Table

world_population

  • continent VARCHAR
  • country VARCHAR
  • population BIGINT

Assume each row is a country’s latest population and (continent, country) is unique.

Tasks

  1. Return the most populous country in each continent.
    • Output columns: continent, country, population.
  2. Follow-up: Return the top 2 most populous countries in each continent.
    • Output columns: continent, country, population, plus a rank_in_continent (1 = largest).
  3. Follow-up: For each continent’s top 2 countries, compute what percentage of the continent’s total population each country represents.
    • Output columns: continent, country, population, continent_population, population_share_pct.
    • population_share_pct = 100 * population / continent_population (round to 2 decimals).

Notes

  • If there are ties in population, break ties by country ascending to make results deterministic.

Top countries by population per continent

You are given a table world_population where each row is a country's latest population and (continent, country) is unique.

The interviewer asks three progressive questions:

  1. Return the most populous country in each continent (continent, country, population).
  2. Follow-up: Return the top 2 most populous countries in each continent, adding a rank_in_continent column (1 = largest).
  3. Follow-up: For each continent's top 2 countries, compute what percentage of the continent's total population each country represents.

Write ONE query that answers the final follow-up and subsumes the earlier parts. For the top 2 most populous countries in each continent, return:

  • continent
  • country
  • population
  • rank_in_continent — 1 = most populous in that continent
  • continent_population — the total population of all countries in that continent (not just the top 2)
  • population_share_pct — 100 * population / continent_population, rounded to 2 decimals

Notes:

  • If countries tie on population, break ties by country ascending so results are deterministic.
  • A continent with fewer than 2 countries returns only the rows it has.
  • Rows may be returned in any order.

Table world_population

columntypeconstraints
continentVARCHAR(50)NOT NULL
countryVARCHAR(100)NOT NULL
populationBIGINTNOT NULL

Sample data:

continentcountrypopulation
AsiaChina1382323332
AsiaIndia1326801576
AsiaIndonesia260581100
AsiaVietnam94444200
North AmericaUnited States of America324118787
North AmericaMexico128632004
North AmericaCanada36286378
EuropeGermany82000000
EuropeFrance67000000
EuropeUnited Kingdom67000000
EuropeItaly60000000
AfricaNigeria98000000
AfricaEgypt98000000
AfricaEthiopia97000000
South AmericaBrazil210000000
South AmericaArgentina44000000
OceaniaAustralia25690000

Expected output

continentcountrypopulationrank_in_continentcontinent_populationpopulation_share_pct
AfricaEgypt98000000129300000033.45
AfricaNigeria98000000229300000033.45
AsiaChina13823233321306415020845.11
AsiaIndia13268015762306415020843.3
EuropeGermany82000000127600000029.71
EuropeFrance67000000227600000024.28
North AmericaUnited States of America324118787148903716966.28
North AmericaMexico128632004248903716926.3
OceaniaAustralia25690000125690000100
South AmericaBrazil210000000125400000082.68
South AmericaArgentina44000000225400000017.32

Hints

  1. ROW_NUMBER() OVER (PARTITION BY continent ORDER BY population DESC, country ASC) gives a deterministic per-continent rank.
  2. A windowed SUM(population) OVER (PARTITION BY continent) computed BEFORE filtering to the top 2 gives the whole continent's total.
  3. Watch out for integer division: use 100.0 (or a numeric cast) when computing the percentage, then ROUND(..., 2).