Advanced SQL: Intro To Database Systems Andy Pavlo
Advanced SQL: Intro To Database Systems Andy Pavlo
Advanced SQL: Intro To Database Systems Andy Pavlo
R E L AT I O N A L L A N G UA G E S
S Q L H I S TO R Y
S Q L H I S TO R Y
R E L AT I O N A L L A N G UA G E S
Also includes:
→ View definition
→ Integrity & Referential Constraints
→ Transactions
Aggregations + Group By
String / Date / Time Operations
Output Control + Redirection
Nested Queries
Common Table Expressions
Window Functions
E X A M P L E D ATA B A S E
student(sid,name,login,gpa) enrolled(sid,cid,grade)
sid name login age gpa sid cid grade
53666 Kanye kayne@cs 39 4.0 53666 15-445 C
53688 Bieber jbieber@cs 22 3.9 53688 15-721 A
53655 Tupac shakur@cs 26 3.5 53688 15-826 B
53655 15-445 B
course(cid,name) 53666 15-721 C
cid name
15-445 Database Systems
15-721 Advanced Database Systems
15-826 Data Mining
15-823 Advanced Topics in Databases
A G G R E G AT E S
A G G R E G AT E S
A G G R E G AT E S
A G G R E G AT E S
A G G R E G AT E S
M U LT I P L E A G G R E G AT E S
AVG(gpa) COUNT(sid)
SELECT AVG(gpa), COUNT(sid) 3.25 12
FROM student WHERE login LIKE '%@cs'
D I S T I N C T A G G R E G AT E S
A G G R E G AT E S
G R O U P BY
G R O U P BY
G R O U P BY
G R O U P BY
X
SELECT AVG(s.gpa), e.cid, s.name
FROM enrolled AS e, student AS s
WHERE e.sid = s.sid
GROUP BY e.cid
G R O U P BY
X
SELECT AVG(s.gpa), e.cid, s.name
FROM enrolled AS e, student AS s
WHERE e.sid = s.sid
GROUP BY e.cid,
e.cid s.name
H AV I N G
X
SELECT AVG(s.gpa) AS avg_gpa, e.cid
FROM enrolled AS e, student AS s
WHERE e.sid = s.sid
AND avg_gpa > 3.9
GROUP BY e.cid
H AV I N G
X
SELECT AVG(s.gpa) AS avg_gpa, e.cid
FROM enrolled AS e, student AS s
WHERE e.sid = s.sid
GROUP
AND avg_gpa
BY e.cid> 3.9
GROUP BY
HAVING avg_gpa
e.cid > 3.9;
H AV I N G
X
SELECT AVG(s.gpa) AS avg_gpa, e.cid
FROM enrolled AS e, student AS s
WHERE e.sid = s.sid
GROUP
AND avg_gpa
BY e.cid> 3.9
GROUP BY
HAVING avg_gpa
e.cid > 3.9;
AVG(s.gpa) e.cid
3.75 15-415 avg_gpa e.cid
3.950000 15-721 3.950000 15-721
3.900000 15-826
CMU 15-445/645 (Fall 2019)
24
S T R I N G O P E R AT I O N S
String Case String Quotes
SQL-92 Sensitive Single Only
Postgres Sensitive Single Only
MySQL Insensitive Single/Double
SQLite Sensitive Single/Double
DB2 Sensitive Single Only
Oracle Sensitive Single Only
S T R I N G O P E R AT I O N S
S T R I N G O P E R AT I O N S
S T R I N G O P E R AT I O N S
D AT E / T I M E O P E R AT I O N S
OUTPUT REDIRECTION
OUTPUT REDIRECTION
OUTPUT CONTROL
OUTPUT CONTROL
OUTPUT CONTROL
OUTPUT CONTROL
NESTED QUERIES
NESTED QUERIES
NESTED QUERIES
NESTED QUERIES
NESTED QUERIES
NESTED QUERIES
NESTED QUERIES
NESTED QUERIES
NESTED QUERIES
NESTED QUERIES
X
SELECT MAX(e.sid), s.name
FROM enrolled AS e, student AS s
WHERE e.sid = s.sid;
NESTED QUERIES
NESTED QUERIES
NESTED QUERIES
NESTED QUERIES
NESTED QUERIES
NESTED QUERIES
NESTED QUERIES
NESTED QUERIES
cid name
15-823 Advanced Topics in Databases
NESTED QUERIES
cid name
15-823 Advanced Topics in Databases
WINDOW FUNCTIONS
WINDOW FUNCTIONS
WINDOW FUNCTIONS
Aggregation functions:
→ Anything that we discussed earlier sid cid grade row_num
Special window functions: 53666
53688
15-445
15-721
C
A
1
2
→ ROW_NUMBER()→ # of the current row 53688 15-826 B 3
→ RANK()→ Order position of the current 53655 15-445 B 4
row. 53666 15-721 C 5
WINDOW FUNCTIONS
Aggregation functions:
→ Anything that we discussed earlier sid cid grade row_num
Special window functions: 53666
53688
15-445
15-721
C
A
1
2
→ ROW_NUMBER()→ # of the current row 53688 15-826 B 3
→ RANK()→ Order position of the current 53655 15-445 B 4
row. 53666 15-721 C 5
WINDOW FUNCTIONS
WINDOW FUNCTIONS
WINDOW FUNCTIONS
SELECT *,
ROW_NUMBER() OVER (ORDER BY cid)
FROM enrolled
ORDER BY cid
WINDOW FUNCTIONS
Find the student with the highest grade for each course.
SELECT * FROM (
SELECT *,
RANK() OVER (PARTITION BY cid
ORDER BY grade ASC)
AS rank
FROM enrolled) AS ranking
WHERE ranking.rank = 1
WINDOW FUNCTIONS
Find the student with the highest grade for each course.
WINDOW FUNCTIONS
Find the student with the highest grade for each course.
C O M M O N TA B L E E X P R E S S I O N S
WITH cteName AS (
SELECT 1
)
SELECT * FROM cteName
C O M M O N TA B L E E X P R E S S I O N S
WITH cteName AS (
SELECT 1
)
SELECT * FROM cteName
C O M M O N TA B L E E X P R E S S I O N S
C O M M O N TA B L E E X P R E S S I O N S
C O M M O N TA B L E E X P R E S S I O N S
CTE RECURSION
C O N C LU S I O N
NEXT CLASS
Storage Management