SQL: The Query Language
Administrivia
Relational Query Languages
The SQL Query Language
The SQL DML
Querying Multiple Relations
Basic SQL Query
Query Semantics
Cross Product
Step 2) Discard tuples that fail predicate
Step 3) Discard Unwanted Columns
Now the Details
Example Schemas (in SQL DDL)
Another Join Query
Some Notes on Range Variables
More Notes
Find sailors who’ve reserved at least one boat
Expressions
String operations
Find sid’s of sailors who’ve reserved a red or a green boat
Find sid’s of sailors who’ve reserved a red and a green boat
AND Continued…
Nested Queries
Nested Queries with Correlation
More on Set-Comparison Operators
Rewriting INTERSECT Queries Using IN
Division in SQL
Basic SQL Queries - Summary
Aggregate Operators
Aggregate Operators (continued)
Find name and age of the oldest sailor(s)
GROUP BY and HAVING
Queries With GROUP BY
Group By Examples
Conceptual Evaluation
Find the number of reservations for each red boat.
Slide 45
Queries With GROUP BY and HAVING
Slide 47
Find the age of the youngest sailor with age 18, for each rating with at least 2 such sailors
Slide 49
Slide 50
SELECT S.name, S.sid FROM Sailors S, reserves R WHERE S.sid = r.sid GROUP BY S.name, S.sid HAVING COUNT(DISTINCT R.bid) = Select COUNT (*) FROM Boats
INSERT
DELETE & UPDATE
Null Values
Joins
Inner Join
SELECT s.sid, s.name, r.bid FROM Sailors s INNER JOIN Reserves r ON s.sid = r.sid
Left Outer Join
SELECT s.sid, s.name, r.bid FROM Sailors s LEFT OUTER JOIN Reserves r ON s.sid = r.sid
Right Outer Join
SELECT r.sid, b.bid, b.name FROM Reserves r RIGHT OUTER JOIN Boats b ON r.bid = b.bid
Full Outer Join
SELECT r.sid, b.bid, b.name FROM Reserves r FULL OUTER JOIN Boats b ON r.bid = b.bid
Views
Slide 65