SQL4 Answers
SQL4 Answers
Q1: What is the country code of the country with name 'Canada'?
Q2: Find the name and population of the country with code "ALB"?
Q4:Find the name and population of the countries of "Africa" with population more
than 60000000?
Q5: How many different languages are spoken in Australia, the code of Australia is
AUS?
SubQuery:
select count(language) from countryLanguage
where countryCode=
(
select code from country
where name='Canada'
)
Q7: Find the name and the population of the capital of the country with code "LKA"?
SubQuery:
select name, population from city
where ID =
( select capital from country
where code = 'LKA'
)
join:
SELECT CI.name, CI.population
FROM country CO inner join city CI ON CO.capital=CI.id
where code='LKA';
Q9: For each continent, find the number of countries and the average population?
Q10: Find how many countries have "English" as their official language?
The schema of the result should be (numofcountries).
Q11: What is the country with the largest number of spoken languages?
Q12: List all countries in Europe, Oceania, and Antarctica, sorted by continent?
Assigment
SubQuery:
select Language from countrylanguage
where countryCode =
(
select code from country
where name='Italy'
)
order by percentage desc
limit 1,1;
Q14: Find the most popular official languages in the world.
Report the 5 most popular languages and the number of countries where the language
is official in
a descending order of popularity?
The schema of the result should be (countrylanguage, numofcountries).
Assigment
Q15: Find the languages that are spoken by most people in the world
along with the number of speakers.
Report only the languages that are spoken by more than 50M people in a descending
order of popularity?
The schema of the result should be (countrylanguage, numofpeople).
Join:
select language as countrylanguage, sum(population*percentage/100) as
numofpeople
from country inner join countrylanguage
on code = countryCode
group by language
having numofpeople> 50000000
order by numofpeople desc;
Q16: List the names of all countries where German is spoken as an official language
Join:
select name, percentage
from country inner join countrylanguage
on code=countryCode
where language='German' And IsOfficial= 'T';
SELECT name
FROM country
WHERE name LIKE 'A%' OR name LIKE 'B%';
Q19: Display countries names, cities names, along with their official language?
Join:
select CO.name as CountryName, CI.name as CityName, CL.language
from country CO, city CI, countryLanguage CL
where CO.code=CI.countryCode And CO.code=CL.countrycode And IsOfficial='T';
OR:
select CO.name, CI.name, CL.language
from country CO inner join city CI
on CO.code=CI.countryCode
inner join countryLanguage CL
on CO.code=CL.countrycode
where IsOfficial='T';
Q20: Which countries have the densest population (population divided by surface
area)?
Show the densest at the top.