




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
SQL, Structured Query Language, Brief History, Creating And Dropping Tables, SFW Statement, Reading a Table, Selection And Projection, Join, Rename, Set Versus Bag Semantics, Efficiency, Operational Semantics of SFW, Forcing Set Semantics, Union, Except, Intersect, Unioun All, Except All, Intersect All, Table Expression, Scalar Subqueries, IN Subqueries, Exists Subqueries, Operational Semantics of Subqueries, Scoping Rule of Subqueries, Quantified Subqueries, Aggregates, Aggregates with Distinc
Typology: Slides
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Sample solution will be available next Tuesday
Optional; “replaces” your lowest homework grade Two slots: last lecture before midterm and last lecture before final Three students per slot
3
Pronounced “S-Q-L” or “sequel” The standard query language support by most commercial DBMS
IBM System R ANSI SQL ANSI SQL92 (SQL2) SQL3 (still under construction after years!)
4
CREATE TABLE table_name ( … , column_namei column_typei , … ); DROP TABLE table_name ; Examples create table Student (SID integer, name varchar(30), email varchar(30), age integer, GPA float); create table Course (CID char(10), title varchar(100)); create table Enroll (SID integer, CID char(10)); drop table Student; drop table Course; drop table Enroll; -- lines starting with -- are ignored. -- SQL is insensitive to white space. -- SQL is case insensitive; writing ...Course... is equivalent to -- writing ...COURSE...
5
6
Single-table query, so no cross product here WHERE clause is optional * is a short hand for “all columns”
SELECT name FROM Student WHERE age < 18;
SELECT 2004 – age FROM Student WHERE name = ’Lisa’; SELECT list can contain expressions
SELECT Student.SID, Student.name FROM Student, Enroll, Course WHERE Student.SID = Enroll.SID AND Enroll.CID = Course.CID AND title LIKE ’%Database%’; LIKE matches a string against a pattern
9
Relational algebra query: π e 1. SID , e 2. SID ( ρ e 1 Enroll e 1. CID = e 2. CID ∧ e 1. SID > e 2. SID ρ e 2 Enroll ) SQL: SELECT e1.SID AS SID1, e2.SID AS SID FROM Enroll AS e1, Enroll AS e WHERE e1.CID = e2.CID AND e1.SID > e2.SID; AS keyword is completely optional
10
FROM Student sb, Student sl, Enroll eb, Enroll el, Course c WHERE sb.name = ’Bart’ AND sl.name = ’Lisa’ AND eb.SID = sb.SID AND el.SID = sl.SID AND eb.CID = el.CID AND eb.CID = c.CID;
SELECT c.title
Tip: Write the FROM clause first, then WHERE, and then SELECT
11
A large number of queries can be written using only selection, projection, and cross product (or join) Any query that uses only these operators can be written in a canonical form: π L (σ p ( R 1 × … × R (^) m ))
12
No duplicates Relational model and algebra use set semantics
Duplicates allowed Number of duplicates is significant SQL uses bag semantics by default
(SELECT SID FROM ClubMember) EXCEPT (SELECT SID FROM Enroll);
21
In set and bag operations, FROM clauses, etc. A way to “nest” queries
(SELECT SID FROM ClubMember) EXCEPT ALL (SELECT SID FROM Enroll)
SELECT DISTINCT name FROM Student, ( ) AS S WHERE Student.SID = S.SID;
22
SELECT * FROM Student WHERE age = (
);
SELECT age FROM Student WHERE name = ’Bart’
What’s Bart’s age?
23
SELECT * FROM Student WHERE age IN (
);
SELECT age FROM Student WHERE name = ’Bart’
What’s Bart’s age?
24
SELECT * FROM Student AS s WHERE EXISTS (SELECT * FROM Student WHERE name = ’Bart’ AND age = s.age); It is a correlated subquery—a subquery that references tuple variables in surrounding queries
SELECT * FROM Student AS s WHERE EXISTS (SELECT * FROM Student WHERE name = ’Bart’ AND age = s.age);
For each row s in Student Evaluate the subquery with the appropriate value of s.age If the result of the subquery is not empty, output s.*
The DBMS query optimizer may choose to process the query in an equivalent, but more efficient way (example?)
Start with the immediately surrounding query If not found, look in the one surrounding that; repeat if necessary
27
28
A quantified subquery can be used as a value in a WHERE condition Universal quantification (for all): … WHERE x op ALL ( subquery ) … True iff for all t in the result of subquery , x op t Existential quantification (exists): … WHERE x op ANY ( subquery ) … True iff there exists some t in the result of subquery such that x op t )Beware
29
FROM Student WHERE GPA >= ALL (SELECT GPA FROM Student); SELECT * FROM Student WHERE NOT (GPA < ANY (SELECT GPA FROM Student); )Use NOT to negate a condition
30
FROM Student AS s WHERE NOT EXISTS (SELECT * FROM Student WHERE GPA > s.GPA); SELECT * FROM Student WHERE SID NOT IN (SELECT s1.SID FROM Student AS s1, Student AS s WHERE s1.GPA < s2.GPA);
SELECT AVG(GPA) FROM Student;
SID name age GPA 142 Bart 10 2. 857 Lisa 8 4. 123 Milhouse 10 3. 456 Ralph 8 2. ... ... ... ...
SID name age GPA 142 Bart 10 2. 857 Lisa 8 4. 123 Milhouse 10 3. 456 Ralph 8 2. ... ... ... ...
Group all rows into one group AVG_GPA 3
Compute aggregate over the group
Aggregated, or A GROUP BY column
39
SELECT SID, age FROM Student GROUP BY age;
Recall there is one output row per group There can be multiple SID values per group
SELECT SID, MAX(GPA) FROM Student;
Recall there is only one group for an aggregate query with no GROUP BY clause There can be multiple SID values Wishful thinking (that the output SID value is the one associated with the highest GPA) does NOT work
40
Compute FROM (×) Compute WHERE (σ) Compute GROUP BY: group rows according to the values of GROUP BY columns Compute HAVING (another σ over the groups) Compute SELECT for each group that passes the HAVING condition (π)
41
Find the average GPA for each age group over 10 SELECT age, AVG(GPA) FROM Student GROUP BY age HAVING age > 10; Can be written using WHERE without table expressions List the average GPA for each age group with more than a hundred students SELECT age, AVG(GPA) FROM Student GROUP BY age HAVING COUNT(*) > 100; Can be written using WHERE and table expressions
42
More expressive power than relational algebra
After SELECT list has been computed and optional duplicate elimination has been carried out, sort the output according to ORDER BY specification
SELECT SID, name, age, GPA FROM Student ORDER BY GPA DESC, name; ASC is the default option Strictly speaking, only output columns can appear in ORDER BY clause (although some DBMS support more) Can use sequence numbers of output columns instead ORDER BY 4 DESC, 2;
45