SQL Queries
SQL Queries
sql
---------------------------------------------------------------
-- SQL queries using one relation
-- Return all the attributes for cities with population over 1 million in the US
SELECT *
FROM City
WHERE Population >= '1000000' AND CountryCode = 'USA';
-- This query returns only the name of these cities, and renames the attribute
-- Name to LargeUSACity
SELECT Name AS LargeUSACity
FROM City
WHERE Population >= '1000000' AND CountryCode = 'USA';
-- If we want to get a float value, and not the rounded integer value
SELECT Name, ((Population * 1.0)/ 1000000) AS PopulationInMillion
FROM City
WHERE Population >= '5000000';
-- Find all forms of goverment (the below query IS NOT CORRECT !!)
SELECT GovernmentForm
FROM Country ;
-- Hmmm, the above query keeps multiple copies of the same value.
-- Add the DISTINCT keyword to remove all the duplicates
SELECT DISTINCT GovernmentForm
FROM Country ;
-- Instead the correct way is to filter out the NULL values by using NOT NULL
SELECT Name, IndepYear
FROM Country
WHERE IndepYear NOT NULL
ORDER BY IndepYear ASC
LIMIT 1;
---------------------------------------------------------------
-- Multi-relational queries
-- Countries that have population more than 100 million and do not
-- speak English: here we use the set operator EXCEPT
SELECT C.Name
FROM Country C
WHERE C.Population >= 100000000
EXCEPT
SELECT C.Name
FROM Country C, CountryLanguage L
WHERE C.Code = L.CountryCode
AND L.Language = 'English' ;
--------------------------------------------------------
-- Nested queries
-- Find all countries in Europe that have *some* city with population
-- more than 5 million
SELECT C.Name
FROM Country C
WHERE C.Continent = 'Europe'
AND C.Code IN (SELECT CountryCode
FROM City
WHERE Population > 5000000);
-- alternatively:
SELECT C.Name
FROM Country C
WHERE C.Continent = 'Europe'
AND EXISTS (SELECT *
FROM City T
WHERE T.Population > 5000000
AND T.CountryCode = C.Code) ;
-- Find all countries in Europe that have all cities with population
-- less than 1 million
SELECT C.Name
FROM Country C
WHERE C.Continent = 'Europe'
AND NOT EXISTS (SELECT * FROM City T
WHERE T.Population > 1000000
AND T.CountryCode = C.Code) ;
-----------------------------------------------------
-- Aggregate queries
-- Or max population:
SELECT MAX(Population)
FROM Country
WHERE Continent = 'Europe';
-- Count the number of languages (this will NOT give the correct answer!!!)
SELECT COUNT(Language)
FROM CountryLanguage ;
-- Instead use DISTINCT to eliminate duplicates:
SELECT COUNT(DISTINCT Language)
FROM CountryLanguage ;
-- Find the name and population of the country with the max population in Europe!
-- The example below is not correct! Why?
SELECT Name, MAX(Population)
FROM Country
WHERE Continent = 'Europe';
-- Find how many countries speak each language (with percentage > 50%) and
-- output them in decreasing order!
SELECT Language, COUNT(CountryCode) AS N
FROM CountryLanguage
WHERE Percentage >= 50
GROUP BY Language
ORDER BY N DESC ;
----------------------------------------------------------------
-- NULL behavior
SELECT COUNT(*)
FROM Country
WHERE IndepYear > 1990 OR IndepYear <= 1990 ;
-- Returns max population of a city for each country, and for countries
-- without any city returns NULL
SELECT C.Name AS Country, MAX(T.Population) AS N
FROM Country C LEFT OUTER JOIN City T ON C.Code = T.CountryCode
GROUP BY C.Name
ORDER BY N DESC ;
------------------------------------------------
-- DB Modifications
------------------------------------------------
-- Views
-- Let's create a view that stores the name, and the official languages for each
country
CREATE VIEW OfficialCountryLanguage AS
SELECT C.Name AS CountryName, L.Language AS Language
FROM CountryLanguage L, Country C
WHERE L.CountryCode = C.Code
AND L.IsOfficial = 'T' ;
-- Now we can use this view! Find the countries with more than one offficial
language!
SELECT CountryName, COUNT(Language)
FROM OfficialCountryLanguage
GROUP BY CountryName
HAVING COUNT(Language) > 1 ;