linfo2172-databases/P1/queries.sql

114 lines
3.3 KiB
SQL

-- Question 1 --
SELECT name FROM Mountain;
-- Question 2 --
SELECT country FROM CountryCovid WHERE year = 2021 AND month = 3 AND total_cases > 10000;
-- Question 3 --
SELECT province.name AS province, country.name AS country
FROM province
LEFT JOIN Encompasses on Encompasses.country = province.country
LEFT JOIN country on country.code = province.country
WHERE continent = "Europe" and province.area < 200
;
-- Question 4 --
SELECT country.name AS country, country.population, CountryCovid.total_deaths
FROM country
LEFT JOIN Encompasses on Encompasses.country = country.code
LEFT JOIN CountryCovid on CountryCovid.country = country.code and CountryCovid.year = 2022 and CountryCovid.month = 12
WHERE continent = "Europe" and CountryCovid.total_deaths > 10000
;
-- Question 5 --
SELECT name, Independence
FROM Independence
LEFT JOIN Country on Country.code = Independence.country
-- Question 6 --
SELECT Country.code, Country.name
FROM Country
INNER JOIN Borders on country1 = Country.code AND (country2 = "TJ" or country2 = "IND")
GROUP BY code
HAVING COUNT(*) = 2
;
-- Question 7 --
SELECT capital
FROM Country
INNER JOIN GeoMountain ON GeoMountain.country = Country.code
INNER JOIN Mountain ON GeoMountain.mountain = Mountain.name
WHERE mountains = "Alps" AND height > 4000
GROUP BY Country.code
;
-- Question 8 --
SELECT country.code, country.name
FROM country
LEFT JOIN Language on Language.country = country.code
WHERE Language.country IS NULL
;
-- Question 9 --
SELECT DISTINCT mountains
FROM Mountain
INNER JOIN GeoMountain ON GeoMountain.mountain = Mountain.name
GROUP BY mountain.name
HAVING COUNT(DISTINCT GeoMountain.country) >= 2
;
-- Question 10 --
SELECT Country.capital AS name
FROM Country
INNER JOIN CountryCovid ON Country.code = CountryCovid.country
INNER JOIN Province ON Country.capital = Province.name
WHERE CountryCovid.total_deaths > 10000
GROUP BY Country.code
;
-- Question 11 --
SELECT DISTINCT Country.name
FROM Borders AS B
JOIN Borders on B.country2 = Borders.country1 OR B.country1 = Borders.country1
LEFT JOIN Country ON Country.code = Borders.country2
WHERE B.country1 = "USA" AND Borders.country2 != "USA"
;
-- Question 12 --
SELECT COUNT(DISTINCT country) AS cnt
FROM CountryCovid
;
-- Question 13 --
SELECT C1.name AS country1, C2.name AS country2
FROM Borders
LEFT JOIN CountryCovid ON CountryCovid.country = Borders.country1
LEFT JOIN Country AS C1 ON C1.code = Borders.country1
LEFT JOIN Country AS C2 ON C2.code = Borders.country2
WHERE CountryCovid.year = 2021 AND CountryCovid.month = 12 AND CountryCovid.total_cases = 0 OR CountryCovid.total_cases IS NULL
;
-- Question 14 --
SELECT Language.country, Language.name
FROM Language
EXCEPT
SELECT Language.country, Language.name
FROM Language
WHERE EXISTS (SELECT 1 FROM Language AS L2 WHERE L2.country = Language.country AND L2.percentage > Language.percentage)
;
-- Question 15 --
SELECT B.country1 as c1, B.country2 as c2
FROM Borders AS B
LEFT JOIN Borders ON B.country1 = Borders.country2 AND B.country2 = Borders.country1
WHERE Borders.country1 IS NULL OR Borders.country2 IS NULL
;
-- Question 16 --
SELECT Country.name, 100.0-sum(COALESCE(percentage, 0)) AS unknown_lang_p
FROM Country
LEFT JOIN Language ON Language.country = Country.code
GROUP BY Country.code
HAVING sum(percentage) < 99.0 OR sum(percentage) IS NULL
;