03 Advanced SQL Annotateddi
03 Advanced SQL Annotateddi
1 / 76
Advanced SQL
Today’s Agenda
Advanced SQL
1.1 Recap
1.2 Relational Language
1.3 Aggregates
1.4 Grouping
1.5 String and Date/Time Functions
1.6 Output Control
1.7 Nested Queries
1.8 Window Functions
1.9 Common Table Expressions
1.10 Joins
2 / 76
Advanced SQL Recap
Recap
3 / 76
Advanced SQL Recap
Relational Model
4 / 76
Advanced SQL Recap
Core Operators
• These operators take in relations (i.e., tables) as input and return a relation as output.
• We can “chain” operators together to create more complex operations.
• Selection (σ)
• Projection (Π)
• Union (∪)
• Intersection (∩)
• Difference (−)
• Product (×)
• Join (1)
5 / 76
Advanced SQL Relational Language
Relational Language
6 / 76
Advanced SQL Relational Language
Relational Language
• User only needs to specify the answer that they want, not how to compute it.
• The DBMS is responsible for efficient evaluation of the query.
▶ Query optimizer: re-orders operations and generates query plan
7 / 76
Advanced SQL Relational Language
SQL History
8 / 76
Advanced SQL Relational Language
SQL History
9 / 76
Advanced SQL Relational Language
Relational Language
10 / 76
Advanced SQL Relational Language
• Aggregations + Group By
• String / Date / Time Operations
• Output Control + Redirection
• Nested Queries
• Join
• Common Table Expressions
• Window Functions
11 / 76
Advanced SQL Relational Language
Example Database
cid name
1 Computer Architecture
courses 2 Machine Learning
3 Database Systems
4 Programming Languages
12 / 76
Advanced SQL Aggregates
Aggregates
13 / 76
Advanced SQL Aggregates
Aggregates
14 / 76
Advanced SQL Aggregates
Aggregates
CNT
3
15 / 76
Advanced SQL Aggregates
Multiple Aggregates
• Task: Get the number of students and their average GPA that have a "@cs" login.
SELECT AVG(gpa), COUNT(sid)
FROM students WHERE login LIKE '%@cs'
AVG CNT
3.6666 3
16 / 76
Advanced SQL Aggregates
Distinct Aggregates
COUNT
3
17 / 76
Advanced SQL Aggregates
Aggregates
18 / 76
Advanced SQL Aggregates
Aggregates
18 / 76
Advanced SQL Grouping
Grouping
19 / 76
Advanced SQL Grouping
Group By
20 / 76
Advanced SQL Grouping
Group By
21 / 76
Advanced SQL Grouping
Having
22 / 76
Advanced SQL Grouping
Having
e.cid AVG
1 3.8
2 3.8
23 / 76
Advanced SQL String and Date/Time Functions
24 / 76
Advanced SQL String and Date/Time Functions
String Operations
25 / 76
Advanced SQL String and Date/Time Functions
String Operations
26 / 76
Advanced SQL String and Date/Time Functions
String Operations
27 / 76
Advanced SQL String and Date/Time Functions
String Operations
• SQL standard says to use || operator to concatenate two or more strings together.
SQL-92
SELECT name FROM students WHERE login = LOWER(name) || '@cs'
MSSQL
SELECT name FROM students WHERE login = LOWER(name) + '@cs'
MySQL
SELECT name FROM students WHERE login = CONCAT(LOWER(name), '@cs')
28 / 76
Advanced SQL String and Date/Time Functions
Date/Time Operations
29 / 76
Advanced SQL Output Control
Output Control
30 / 76
Advanced SQL Output Control
Output Redirection
31 / 76
Advanced SQL Output Control
Output Redirection
32 / 76
Advanced SQL Output Control
Output Control
sid grade
1 A
4 A
33 / 76
Advanced SQL Output Control
Output Control
34 / 76
Advanced SQL Nested Queries
Nested Queries
35 / 76
Advanced SQL Nested Queries
Nested Queries
36 / 76
Advanced SQL Nested Queries
Nested Queries
37 / 76
Advanced SQL Nested Queries
Nested Queries
38 / 76
Advanced SQL Nested Queries
Nested Queries
name
Maria
Peter
39 / 76
Advanced SQL Nested Queries
Nested Queries
40 / 76
Advanced SQL Nested Queries
Nested Queries
41 / 76
Advanced SQL Nested Queries
Nested Queries
42 / 76
Advanced SQL Nested Queries
Nested Queries
43 / 76
Advanced SQL Nested Queries
Nested Queries
44 / 76
Advanced SQL Nested Queries
Nested Queries
name
Rahul
Shiyi
45 / 76
Advanced SQL Nested Queries
Nested Queries
• Task: Find students record with the highest id that is enrolled in at least one course.
--- Won't work in SQL-92
SELECT MAX(e.sid), s.name
FROM enrolled AS e, students AS s
WHERE e.sid = s.sid;
46 / 76
Advanced SQL Nested Queries
Nested Queries
• Task: Find students record with the highest id that is enrolled in at least one course.
--- "Is greater than every other sid"
SELECT sid, name
FROM students
WHERE ...
--- "Is greater than every other sid"
SELECT sid, name
FROM students
WHERE sid >= ALL(
SELECT sid FROM enrolled
)
sid name
4 Peter
47 / 76
Advanced SQL Nested Queries
Nested Queries
• Task: Find students record with the highest id that is enrolled in at least one course.
SELECT sid, name FROM students
FROM students
WHERE sid IN (
SELECT MAX(sid) FROM enrolled
)
SELECT sid, name FROM students
WHERE sid IN (
SELECT sid FROM enrolled
ORDER BY sid DESC LIMIT 1
)
48 / 76
Advanced SQL Nested Queries
Nested Queries
49 / 76
Advanced SQL Nested Queries
Nested Queries
cid name
4 Peter
50 / 76
Advanced SQL Window Functions
Window Functions
51 / 76
Advanced SQL Window Functions
Window Functions
52 / 76
Advanced SQL Window Functions
Window Functions
Window Functions
• The OVER keyword specifies how to group together tuples when computing the
window function.
• Use PARTITION BY to specify group.
SELECT cid, sid, ROW_NUMBER()
OVER (PARTITION BY cid) --- Note the row numbering
FROM enrolled
ORDER BY cid
cid sid row_number
1 1 1
2 1 1
2 4 2
3 2 1
54 / 76
Advanced SQL Window Functions
Window Functions
• You can also include an ORDER BY in the window grouping to sort entries in each
group.
SELECT cid, sid, ROW_NUMBER()
OVER (ORDER BY cid) --- Note the row numbering
FROM enrolled
ORDER BY cid
cid sid row_number
1 1 1
2 1 2
2 4 3
3 2 4
55 / 76
Advanced SQL Window Functions
Window Functions
• Task: Find the students with the highest grade for each course.
SELECT cid, sid, grade, rank FROM (
SELECT *, RANK() -- Group tuples by cid and then sort by grade
OVER (PARTITION BY cid ORDER BY grade ASC) AS rank
FROM enrolled
) AS ranking
WHERE ranking.rank = 1
56 / 76
Advanced SQL Window Functions
Window Functions
• Task: Get the name of the students with the second highest grade for each course.
SELECT cid, sid, grade, rank FROM (
SELECT *, RANK()
OVER (PARTITION BY cid ORDER BY grade ASC) AS rank
FROM enrolled
) AS ranking
WHERE ranking.rank = 2 --- Update rank
57 / 76
Advanced SQL Window Functions
Window Functions
• Task: Get the name of the students with the second highest grade for each course.
SELECT * FROM (
SELECT C.name, S.name, E.grade, RANK()
OVER (PARTITION BY E.cid ORDER BY E.grade ASC) AS grade_rank
FROM students S, courses C, enrolled E
WHERE S.sid = E.sid AND C.cid = E.cid --- Connect with students
) AS ranking
WHERE ranking.grade_rank = 2
58 / 76
Advanced SQL Common Table Expressions
59 / 76
Advanced SQL Common Table Expressions
60 / 76
Advanced SQL Common Table Expressions
61 / 76
Advanced SQL Common Table Expressions
• Task: Find students record with the highest id that is enrolled in at least one course.
WITH cteSource (maxId) AS (
SELECT MAX(sid) FROM enrolled
)
SELECT name FROM students, cteSource
WHERE students.sid = cteSource.maxId
62 / 76
Advanced SQL Common Table Expressions
63 / 76
Advanced SQL Joins
Joins
64 / 76
Advanced SQL Joins
Types of Join
• Types of Join
▶ (INNER) JOIN (1) −→ Returns records that have matching values in both tables
▶ LEFT OUTER JOIN (=1) −→ Returns all records from the left table, and the matched
records from the right table
▶ RIGHT OUTER JOIN (1<) −→ Returns all records from the right table, and the matched
records from the left table
▶ FULL OUTER JOIN (=1<) −→ Returns all records when there is a match in either left or
right table
65 / 76
Advanced SQL Joins
Example Database
66 / 76
Advanced SQL Joins
name grade
Maria Stars
Maria Climbing
Rahul Coding
67 / 76
Advanced SQL Joins
name grade
Maria Stars
Maria Climbing
Rahul Coding
Peter NULL
Shiyi NULL
68 / 76
Advanced SQL Joins
name grade
Maria Stars
Maria Climbing
Rahul Coding
NULL Rugby
69 / 76
Advanced SQL Joins
name grade
Maria Stars
Maria Climbing
Rahul Coding
NULL Rugby
Peter NULL
Shiyi NULL
70 / 76
Advanced SQL Joins
71 / 76
Advanced SQL Joins
name
Maria
Rahul
72 / 76
Advanced SQL Joins
name
Shiyi
Peter
73 / 76
Advanced SQL Joins
name
Maria
Maria
Rahul
74 / 76
Advanced SQL Joins
Conclusion
75 / 76
Advanced SQL Joins
Next Class
• Storage Management
76 / 76