Introduction >>>
Scenario world
f35f7fece0cf0019765619c477c900ddd8131dbc27b32e9261f4ab7cdc49773c
See details on the model
In this scenario, we have a single table world in which each row describes a country. The table contains the following attributes:
- name of type VARCHAR(50) NOT NULL, the name of the country (e.g. France)
- continent of type VARCHAR(60), in which continent is the country in (e.g. Europe)
- area of time DECIMAL(10) the area of the country in square kilometer
- population of type DECIMAL(11) the number of inhabitants
- gdp of type DECIMAL(14) the Gross Domestic Product
- capital of type VARCHAR(60) the name of the capital city
- tld of type VARCHAR(5) the Top Level Domain of the country (the last part of the domain names of the country, e.g. .fr for France)
- flag of type VARCHAR(255) an url of the flag of the country
Question
Compute for each country its size rank and its area rank (i.e. the i and j such that it is the i-th most populated and the j-th largest).
OVER() aggregation without GROUP BY
There is a way in SQL to compute some aggregates without aggregation. A typical example is to compute the rank according to some order. To compute such "aggregate" the syntax is:
SELECT agg_fun(col) OVER(PARTITION BY criterion ORDER BY criterion) FROM ...
Here, agg_fun(col) is an aggregation over the column "col" (so either MAX, SUM, etc. or RANK to give the RANK), ORDER BY defines the order in which we add elements in the aggregate and partition gives "boundaries" where we reset the aggregation function.
For instance to compute the order of countries by area within each continent we can use the following:
SELECT name, continent, RANK() OVER(PARTITION BY continent ORDER BY area DESC ) as r
FROM world
WHERE area is NOT NULL
ORDER BY continent, r;
{"rows":[["Russia",1,9],["Canada",2,36],["United States",3,3],["China",4,1],["Brazil",5,5],["Australia",6,50],["India",7,2],["Argentina",8,31],["Kazakhstan",9,60],["Algeria",10,32],["Saudi Arabia",11,42],["Mexico",12,11],["Indonesia",13,4],["Sudan",14,34],["Libya",15,106],["Iran",16,17],["Mongolia",17,134],["Peru",18,40],["Chad",19,69],["Niger",20,61],["Angola",21,57],["Mali",22,66],["South Africa",23,24],["Colombia",24,26],["Ethiopia",25,14],["Bolivia",26,83],["Mauritania",27,128],["Egypt",28,15],["Tanzania",29,29],["Nigeria",30,7],["Venezuela",31,43],["Pakistan",32,6],["Namibia",33,138],["Mozambique",34,49],["Turkey",35,18],["Chile",36,58],["Zambia",37,68],["Myanmar",38,22],["Afghanistan",39,46],["France",40,19],["Somalia",41,78],["Central African Republic",42,117],["South Sudan",43,72],["Ukraine",44,30],["Madagascar",45,53],["Botswana",46,142],["Kenya",47,28],["Yemen",48,47],["Thailand",49,20],["Spain",50,27],["Turkmenistan",51,114],["Cameroon",52,54],["Papua New Guinea",53,97],["Sweden",54,86],["Uzbekistan",55,39],["Morocco",56,38],["Iraq",57,35],["Paraguay",58,101],["Zimbabwe",59,70],["Japan",60,10],["Germany",61,16],["Philippines",62,12],["Congo, Republic of",63,120],["Finland",64,111],["Vietnam",65,13],["Malaysia",66,41],["Norway",67,115],["Poland",68,33],["Oman",69,125],["Italy",70,23],["Ecuador",71,65],["Burkina Faso",72,59],["New Zealand",73,121],["Gabon",74,146],["Guinea",75,77],["United Kingdom",76,21],["Uganda",77,37],["Ghana",78,44],["Romania",79,56],["Laos",80,103],["Guyana",81,157],["Belarus",82,88],["Kyrgyzstan",83,109],["Senegal",84,71],["Syria",85,52],["Cambodia",86,67],["Uruguay",87,130],["Suriname",88,163],["Tunisia",89,76],["Bangladesh",90,8],["Nepal",91,45],["Tajikistan",92,96],["Greece",93,75],["Nicaragua",94,108],["North Korea",95,48],["Malawi",96,63],["Eritrea",97,104],["Benin",98,84],["Honduras",99,92],["Liberia",100,123],["Bulgaria",101,98],["Cuba",102,74],["Guatemala",103,64],["Iceland",104,168],["South Korea",105,25],["Hungary",106,85],["Portugal",107,81],["Jordan",108,102],["Serbia",109,99],["Azerbaijan",110,87],["Austria",111,93],["United Arab Emirates",112,89],["Czech Republic",113,80],["Panama",114,129],["Sierra Leone",115,107],["Ireland",116,119],["Georgia",117,122],["Sri Lanka",118,55],["Lithuania",119,133],["Latvia",120,143],["Togo",121,100],["Croatia",122,124],["Bosnia and Herzegovina",123,126],["Costa Rica",124,118],["Slovakia",125,112],["Dominican Republic",126,90],["Estonia",127,149],["Denmark",128,110],["Netherlands",129,62],["Switzerland",130,95],["Bhutan",131,158],["Taiwan",132,51],["Guinea-Bissau",133,145],["Moldova",134,127],["Belgium",135,73],["Lesotho",136,139],["Armenia",137,132],["Solomon Islands",138,161],["Albania",139,135],["Equatorial Guinea",140,147],["Burundi",141,91],["Haiti",142,82],["Rwanda",143,79],["Macedonia",144,141],["Djibouti",145,154],["Belize",146,167],["El Salvador",147,105],["Israel",148,94],["Slovenia",149,140],["Fiji",150,156],["Kuwait",151,131],["Swaziland",152,150],["Timor-Leste",153,153],["Montenegro",154,160],["Vanuatu",155,171],["Qatar",156,137],["Gambia",157,144],["Jamaica",158,136],["Lebanon",159,116],["Cyprus",160,155],["Brunei",161,166],["Trinidad and Tobago",162,148],["Cape Verde",163,164],["Samoa",164,172],["Luxembourg",165,162],["Mauritius",166,151],["Comoros",167,159],["Kiribati",168,175],["Bahrain",169,152],["Dominica",170,181],["Tonga",171,177],["Singapore",172,113],["Saint Lucia",173,173],["Andorra",174,180],["Palau",175,187],["Seychelles",176,178],["Antigua and Barbuda",177,179],["Barbados",178,170],["Saint Vincent and the Grenadines",179,174],["Grenada",180,176],["Malta",181,165],["Maldives",182,169],["Saint Kitts and Nevis",183,183],["Marshall Islands",184,182],["Liechtenstein",185,184],["San Marino",186,186],["Tuvalu",187,188],["Nauru",188,189],["Monaco",189,185],["Vatican City",190,190]],"fields":[{"name":"name","dataTypeID":1043},{"name":"area_order","dataTypeID":20},{"name":"size_order","dataTypeID":20}],"affectedRows":0}
False
False