Interview Ready Questions on Group By and Window Functions in SQL: Exploring the World Population Dataset.

The Analyst GeekThe Analyst Geek
13 min read

1. Introduction -

  • Database: MS SQL SERVER

  • We have data from 20 countries on which we are performing the Grouping and Advance SQL functions like Window Functions.

  • For grouping follow - FWGH (From - Where - Group BY - Having) Sequence.

2. Create & Insert Statement -

Creating Database

create database Country;
use Country;

Creating Table

-- Create table
CREATE TABLE Country (
    id INT,
    code VARCHAR(2),
    name VARCHAR(100),
    area INT,
    area_land INT,
    area_water INT,
    population BIGINT,
    population_growth DECIMAL(4, 2),
    birth_rate DECIMAL(5, 2),
    death_rate DECIMAL(5, 2),
    migration_rate DECIMAL(5, 2),
    continent VARCHAR(50) 
);

Insert Statement


INSERT INTO Country (id, code, name, area, area_land, area_water, population, population_growth, birth_rate, death_rate, migration_rate, continent)
VALUES
    (1, 'af', 'Afghanistan', 652230, 652230, 0, 32564342, 2.32, 38.57, 13.89, 1.51, 'Asia'),
    (2, 'al', 'Albania', 28748, 27398, 1350, 3029278, 0.3, 12.92, 6.58, 3.3, 'Europe'),
    (3, 'ag', 'Algeria', 2381741, 2381741, 0, 39542166, 1.84, 23.67, 4.31, 0.92, 'Africa'),
    (4, 'an', 'Andorra', 468, 468, 0, 85580, 0.12, 8.13, 6.96, 0.0, 'Europe'),
    (5, 'ao', 'Angola', 1246700, 1246700, 0, 19625353, 2.78, 38.78, 11.49, 0.46, 'Africa'),
    (6, 'aq', 'Antarctica', 14000000, 280000, 13720000, 1106, NULL, NULL, NULL, NULL, 'Antarctica'),
    (7, 'ar', 'Argentina', 2780400, 2736690, 43710, 43431886, 0.93, 16.64, 7.33, 0.0, 'South America'),
    (8, 'am', 'Armenia', 29800, 28203, 1597, 3056382, 0.2, 13.61, 9.34, 0.0, 'Asia'),
    (9, 'aw', 'Aruba', 180, 180, 0, 112162, 1.22, 12.67, 8.68, 8.09, 'North America'),
    (10, 'au', 'Australia', 7692024, 7682300, 9724, 22751014, 1.05, 12.15, 7.14, 5.65, 'Oceania'),
    (11, 'at', 'Austria', 83858, 82445, 1413, 8665550, 0.55, 9.41, 9.42, 5.56, 'Europe'),
    (12, 'az', 'Azerbaijan', 86600, 82629, 3971, 9780780, 0.96, 16.64, 7.07, 0.0, 'Asia'),
    (13, 'bs', 'Bahamas', 13880, 10010, 3870, 324597, 0.95, 15.5, 7.29, 2.01, 'North America'),
    (14, 'bh', 'Bahrain', 760, 760, 0, 1346613, 4.51, 15.43, 2.71, 13.31, 'Asia'),
    (15, 'bd', 'Bangladesh', 147570, 130170, 17400, 156186882, 1.6, 19.68, 5.96, 0.0, 'Asia'),
    (16, 'bb', 'Barbados', 430, 430, 0, 290604, 0.31, 11.87, 8.69, 0.0, 'North America'),
    (17, 'by', 'Belarus', 207600, 202900, 4700, 9589689, 0.2, 10.7, 13.36, 0.7, 'Europe'),
    (18, 'be', 'Belgium', 30528, 30280, 248, 11323973, 0.66, 11.98, 9.54, 2.91, 'Europe'),
    (19, 'bz', 'Belize', 22966, 22806, 160, 347369, 1.97, 24.86, 5.29, 0.0, 'North America'),
    (20, 'bj', 'Benin', 112622, 110622, 2000, 10448647, 2.78, 38.85, 8.21, 0.0, 'Africa');

3. Table Preview

Query -

select * from Country2;

Output -

