SQL4 Answers

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

use World ;

Q1: What is the country code of the country with name 'Canada'?

SELECT code FROM country WHERE name='Canada';

Q2: Find the name and population of the country with code "ALB"?

SELECT name, population FROM country WHERE code='ALB';

Q3: Find official languages of Bolivia, the code of Boliviais "BOL"?

SELECT language FROM countryLanguage


WHERE countryCode='BOL' And IsOfficial=true;

Q4:Find the name and population of the countries of "Africa" with population more
than 60000000?

select name,population from country


where continent='Africa' And population>60000000;

Q5: How many different languages are spoken in Australia, the code of Australia is
AUS?

SELECT count(language) FROM countrylanguage WHERE countrycode='AUS';

Q6: How many different languages are spoken in Canada?

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"?

can we do this using join but it is preferable to use SubQuery.

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';

Q8: Find the number of countries in the world?

SELECT count(*) FROM Country;

Q9: For each continent, find the number of countries and the average population?

SELECT continent, count(*), avg(population) FROM Country


group by continent;

Q10: Find how many countries have "English" as their official language?
The schema of the result should be (numofcountries).

select count(*) as numofcountries from countrylanguage


where language='English' And IsOfficial=true;

Q11: What is the country with the largest number of spoken languages?

select countrycode,count(*)as NumOfLang from countrylanguage


group by countrycode
ORDER BY NumOfLang DESC
LIMIT 1;

Q12: List all countries in Europe, Oceania, and Antarctica, sorted by continent?

Assigment

Q13: What is the second most popular language spoken in Italy?

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

and the percentage of use of this language?

Join:
select name, percentage
from country inner join countrylanguage
on code=countryCode
where language='German' And IsOfficial= 'T';

Q17: List the countries whose names start with either A or B?

SELECT name
FROM country
WHERE name LIKE 'A%' OR name LIKE 'B%';

Q18: Display countries names with number of non-official languages.


Join:
Assigment

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.

SELECT name, population, surfacearea, population / surfacearea as


population_density
FROM country
ORDER BY population_density DESC
limit 1;

You might also like