Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created April 2, 2020 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/f27b5dcd6e13b86f8caa8e9b00012259 to your computer and use it in GitHub Desktop.
Save codecademydev/f27b5dcd6e13b86f8caa8e9b00012259 to your computer and use it in GitHub Desktop.
Codecademy export
--Table overview
select *
from countries
limit 10;
select *
from population_years
limit 10;
--Number of countries in Africa
select count(*)
from countries
where continent = 'Africa';
--Population of Oceania
with oceania_countries as (
select id, name
from countries
where continent = 'Oceania'
)
select round(avg(population_years.population), 2) as 'Average Oceania Population in 2005 (in millions)'
from population_years
join oceania_countries
where population_years.country_id = oceania_countries.id
and year = 2005;
--Average population of South American countries in 2003
select round(avg(population_years.population), 2)
from countries
join population_years
on countries.id = population_years.country_id
where countries.continent = 'South America'
and population_years.year = 2003;
--Smallest population country in 2007
select countries.name, min(population_years.population) as 'Country with smallest population in 2007'
from countries
join population_years
on countries.id = population_years.country_id
where population_years.year = 2007;
--Average population of Poland during all years
select round(avg(population_years.population), 2) as 'Average population of Poland 2000-2010 (in millions)'
from countries
join population_years
on countries.id = population_years.country_id
where countries.name = 'Poland';
--Number of countries with "The" in the name
select count(name)
from countries
where name like '% The %' or name like '% The' or name like 'The %';
--Total population of each continent in 2010
select countries.continent, sum(population_years.population) as 'population in 2010'
from countries
join population_years
on countries.id = population_years.country_id
where population_years.year = 2010
group by continent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment