CS 405G: Introduction to Database Systems

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Review Session ER and Relational –ER  Relational –Constraints, Weak Entities, Aggregation, ISA Relational Algebra  Relational Calculus –Selections/Projections/Joins/Division.
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
CS 166: Database Management Systems
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5.
SQL.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
SQL Part II: Advanced Queries. 421B: Database Systems - SQL Queries II 2 Aggregation q Significant extension of relational algebra q “Count the number.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
SQL (2).
CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.
M ATH IN SQL. 222 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. SUM ( [DISTINCT] A): the sum of all.
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
SQL: The Query Language CS 186, Spring 2006, Lectures 11&12 R &G - Chapter 5 Life is just a bowl of queries. -Anon.
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
1 Rewriting Intersect Queries Using In SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ INTERSECT.
1 Advanced SQL. 2 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name,
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
Chapter 5.  Data Manipulation Language (DML): subset of SQL which allows users to create queries and to insert, delete and modify rows.  Data Definition.
INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008.
CS 370 Database Systems Lecture 12 Introduction to SQL.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
SQL 2 Introduction Structured Query Language (SQL): the most widely used commercial relational database language Originally.
Unit 5/COMP3300/ SQL: Queries, Programming, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Example Instances R1 S1 S2  We will use these instances of the Sailors and Reserves relations in our.
ICS 321 Spring 2011 The Database Language SQL (iii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 3/14/20111Lipyeow.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
CMPT 258 Database Systems SQL Queries (Chapter 5).
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Overview: Features of SQL  Data definition language: used to create, destroy, and modify tables and.
SQL II.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
SQL CS 186, Spring 2007, Lecture 7 R&G, Chapter 5 Mary Roth   The important thing is not to stop questioning. Albert Einstein Life is just a bowl of.
SQL: The Query Language Part 1 R&G - Chapter 5 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
SQL: The Query Language Part 1 R&G - Chapter 5 Lecture 7 The important thing is not to stop questioning. Albert Einstein.
COP Introduction to Database Structures
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
SQL The Query Language R & G - Chapter 5
Basic SQL Lecture 6 Fall
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
CS 405G: Introduction to Database Systems
SQL: Queries, Constraints, Triggers
SQL: The Query Language Part 1
SQL: Structured Query Language
SQL: Queries, Programming, Triggers
SQL: The Query Language (Part III)
Presentation transcript:

CS 405G: Introduction to Database Systems SQL II Instructor: Jinze Liu

Review: SQL DML DML includes 4 main statements: SELECT (query), INSERT, UPDATE and DELETE e.g: PROJECT SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid = E.sid AND S.age=19 JOIN SELECT

Example: Find sailors who have reserved a red and a green boat Now let’s do this with a self-join SELECT R.sid FROM Boats B,Reserves R WHERE B.color = ‘red’ AND R.bid=B.bid INTERSECT WHERE B.color = ‘green’ AND R.bid=B.bid Boats bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria 105 Titanic green Reserves sid bid day 1 101 9/12 2 103 9/13 105 sid 1 2 sid 1 sid 1  =

Find sids of sailors who’ve reserved a red and a green boat SELECT R1.sid FROM Boats B1, Reserves R1, Boats B2, Reserves R2 WHERE B1.color = ‘red’ AND B1.bid=R1.bid AND B2.color = ‘green’ AND B2.bid=R2.bid AND R1.sid=R2.sid Find red reserved boats Find green reserved boats Find matching green and red reserved boats bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria 105 Titanic green sid bid day 1 101 9/12 2 103 9/13 105 sid 1 2 sid 1 = sid 1

Nested Queries The WHERE clause can itself contain an SQL query! (Actually, so can FROM and HAVING clauses can too) e.g. Find the names of sailors who’ve reserved boat #103: Reserves SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) sid bid day 1 101 9/12 2 103 9/13 105 sname Bilbo Sailors First compute the set of all sailors that have reserved boat 103… sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 …and then check each the sid of each tuple in Sailors to see if it is in S1 S sid 2 S S

Nested Queries This nested query was uncorrelated because the subquery does not refer to anything in the enclosing query SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) It can be evaluated once and then checked for each tuple in enclosing query

Nested Queries with correlation Nested queries can also be correlated; the subquery refers to the enclosing query SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND R.sid=S.sid) The subquery must be re-evaluated for each tuple in enclosing query EXISTS is a set operator that is true if result of set expression has at least one tuple

Nested Queries e.g. Find the names of sailors who’ve reserved boat #103: Notice that this query computes the same answer as the previous query! SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND R.sid=S.sid) 2 1 3 Sailors Reserves sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 sid bid day sid bid day 1 101 9/12 2 103 9/13 105 S 2 103 9/13 S S

