Week 3 Notes


SQL Language

Date 09/19/2022
Topic SQL Langugage
Professor John DeMastri
Week 3

Review

SELECT Name Country, 
IF(INSTR(Region, 'Central Americal'), Region, Continent) AS Area,
TRUNCATE( SurfaceArea*(0.62137*0.62137), 2) 
AS 'Area in Miles Squared'
FROM Country 
WHERE Continent LIKE '%America%';

INSTR(str, substr) - Returns the position of the first occurrence of substring substr in string str. Like Locate() except arguments are reversed.

Unions

select Name Country, IF(INSTR(Region, 'Central America'), Region, Continent) as Area,
truncate( SurfaceArea*(0.62*0.62), 2) as 'Area in Miles Squared' from Country 
where name in ('United States', 'Canada');

or

select Name Country, IF(INSTR(Region, 'Central America'), Region, Continent) as Area,
truncate( SurfaceArea*(0.62*0.62), 2) as 'Area in Miles Squared' from Country 
where (name = 'United States', name = 'Canada');

or

select Name Country, IF(INSTR(Region, 'Central America'), Region, Continent) as Area,
truncate( SurfaceArea*(0.62*0.62), 2) as 'Area in Miles Squared' from Country 
where (name = 'Canada');

UNION

select Name Country, IF(INSTR(Region, 'Central America'), Region, Continent) as Area,
truncate( SurfaceArea*(0.62*0.62), 2) as 'Area in Miles Squared' from Country 
where (name = 'United States');

Intersect

Only returns rows that matches:

SELECT p.name, p.age
FROM People p
INTERSECT
SELECT s.name, s.age
FROM Staff s

Note: the above statement is not supported by MySQL, because INTERSECT is not allowed

SELECT p.name, p.age
	FROM People p
	INNER JOIN Staff s
	ON s.name=p.name

Embedded query

SELECT name, age FROM People 
	WHERE name IN (SELECT name from Staff)

Aggregate operators

In addition to retrieving data, we often want to perform some computation and summarization:

SELECT 
	MAX(LifeExpectancy) max, 
	MIN(LifeExpectancy) min, 
	AVG(LifeExpectancy) avg
	FROM Country

Count how many cities have population greater than 1M:

SELECT COUNT(*) FROM City WHERE Population>1000000;

GROUP BY and HAVING

SELECT [DISTINCT] select-list
FROM from-list
WHERE qualification
GROUP BY group-list
HAVING group-qualification;
SELECT cl.Language, AVG(LifeExpectancy) AS 'Avg Life Expectancy'
FROM country c, countrylanguage cl
WHERE c.code=cl.CountryCode
AND cl.isOfficial='T'
GROUP BY cl.language; 

Using HAVING

SELECT cl.Language, AVG(LifeExpectancy) AS 'Avg Life Expectancy', count(*) n
FROM country c, countrylanguage cl
WHERE c.code=cl.CountryCode
AND cl.isOfficial='T'
GROUP BY cl.language
HAVING n>2;

SQL Programming

Date 09/21/2022
Topic SQL Programming
Professor John DeMastri
Week 3

Data Types

Primary Data Types

GROUP BY and HAVING recap

HAVING applies to the selection after the grouping, whereas WHERE applies before the grouping.

Nested Queries

Uncorrelated Nest Queries (inside out)

Find surface area for countries where language is English

select c.name, c.SurfaceArea from country c
where c.code IN (
	select cl.CountryCode from CountryLanguage cl where Language='English'
);

Uncorrelated Nest Queries (outside in)

select c.name, c.SurfaceArea from country c
where EXISTS (
	select * from CountryLanguage cl where c.code=c.CountryCode and Language='English'
);
select c.region, sum(c.SurfaceArea) as sa 
from country c
where EXISTS (
select * from CountryLanguage cl
where c.code=clCountryCode
and Language='English'
)
group by c.region
order by sa desc;
select c.region, sum(c.SurfaceArea) as sa 
from country c
where NOT EXISTS (
select * from CountryLanguage cl
where c.code=clCountryCode
and Language='English'
)
group by c.region
order by sa desc;

SQL Examples

Date 09/23/2022
Topic SQL Examples
Professor John DeMastri
Week 3

(Lecture was a discussion with Q&A)