Created
March 23, 2020 14:35
-
-
Save somodiferenc/8267ab72060ef4c3e0436b461dce64ef to your computer and use it in GitHub Desktop.
My solution to SQL practice World Population_II
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- How many entries in the database are from Africa? | |
SELECT COUNT(*) FROM countries | |
WHERE continent = 'Africa'; | |
-- What was the total population of Oceania in 2005? | |
SELECT SUM(population_years.population) | |
FROM population_years JOIN countries | |
ON population_years.country_id = countries.id | |
WHERE population_years.year = 2005 | |
AND countries.continent = 'Oceania'; | |
-- What is the average population of countries in South America in 2003? | |
SELECT AVG(population_years.population) AS 'avg population' | |
FROM population_years JOIN countries | |
ON population_years.country_id = countries.id | |
WHERE population_years.year = 2003 | |
AND countries.continent = 'South America'; | |
-- What country had the smallest population in 2007? | |
SELECT countries.name, population_years.population | |
FROM countries JOIN population_years | |
ON countries.id = population_years.country_id | |
WHERE population_years.year = 2007 | |
ORDER BY population_years.population ASC | |
LIMIT 3; | |
-- What is the average population of Poland during the time period covered by this dataset? | |
SELECT ROUND(AVG(population_years.population),2) | |
AS 'avg pop in Poland' | |
FROM population_years JOIN countries | |
ON population_years.country_id = countries.id | |
WHERE countries.name = 'Poland'; | |
-- How many countries have the word "The" in their name? | |
SELECT COUNT (*) AS 'countries with "The"' | |
FROM countries | |
WHERE name LIKE '%The%'; | |
-- What was the total population of each continent in 2010? | |
SELECT countries.continent, | |
ROUND(SUM(population_years.population),2) AS 'SUM pop in 2010' | |
FROM countries JOIN population_years | |
ON countries.id = population_years.country_id | |
WHERE population_years.year = 2010 | |
GROUP BY countries.continent; |
Hello! Thanks for your feedback! Yes, indeed, this way it is more simple.
Please, find the correction below:
"What country had the smallest population in 2007"
SELECT countries.name, MIN(population_years.population) FROM countries JOIN population_years ON countries.id = population_years.country_id WHERE population_years.year = 2007;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You made a mistake in "What country had the smallest population in 2007". Try using
MIN (population_years.population)
to get the lowest value in the specified year.