CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 1 R &G - Chapter 5 Life is just a bowl of queries. -Anon (not Forrest Gump)
Advertisements

1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
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: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
SQL (2).
CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.
SQL Review.
 CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014.
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.
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.
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
SQL: The Query Language Part 1
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.
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.
Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Introduction to SQL Basics --- SQL in 45 Minutes Chapter 5.
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.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
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.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
 CS 405G: Introduction to Database Systems Lecture 6: Relational Algebra Instructor: Chen Qian.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CMPT 258 Database Systems SQL Queries (Chapter 5).
CS 405G: Introduction to Database Systems Relational Algebra.
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.
CS 405G: Introduction to Database Systems SQL III.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu.
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.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5 Jianping Fan.
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.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
SQL: The Query Language Part 1 R&G - Chapter 5 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.
Basic SQL Queries Go over example queries, like 10 > ALL.
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
EECS 647: Introduction to Database Systems
CS 405G: Introduction to Database Systems
CS 405G: Introduction to Database Systems
CS 405G: Introduction to Database Systems
SQL: The Query Language Part 1
SQL: Structured Query Language
CS 405G: Introduction to Database Systems
CS 405G: Introduction to Database Systems
Presentation transcript:

CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007

2/10/2016Jinze University of Kentucky2 Review SFW statement: Sort the outputs: USE bag operation: Enforce set operation: Output average: Partition rows in a table: Select certain groups: Subqueries: ORDER BY Default choice DISTINCT AVG (aggregation function) GROUP BY HAVING =, in, exists

2/10/2016Jinze University of Kentucky3 Operational semantics of subqueries 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?)

2/10/2016Jinze University of Kentucky4 Exercise Goal: Get the Name of one’s supervisor EMPLOYEE (Eid, Mid, Name) SQL Statement: SELECT EID, Name FROM EMPLOYEE WHERE MId =Eid; SELECT E1.EID, E2.Name FROM EMPLOYEE E1, EMPLOYEE E2 WHERE E1.MId = E2.EId; EidMidName John Smith Mary Carter Bob Lee Lisa Wang 1611 Jack Snow EidName 1123Mary Carter 1234Bob Lee 1311Jack Snow 1455Jack Snow 1611Jack Snow

2/10/2016Jinze University of Kentucky5 Exercise Goal: group employees according to their department, for each department, list EIds of its employees and list the head count EMPLOYEE (Eid, Name), DEPARTMENT(Eid, Did) SQL Statement: SELECT Did, Eid, COUNT(Eid) FROM EMPLOYEE e, DEPARTMENT d WHERE e.Eid = d.Eid GROUP BY Did There is something wrong!  Wishful thinking (list a group of Eid after grouping) won’t work  Solution: produce (1) a summary table listing Did and head count, and (2) sort Department according to Did.

2/10/2016Jinze University of Kentucky6 More on Nested Queries: Scoping To find out which table a column belongs to Start with the immediately surrounding query If not found, look in the one surrounding that; repeat if necessary Use table_name. column_name notation and AS (renaming) to avoid confusion

2/10/2016Jinze University of Kentucky7 An Example SELECT * FROM Student s WHERE EXISTS (SELECT * FROM Enroll e WHERE SID = s.SID AND EXISTS (SELECT * FROM Enroll WHERE SID = s.SID AND CID <> e.CID)); Students who are taking at least two courses

2/10/2016Jinze University of Kentucky8 Quantified subqueries 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 In common parlance, “any” and “all” seem to be synonyms In SQL, ANY really means “some”

2/10/2016Jinze University of Kentucky9 Examples of quantified subqueries Which employees have the highest salary? Employee (Sid, Name, Salary) SELECT * FROM Employee WHERE Salary >= ALL (SELECT Salary FROM Employee); How about the lowest salary? SELECT * FROM Employee WHERE Salary <= ALL (SELECT salary FROM Employee);

2/10/2016Jinze University of Kentucky10 More ways of getting the highest Salary Who has the highest Salary? SELECT * FROM Employee e WHERE NOT EXISTS (SELECT * FROM Employee WHERE Salary > e.Salary); SELECT * FROM Employee WHERE Eid NOT IN (SELECT e1.SID FROM Employee e1, Employee e2 WHERE e1.Salary < e2.Salary);

2/10/2016Jinze University of Kentucky11 Nested Queries Nested queries do not add expression power to SQL For convenient Write intuitive SQL queries Can always use SQL queries without nesting to complete the same task (though sometime it is hard)

2/10/2016Jinze University of Kentucky12 More Exercise Sailors (sid: INTEGER, sname: string, rating: INTEGER, age: REAL) Boats (bid: INTEGER, bname: string, color: string) Reserves (sid: INTEGER, bid: INTEGER) sidsnameratingage 1Fred722 2Jim239 3Nancy827 Sailors sidbid Reserves bidbnamecolor 101Ninared 102Pintablue 103Santa Maria red Boats

2/10/2016Jinze University of Kentucky13 Exercise I Find sid’s 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 B2.color=‘green’ AND R1.bid=B1.bid AND R2.bid=B2.bid AND R1.sid=R2.sid SELECT sid FROM Boats, Reserves Where B.color = ‘red’ INTERSECT ( SELECT sid FROM Boats, Reserves Where B.color = ‘green’)

2/10/2016Jinze University of Kentucky14 Exercise II Find sid’s of sailors who have not reserved a boat SELECT sid FROM Sailors EXCEPT ( SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid) Non-monotonic operation!

2/10/2016Jinze University of Kentucky15 Exercise III (a tough one) Find sailors who’ve reserved all boats SELECT Sid FROM Sailor EXCEPT SELECT Sid FROM (SELECT bid, sid FROM Boat, Sailor EXCEPT Reserves) Non-monotonic operation! #All possible combinations between sid and bid #Existing reservations #Those who do not reserve all boats #All sailors