Skip to content

Instantly share code, notes, and snippets.

@PolyannaMeira
Last active June 21, 2024 09:38
Show Gist options
  • Save PolyannaMeira/31e584510accc8a8026df8faa037d8d0 to your computer and use it in GitHub Desktop.
Save PolyannaMeira/31e584510accc8a8026df8faa037d8d0 to your computer and use it in GitHub Desktop.
-- 1. What are the names of countries with population greater than 8 million?
SELECT Name
FROM country
WHERE Population > 8000000;
-- 2. What are the names of countries that have “land” in their names?
SELECT Name
FROM country
WHERE name LIKE '%land%';
-- 3. What are the names of the cities with population in between 500,000 and 1 million?
SELECT name
from city
where Population between 500000 and 1000000;
-- 4. What's the name of all the countries on the continent ‘Europe’?
SELECT name
FROM country
WHERE Continent= 'Europe';
-- 5. What are the names of all the cities in the Netherlands?
SELECT name
from city
where CountryCode = 'NLD';
-- 6. What is the population of Rotterdam?
SELECT population
from city
where name='Rotterdam';
-- 7. Which countries don't have a head of state? Hint: looks for NULL and '' values
SELECT Name, HeadOfState
from country
WHERE HeadOfState is NULL or '';
-- 8. What's the top 10 least populated cities? Return the name and population
SELECT name, population
from city
order by Population ASC LIMIT 10;
-- 9. What countries in Africa have the local name the same as their common name?
SELECT Name, LocalName
FROM country
WHERE Continent = 'Africa' AND Name = LocalName;
-- 10. What countries have Spanish as official language? Hint: see countrylanguage table
SELECT name
FROM country
JOIN countrylanguage on country.Code = countrylanguage.CountryCode
WHERE LANGUAGE='spanish' and IsOfficial ='t' ;
-- 11. What countries have official languages spoken between 1% and 10% of the population?
SELECT country.name
FROM country
JOIN countrylanguage ON country.code = countrylanguage.countrycode
WHERE countrylanguage.isofficial = 'T' AND countrylanguage.percentage BETWEEN 1 AND 10;
-- 12. What languages are spoken by over 90% of the population of a country? Return just the language names, but don't repeat entries
SELECT DISTINCT cl.Language
FROM countrylanguage cl
WHERE cl.Percentage > 90;
-- 13. In which countries is 'Creole English' used? Order by descending percentage of speakers
SELECT cl.CountryCode, c.Name, cl.Percentage
FROM countrylanguage cl
JOIN country c ON cl.CountryCode = c.Code
WHERE cl.Language = 'Creole English'
ORDER BY cl.Percentage DESC;
-- 14. What are the 5 oldest countries (by independence date) with some form of republic government? Tip: there are multiple types of republic
SELECT name, IndepYear, GovernmentForm
FROM country c
WHERE GovernmentForm LIKE '%republic%'
ORDER BY IndepYear
LIMIT 5;
-- 15. For each country, how many people speak each language? Important: we want absolute values, not a percentage. Return the name of the country, the name of the language, and number of speakers of that language - Hint: you need both the country and countrylanguage tables - Hint: you can do calculations between columns, for example (SELECT a - b from table;)
SELECT c.Name, cl.Language, ROUND((cl.Percentage / 100) * c.Population) AS NumberOfSpeakers
FROM country c
JOIN countrylanguage cl ON c.Code = cl.CountryCode;
-- BONUS
-- 1. What is the total population of the world?
SELECT SUM(Population) AS TotalWorldPopulation
FROM country;
-- 2. What is the average population of countries in Europe?
SELECT AVG(Population) AS AveragePopulation
FROM country
WHERE Continent = 'Europe';
-- 3. How many *official* languages are spoken in Belgium (`BEL`)? Return the country code, and the number of languages as "Number of Languages"
SELECT countrycode, COUNT(language) AS number_of_languages
FROM countrylanguage
WHERE countrycode = 'BEL' AND isofficial = 'T'
GROUP BY countrycode;
-- MORE
-- 1. Retrieve the names of all the cities along with their respective country names, make sure the column names are easy to understand.
SELECT city.name AS city,country.Name AS country
FROM city
JOIN country ON city.CountryCode =country.Code ;
-- 2. Find the average life expectancy of countries in the continent 'Europe'.
SELECT AVG(LifeExpectancy)
FROM country
WHERE Continent = "Europe";
-- 3. Get the names and populations of cities in the district 'California'.
SELECT name, Population
FROM city
WHERE District = 'California';
-- 4. Retrieve the capital city of each country along with the country name.
SELECT country.name AS country_name, city.name AS capital_city
FROM country
JOIN city ON country.capital = city.id;
-- 5. Find the largest city by population in each country.
-- Tip: It is possible to use nested queries, for example, if you wanted to get all the countries which population
-- is greater than Spain's you would do:
-- SELECT Name FROM country WHERE Population > (SELECT Population FROM country WHERE Name = 'Spain')
SELECT country.name AS country_name, city.name AS city_name, city.population
FROM city
JOIN country ON city.CountryCode = country.Code
WHERE city.population = (SELECT MAX(population) FROM city WHERE CountryCode = city.CountryCode);
-- 6. List the names of all cities with a population of over 1 million in the continent of 'Asia'.
SELECT city.Name
FROM city
JOIN country
WHERE city.CountryCode =country.Code AND country.Continent ='Asia' and city.Population >1000000;
-- 7. Get the names and continents of countries that do not have an official language recorded in the database.
SELECT country.Name ,country.Continent
FROM country
Left JOIN countrylanguage
ON country.code = countrylanguage.countrycode AND countrylanguage.isofficial = 'T'
WHERE countrylanguage.countrycode IS NULL AND countrylanguage.Language IS NULL;
-- 8. List the countries in the 'Oceania' continent with an average life expectancy over 70.
SELECT name
FROM country
WHERE Continent = "oceania" ANd LifeExpectancy > 70;
-- 9. Find the total number of languages spoken in the continent 'Africa'.
SELECT COUNT(countrylanguage.Language) AS "Total number of Languages in Africa"
From country
LEFT JOIN countrylanguage
ON country.code = countrylanguage.countrycode
WHERE country.Continent ='Africa' ;
-- 10. Retrieve the names and populations of cities located in countries with a surface area greater than 1 million square kilometers.
SELECT city.name AS city_name, city.population
FROM city
JOIN country ON city.CountryCode = country.code
WHERE country.surfaceArea > 1000000;
-- 11. Retrieve the names and populations of capital cities with populations over 500,000.
SELECT city.name, country.population
FROM city
JOIN country ON country.code = city.countrycode
WHERE country.population > 500000;
-- 12. List the names and continents of countries where English is an official language.
SELECT country.Name ,country.Continent
FROM country
JOIN countrylanguage
ON country.code = countrylanguage.countrycode AND countrylanguage.isofficial = 'T'
WHERE countrylanguage.`Language` ="English";
-- 13. Find the name of the country with the highest life expectancy.
SELECT name
From country
ORDER by LifeExpectancy DESC limit 1;
-- 14. Get the names of countries in the 'South America' continent with cities having a population over 1 million.
SELECT country.Name
FROM country
JOIN city
ON country.code = city.CountryCode
WHERE country.Continent="South America" AND city.Population >1000000
-- 15. Find the name and population of the smallest city (by population) in the country 'India'.
SELECT city.Name ,city.Population
FROM country
JOIN city
ON country.code = city.CountryCode
WHERE country.Name ="India" ORDER BY city.Population ASC LIMIT 1 ;
-- 16. Retrieve the country name and its corresponding capital city's name where the capital's population is more than 1 million.
SELECT country.Name ,city.Name
FROM country
JOIN city
ON country.code = city.CountryCode
WHERE city.Population >1000000 ;
-- 17. List the names of countries that have no cities in the database.
SELECT country.Name
FROM country
Left JOIN city
ON country.code =city.countrycode
WHERE city.Name IS NULL;
-- Um LEFT JOIN retorna todos os registros da tabela à esquerda (primeira tabela mencionada após o FROM),
mesmo que não haja correspondências na tabela à direita (segunda tabela mencionada após o LEFT JOIN).
-- 18. Get the name and population of the largest city in the continent 'South America'.
SELECT city.Name,city.Population
FROM city
JOIN country
ON city.countrycode= country.code
WHERE country.Continent ="South America" ORDER by city.Population DESC limit 1 ;
-- 19. List the names and populations of all cities in countries where the official language is 'Spanish'.
SELECT city.Name,city.Population
FROM city
JOIN country ON city.countrycode= country.code
JOIN countrylanguage ON city.countrycode= countrylanguage.CountryCode
WHERE countrylanguage.`Language` ="Spanish" AND countrylanguage.IsOfficial ="T";
-- 20. Get the name of the country and the population of the city with the highest population in that country.
SELECT country.Name, city.Population
FROM country
JOIN city
ON country.code= city.countrycode
ORDER by city.Population DESC limit 1 ;
@sebdesalvador
Copy link

-- 7. Which countries don't have a head of state? Hint: looks for NULL and '' values
SELECT Name  
from country 
WHERE HeadOfState = 'NULL' or '';

= 'NULL' does not exist in SQL :)

@sebdesalvador
Copy link

-- 10. What countries have Spanish as official language? Hint: see countrylanguage table
SELECT CountryCode 
FROM countrylanguage 
WHERE LANGUAGE='spanish' and IsOfficial ='t' ;

Can you think of a way to get the actual name instead of the country codes?

@sebdesalvador
Copy link

Question 8: The query orders by descending population, which returns the most populated cities instead of the least populated ones.
Question 13: The query should join with the country table to get the country names.
Question 7 (More): The query should check countrylanguage.Language IS NULL to identify countries without any official language records.

@PolyannaMeira
Copy link
Author

Hello Sebastian, thank you very much for your help. I will fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment