| Date | 09/19/2022 |
| Topic | SQL Langugage |
| Professor | John DeMastri |
| Week | 3 |
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.
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');
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)
In addition to retrieving data, we often want to perform some computation and summarization:
COUNT([DISTINCT] A); Number of [unique] values in column ASUM([DISTINCT] A); Sum of [unique] values in column AAVG([DISTINCT] A); Average of [unique] values in column AMAX(A); Max of values in column AMIN(A); Min of values in column ASELECT
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 HAVINGSELECT [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;
| Date | 09/21/2022 |
| Topic | SQL Programming |
| Professor | John DeMastri |
| Week | 3 |
Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT
Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC
NUMERIC specifies both precision and scale, DECIMAL specifies scale- precision is at least what the coder specifiesDECIMAL is implemented as NUMERIC DECIMAL(p, s), NUMERIC(p, s)Decimal curr(10, 2) = 5.05Floating-Point Types (Approximate Value) - FLOAT, DOUBLE
FLOAT - 4 bytes of storageDOUBLE - 8 bytes of storageFLOAT(p) to specify precision(but not exponent range.FLOAT(m, d) or DOUBLE(m, d) to approximate how many places after the decimal are to be storedBit-Value Type - BIT
BIT data type is used to store bit values. A type of BIT(M) enables storage of M-bit values. M can range from 1 to 64.Date and Time Data Types
DATE, DATETIME, and TIMESTAMP typesTIME Type
YEAR Type
String Data Types
GROUP BY and HAVING recapHAVING applies to the selection after the grouping, whereas WHERE applies before the grouping.
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'
);
NOT IN can be used anywhere IN is usedselect 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;
order by [element] [desc/(default ascending)]
Can use NOT EXISTS
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;
| Date | 09/23/2022 |
| Topic | SQL Examples |
| Professor | John DeMastri |
| Week | 3 |
(Lecture was a discussion with Q&A)