idcodenameareaarea_landarea_waterpopulationpopulation_growthbirth_ratedeath_ratemigration_ratecontinent
1afAfghanistan6522306522300325643422.3238.5713.891.51Asia
2alAlbania2874827398135030292780.3012.926.583.30Europe
3agAlgeria238174123817410395421661.8423.674.310.92Africa
4anAndorra4684680855800.128.136.960.00Europe
5aoAngola124670012467000196253532.7838.7811.490.46Africa
6aqAntarctica14000000280000137200001106NULLNULLNULLNULLAntarctica
7arArgentina2780400273669043710434318860.9316.647.330.00South America
8amArmenia2980028203159730563820.2013.619.340.00Asia
9awAruba18018001121621.2212.678.688.09North America
10auAustralia769202476823009724227510141.0512.157.145.65Oceania
11atAustria8385882445141386655500.559.419.425.56Europe
12azAzerbaijan8660082629397197807800.9616.647.070.00Asia
13bsBahamas138801001038703245970.9515.507.292.01North America
14bhBahrain760760013466134.5115.432.7113.31Asia
15bdBangladesh147570130170174001561868821.6019.685.960.00Asia
16bbBarbados43043002906040.3111.878.690.00North America
17byBelarus207600202900470095896890.2010.7013.360.70Europe
18beBelgium3052830280248113239730.6611.989.542.91Europe
19bzBelize22966228061603473691.9724.865.290.00North America
20bjBenin1126221106222000104486472.7838.858.210.00Africa

4. Questions

Question 1. - --Find the total population of each continent.

  • Query:
select sum(population) total_population, continent
from Country2
group by continent;
  • Output
total_populationcontinent
69616166Africa
1106Antarctica
202934999Asia
32694070Europe
1074732North America
22751014Oceania
43431886South America

Question 2. - Calculate the average area_land for countries in each continent.

  • Query:
select avg(area_land) avg_land_area, continent 
from country2
group by continent;
  • Output
avg_land_areacontinent
1246354Africa
280000Antarctica
178798Asia
68698Europe
8356North America
7682300Oceania
2736690South America

Question 3. - List the continents with 5 or more than 5 countries.

  • Query:
select continent, count(name) as country_count 
from Country2 
group by continent
having count(name) >= 5;
  • Output
continentcountry_count
Asia5
Europe5

Question 4. - Show the highest population among countries for each continent.

  • Query:
select foo.name, foo.population, foo.continent from
(select max(population) over(partition by continent) as max_pop,
population, name, continent
from Country2) foo
where foo.max_pop = foo.population
  • Output
namepopulationcontinent
Algeria39542166Africa
Antarctica1106Antarctica
Bangladesh156186882Asia
Belgium11323973Europe
Belize347369North America
Australia22751014Oceania
Argentina43431886South America

Question 6. - Display the continents where the average birth rate is higher than 20.

  • Query:
select avg(birth_rate) avg_birth_rate, continent
from Country2
group by continent
having avg(birth_rate) > 20
  • Output
avg_birth_ratecontinent
33.766666Africa
20.786000Asia

Question 7. - List the continents with at least two country having a death rate above 10.

  • Query:
select continent from Country2
where death_rate > 10
group by continent
having count(name)>=1
  • Output
continent
Africa
Asia
Europe

Question 8. - Show the continents with more than 3 countries and a total population greater than 100 million.

  • Query:
select continent,sum(population) sum_pop,count(name) country_count
from Country2
group by continent
having sum(population) > 100000000
and
count(name)>3
  • Output
continentsum_popcountry_count
Asia2029349995

Question 9. - Find the average population growth rate for countries in each continent.

  • Query:
select avg(population_growth) avg_population, continent
from Country2
group by continent
  • Output
avg_populationcontinent
2.466666Africa
NULLAntarctica
1.918000Asia
0.366000Europe
1.112500North America
1.050000Oceania
0.930000South America

Question 10. - Find the continents where the average migration rate is less than 1.

  • Query:
select continent, avg(migration_rate) avg_mig_rate
from Country2
group by continent
having avg(migration_rate)<1
  • Output
continentavg_mig_rate
Africa0.460000
South America0.000000

Question 11. - Query name of countries where birth_rate is less than avg_birth rate of continent.

  • Query:
select a.name,a.birth_rate,
a.avg_birthrate,continent 
from(
    select 
    avg(birth_rate) over(partition by continent)
    avg_birthrate, continent,birth_rate,name
from Country2
) a
where a.birth_rate < a.avg_birthrate
  • Output
namebirth_rateavg_birthratecontinent
Algeria23.6733.766666Africa
Armenia13.6120.786000Asia
Azerbaijan16.6420.786000Asia
Bahrain15.4320.786000Asia
Bangladesh19.6820.786000Asia
Austria9.4110.628000Europe
Andorra8.1310.628000Europe
Aruba12.6716.225000North America
Barbados11.8716.225000North America
Bahamas15.5016.225000North America