Set-Comparison Operators <tuple expression> IN <set expression> True if <tuple> is a member of <set> Also, NOT IN EXISTS <set expression> True if <set expression> evaluates to a set with at least one member Also, NOT EXISTS UNIQUE <set expression> True if <set expression> evaluates to a set with no duplicates; each row can appear exactly once Also, NOT UNIQUE <tuple expression> comparison op ANY <set expression> True if <set expression> contains at least one member that makes the comparison true Also, op ALL

Use NOT Exists for Division Recall: X/Y means only give me X tuples that have a match in Y. Find sailors who’ve reserved all boats. X = set of sailors and Y = set of all boats with reservations. SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT B.bid FROM Boats B WHERE NOT EXISTS (SELECT R.bid FROM Reserves R WHERE R.bid=B.bid AND R.sid=S.sid)) Find Sailors S such that ... there is no boat B... without a reservation by Sailor S

Division SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT B.bid FROM Boats B WHERE NOT EXISTS (SELECT R.bid FROM Reserves R WHERE R.bid=B.bid AND R.sid=S.sid)) 101 103 1 3 2 Reserves Sailors sid bid day 1 103 9/12 2 9/13 3 9/14 101 Boats R sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 bid bname color 101 Nina red 103 Pinta blue S B R S R B R S R

UNIQUE Find the names of sailors who’ve reserved boat #103 exactly once SELECT S.sname FROM Sailors S WHERE UNIQUE (SELECT sid, bid FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) 3 2 1 Sailors Reserves sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 sid bid day sid bid day 1 103 9/12 2 9/13 S 2 103 9/13 S S

ANY Find sailors whose rating is greater than that of some sailor called Bilbo: Correlated or uncorrelated? SELECT * FROM Sailors S WHERE S.rating > ANY(SELECT S2.rating FROM Sailors S2 WHERE S2.sname=‘Bilbo’) Uncorrelated! S1 S2 sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 sid sname rating age 2 Bilbo 39 sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27

Aggregate Operators Very powerful; enables computations over sets of tuples SELECT COUNT (*) FROM Sailors S COUNT: returns a count of tuples in the set AVG: returns average of column values in the set SELECT AVG (S.age) FROM Sailors S WHERE S.rating=10 SUM: returns sum of column values in the set MIN, MAX: returns min (max) value of column values in a set. DISTINCT can be added to COUNT, AVG, SUM to perform computation only over distinct values. SELECT AVG(DISTINCT S.age) FROM Sailors S WHERE S.rating=10

Find name and age of the oldest sailor(s) Aggregate Operators Find name and age of the oldest sailor(s) X Sailors SELECT S.sname, MAX (S.age) FROM Sailors S sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 What will the result be? Not legal syntax; no other columns allowed in SELECT clause without a GROUP BY clause (we’ll learn about those next) sname age Frodo 39 Bilbo Sam

Find name and age of the oldest sailor(s) Aggregate Operators Find name and age of the oldest sailor(s) Instead: SELECT S.sname, S.age FROM Sailors S WHERE S.age = (SELECT MAX (S2.age) FROM Sailors S2) And then find the sailors(s) of that age… Find the maximum age…

GROUP BY and HAVING So far, we’ve seen aggregate operators applied to all tuples. What if we want to apply ops to each of several groups of tuples? Consider: Find the age of the youngest sailor for each rating level. In general, we don’t know how many rating levels exist, and what the rating values for these levels are! Suppose we know that rating values go from 1 to 10; we can write 10 queries that look like this (!): SELECT MIN (S.age) FROM Sailors S WHERE S.rating = i For i = 1, 2, ... , 10:

Queries With GROUP BY To generate values for a column based on groups of rows, use aggregate functions in SELECT statements with the GROUP BY clause Returning 1 row per group And finally compute aggregate function over each group… SELECT [DISTINCT] target-list FROM relation-list [WHERE qualification] GROUP BY grouping-list First select these rows… Then group them by the values in these columns… target-list contains: list of column names from grouping–list terms with aggregate operations (e.g., MIN (S.age)).

For each rating, find the age of the youngest sailor with age  18 Group By Example For each rating, find the age of the youngest sailor with age  18 Sailors sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 4 Pippin 21 5 Merry 17 SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating Group 1 1 Frodo 7 22 7 22 2 21 8 27 2 Bilbo 39 4 Pippin 21 Group 2 Group 3 3 Sam 8 27

