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.

Slides:



Advertisements
Similar presentations
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Advertisements

1 Database Systems ( 資料庫系統 ) October 22/24, 2007 Lecture #5.
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.
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.
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
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.
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
1 Sub-queries and Views. 2 A Complex Query We would like to create a table containing 3 columns: –Sailor id –Sailor age –Age of the oldest Sailor How.
1 Rewriting Minus Queries Using Not In SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ MINUS.
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 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.
INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008.
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.
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.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5 Jianping Fan.
SQL and Query Execution for Aggregation. Example Instances Reserves Sailors Boats.
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
Database Systems October 14, 2009 Lecture #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: Structured Query Language
SQL: Queries, Programming, Triggers
SQL: The Query Language (Part III)
Presentation transcript:

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 SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘green’;

2 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’ and S.sid IN (SELECT S2.sid FROM Sailors S2, Boats B2, Reserves R2 WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘green’);

3 Division Consider: A(X,Y) and B(Y). Then A/B = In general, we require that the set of fields in B be contained in those of A.

4 Suppliers from A who supply All Parts from B snopno S1 S2 S3 S4 P1 P2 P3 P4 P1 P2 P4 A P2 pno B1 P2 P4 pno B2 P1 P2 P4 pno B3 S1 S4 sno A/B2 S1 sno A/B3 S1 S2 S3 S4 sno A/B1

5 Sailors who Reserved all Boats To find the Sailors who reserved all boats: (  sid,bid Reserves)/(  bid Boats) Division can be expressed using other relational algebra operators.

6 Division in SQL (1) 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

7 Division in SQL (2) SELECT S.sname FROM Sailors S WHERE NOT EXISTS((SELECT B.bid FROM Boats B) EXCEPT (SELECT R.bid FROM Reserves R WHERE R.sid = S.sid));

8 Aggregation

9 Aggregate Operators The aggregate operators available in SQL are: –COUNT(*) –COUNT([DISTINCT] A) –SUM([DISTINCT] A) –AVG([DISTINCT] A) –MAX(A) –MIN(A)

10 Some Examples SELECT COUNT(*) FROM Sailors S SELECT AVG(S.age) FROM Sailors S WHERE S.rating=10

11 Find name and age of oldest Sailor SELECT S.sname, MAX(S.age) FROM Sailors S SELECT S.sname, S.age FROM Sailors S WHERE S.age = (SELECT MAX(S2.age) FROM Sailors S2) Wrong!! Right!!

12 Find Average Age for each Rating So far, aggregation has been applied to all tuples that passed the WHERE clause test. How can we apply aggregation to groups of tuples?

13 Basic SQL Query SELECT [Distinct] target-list FROM relation-list WHERE condition GROUP BY grouping-list HAVING group-condition; target-list: Fields appearing in grouping-list and aggregation operators group-condition: Can only constrain attributes appearing in grouping-list

14 Evaluation 1.Compute cross product of relations in FROM 2.Tuples failing WHERE are thrown away 3.Tuples are partitioned into groups by values of grouping-list attributes 4.The group-condition is applied to eliminate groups 5.One answer in generated for each group

15 Find Average Age for each Rating SELECT S.rating, AVG(S.age) FROM Sailors S GROUP BY S.rating

16 Find the number of Reservations of Each Red Boat SELECT B.bid, COUNT(*) as count FROM Boats B, Reserves R WHERE R.bid=B.bid and B.color=‘red’ GROUP BY B.bid What would happen if we put the condition about the color in the HAVING clause?

17 Age of Youngest Sailor that is over 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

18 Age of Youngest Sailor that is over 18, for each Rating with at least 2 Sailors (of any Age) SELECT S.rating, MIN(S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING 1 < (SELECT COUNT(*) FROM Sailors S2 WHERE S.rating = S2.rating)

19 More Options

20 Sorting Results Results can be sorted using the ORDER BY clause Here are sailors who ordered boat 103, ordered by their names: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid and R.bid = 103 ORDER BY S.sname

21 Outer Join How can we find sailor names with their reservation numbers if we don’t want to lose information about sailors who did not reserve any boats?? SELECT S.sname, R.bid FROM Sailors S, Reserves R WHERE S.sid = R.sid(+) The (+) must follow column that we allow to be null.

22 Views A View is a query that looks like a table and can be used as a table. CREATE OR REPLACE VIEW SailorsBoats SELECT S.sname, B.bname, B.color FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid and R.bid = B.bid; SELECT sname FROM SailorBoats WHERE color = ‘red’;

23 Views For Restricting Access Suppose that we have a table: Grades(Login, Exercise, Grade) We would like a user to only be able to see his own grades. We create the following view and grant privileges to query the view (not the underlying table) CREATE OR REPLACE VIEW UserGrades SELECT * FROM Grades WHERE Login = User; Pseudo-column which is equal to the user name.

24 Views for Complex Queries Find those ratings for which the average age is the minimum over all ratings CREATE OR REPLACE VIEW AvgAgeRatings SELECT S.rating, AVG(S.age) as avgage FROM Sailors S WHERE S.rating; SELECT rating, avgage FROM AveAgeRatings WHERE avgage = (SELECT MIN(avgage) FROM AveAgeRatings)

25 Sub-queries in FROM, instead of Views SELECT Temp.rating, Temp.avgage FROM (SELECT S.rating, AVG(S.age) as avgage FROM Sailors S GROUP BY S.rating) as Temp WHERE Temp.avgage = (SELECT MIN(Temp.avgage) FROM Temp)

26 Delete and Update

27 Deleting Tuples The basic form of a delete statement is: DELETE FROM TableName WHERE Condition;

28 Examples DELETE FROM Sailors WHERE rating < 3; DELETE FROM Sailors S1 WHERE S1.rating = (SELECT MIN(S2.rating) FROM Sailors S2) Delete Sailors with rating less than 3: Delete Sailors with the minimum rating:

29 Updating Tuples The basic form of an update statement is: UPDATE TableName SET Column1 = Value1, … ColumnN = ValueN WHERE Condition;

30 Example UPDATE Boats SET color = ‘red’, bname = ‘Voyager’ WHERE bid = 13; Update boat 13: color to red and name to voyager

31 Another Example UPDATE Sailors SET rating = (SELECT MAX(rating) FROM Sailors) WHERE sname = ‘Rusty’; Update rating of Rusty to be the maximum rating of any sailor Note: When updating with a subquery, the subquery must return one value only!