Question 12. - Query Countries which have maximum population growth in their continent.

  • Query:
select foo.name, 
foo.Max_pop_growth, 
foo.population_growth,
foo.continent 
from 
(
select max(population_growth) over(partition by continent) Max_pop_growth,
name,continent,
population_growth
from Country2
) foo
where foo.Max_pop_growth = foo.population_growth
  • Output
nameMax_pop_growthpopulation_growthcontinent
Angola2.782.78Africa
Benin2.782.78Africa
Bahrain4.514.51Asia
Belgium0.660.66Europe
Belize1.971.97North America
Australia1.051.05Oceania
Argentina0.930.93South America
Aruba12.6716.225000North America
Barbados11.8716.225000North America
Bahamas15.5016.225000North America

Question 13. - For each country, calculate the percentage of the total population it represents within its continent.

  • Query:
select foo.name,foo.continent,
foo.population,
foo.sum_pop as continent_tot_pop,
(foo.population* 100)/foo.sum_pop  as percentage_by_continent 
from
(select continent,
name,population,
sum(population) over(partition by continent) sum_pop from Country2) foo;
  • Output
namecontinentpopulationcontinent_tot_poppercentage_by_continent
AlgeriaAfrica395421666961616656
AngolaAfrica196253536961616628
BeninAfrica104486476961616615
AntarcticaAntarctica11061106100
ArmeniaAsia30563822029349991
AfghanistanAsia3256434220293499916
AzerbaijanAsia97807802029349994
BahrainAsia13466132029349990
BangladeshAsia15618688220293499976
AustriaEurope86655503269407026
BelarusEurope95896893269407029
BelgiumEurope113239733269407034
AlbaniaEurope3029278326940709
AndorraEurope85580326940700
ArubaNorth America112162107473210
BelizeNorth America347369107473232
BarbadosNorth America290604107473227
BahamasNorth America324597107473230
AustraliaOceania2275101422751014100
ArgentinaSouth America4343188643431886100

Question 14. - Rank the countries in each continent based on their population.

  • Query:
select name,continent,population,
Rank() over(partition by continent order by population) as country_rank 
from Country2;
  • Output
namecontinentpopulationcountry_rankpercentage_by_continent
BeninAfrica10448647156
AngolaAfrica19625353228
AlgeriaAfrica39542166315
AntarcticaAntarctica11061100
BahrainAsia134661311
ArmeniaAsia3056382216
AzerbaijanAsia978078034
AfghanistanAsia3256434240
BangladeshAsia156186882576
AndorraEurope85580126
AlbaniaEurope3029278229
AustriaEurope8665550334
BelarusEurope958968949
BelgiumEurope1132397350
ArubaNorth America112162110
BarbadosNorth America290604232
BahamasNorth America324597327
BelizeNorth America347369430
AustraliaOceania227510141100
ArgentinaSouth America434318861100

Question 15. - Calculate the cumulative population for each continent in ascending order based on their population.

  • Query:
select continent,population,
sum(population) over(partition by continent order by population) sum_pop 
from Country2;
  • Output
continentpopulationsum_pop
Africa1044864710448647
Africa1962535330074000
Africa3954216669616166
Antarctica11061106
Asia13466131346613
Asia30563824402995
Asia978078014183775
Asia3256434246748117
Asia156186882202934999
Europe8558085580
Europe30292783114858
Europe866555011780408
Europe958968921370097
Europe1132397332694070
North America112162112162
North America290604402766
North America324597727363
North America3473691074732
Oceania2275101422751014
South America4343188643431886

Question 16. - Lead Lag Questions.

  • Query:
select name, area,
lag(area) over(order by name) as previous_area,
lead(area) over(order by name) as next_area
from Country2;
  • Output
nameareaprevious_areanext_area
Afghanistan652230NULL28748
Albania287486522302381741
Algeria238174128748468
Andorra46823817411246700
Angola124670046814000000
Antarctica1400000012467002780400
Argentina27804001400000029800
Armenia298002780400180
Aruba180298007692024
Australia769202418083858
Austria83858769202486600
Azerbaijan866008385813880
Bahamas1388086600760
Bahrain76013880147570
Bangladesh147570760430
Barbados430147570207600
Belarus20760043030528
Belgium3052820760022966
Belize2296630528112622
Benin11262222966NULL
0
Subscribe to my newsletter

Read articles from The Analyst Geek directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

The Analyst Geek
The Analyst Geek

I am a data enthusiast who finds joy in working with data, utilizing my skills to extract meaningful and valuable insights from it.