Find the number of reservations for each red boat. SELECT B.bid, COUNT(*)AS tot_res FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ GROUP BY B.bid Boats bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria 103 1 101 2 Reserves sid bid day 1 102 9/12 2 103 9/13 3 101 9/14 4 2 103 9/13 3 101 9/14 4

Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification Use the HAVING clause with the GROUP BY clause to restrict which group-rows are returned in the result set

SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 Find the age of the youngest sailor with age  18, for each rating with at least 2 such sailors SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 2 3 Answer

Sailors who have reserved all boats sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 SELECT S.name 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) Boats bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria sname sid bid Frodo 1 102 Bilbo 2 101 103 Reserves sname sid count Frodo 1 Bilbo 2 3 count 3 sid bid day 1 102 9/12 2 101 9/14 9/10 103 9/13 sname sid bid Frodo 1 102,102 Bilbo 2 101, 102, 103

More about Joins SELECT (column_list) FROM table_name [INNER | {LEFT |RIGHT | FULL } OUTER] JOIN table_name ON qualification_list WHERE … Explicit join semantics needed unless it is an INNER join (INNER is default)

Default semantics: Inner Join Only rows that match search conditions are returned. SELECT s.sid, s.name, r.bid FROM Sailors s INNER JOIN Reserves r ON s.sid = r.sid Returns only those sailors who have reserved boats SQL-92 also allows: FROM Sailors s NATURAL JOIN Reserves r “NATURAL” means equi-join for each pair of attributes with the same name

SELECT s.sid, s.name, r.bid FROM Sailors s INNER JOIN Reserves r ON s.sid = r.sid

Left Outer Join Left Outer Join returns all matched rows, plus all unmatched rows from the table on the left of the join clause (use nulls in fields of non-matching tuples) SELECT s.sid, s.name, r.bid FROM Sailors s LEFT OUTER JOIN Reserves r ON s.sid = r.sid Returns all sailors & information on whether they have reserved boats

SELECT s.sid, s.name, r.bid FROM Sailors s LEFT OUTER JOIN Reserves r ON s.sid = r.sid

Right Outer Join Right Outer Join returns all matched rows, plus all unmatched rows from the table on the right of the join clause SELECT r.sid, b.bid, b.name FROM Reserves r RIGHT OUTER JOIN Boats b ON r.bid = b.bid Returns all boats & information on which ones are reserved.

SELECT r.sid, b.bid, b.name FROM Reserves r RIGHT OUTER JOIN Boats b ON r.bid = b.bid

Full Outer Join Full Outer Join returns all (matched or unmatched) rows from the tables on both sides of the join clause SELECT r.sid, b.bid, b.name FROM Reserves r FULL OUTER JOIN Boats b ON r.bid = b.bid Returns all boats & all information on reservations

SELECT r.sid, b.bid, b.name FROM Reserves r FULL OUTER JOIN Boats b ON r.bid = b.bid Note: in this case it is the same as the ROJ because bid is a foreign key in reserves, so all reservations must have a corresponding tuple in boats.

INSERT INSERT [INTO] table_name [(column_list)] VALUES ( value_list) <select statement> INSERT INTO Boats VALUES ( 105, ‘Clipper’, ‘purple’) INSERT INTO Boats (bid, color) VALUES (99, ‘yellow’) “bulk insert” from one table to another (must be type compatible): INSERT INTO TEMP(bid) SELECT r.bid FROM Reserves R WHERE r.sid = 22; “bulk insert” from files (in Postgres) Copy

DELETE & UPDATE DELETE FROM Boats WHERE color = ‘red’ DELETE [FROM] table_name [WHERE qualification] DELETE FROM Boats WHERE color = ‘red’ DELETE FROM Boats b WHERE b. bid = (SELECT r.bid FROM Reserves R WHERE r.sid = 22) Can also modify tuples using UPDATE statement. UPDATE Boats SET Color = “green” WHERE bid = 103;

Null Values Values are sometimes unknown (e.g., a rating has not been assigned or inapplicable (e.g., no spouse’s name). SQL provides a special value null for such situations. The presence of null complicates many issues. E.g.: Special operators needed to check if value is/is not null. “rating>8” - true or false when rating is null? What about AND, OR and NOT connectives? Need a 3-valued logic (true, false and unknown). Meaning of constructs must be defined carefully. (e.g., WHERE clause eliminates rows that don’t evaluate to true.) New operators (in particular, outer joins) possible/needed.

Null Values – 3 Valued Logic null AND true is null is null is null is null AND T F Null NULL OR T F Null NULL T F Null T T T F F F T F Null Null F Null T Null Null