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.

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

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
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.
SQL Review.
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.
1 Views and Null values. 2 What does this return? SELECT B.bid, COUNT(*) FROM Boats B, Reserves R WHERE R.bid=B.bid and B.color=‘red’ GROUP BY B.bid For.
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 Views. 2 Views A view is a "virtual table" defined using a query You can use a view as if it were a table, even though it doesn't contain data The view.
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.
SQL Queries and Subqueries Zaki Malik September 04, 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.
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.
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.
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.
SQL: The Query Language Part 1 R&G - Chapter 5 Lecture 7 The important thing is not to stop questioning. Albert Einstein.
1 SQL Structured Query Language. Query Language SQL is a query language Used to examine data in the database SQL queries do not change the contents of.
1 SQL Structured Query Language. Query Language SQL is a query language Used to examine data in the database SQL queries do not change the contents of.
Basic SQL Queries Go over example queries, like 10 > ALL.
COP Introduction to Database Structures
© פרופ' יהושע שגיב, האוניברסיטה העברית
Structured Query Language
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
CS 405G: Introduction to Database Systems
קורס קבצים ובסיסי נתונים
SQL: Structured Query Language
SQL: The Query Language (Part III)
Presentation transcript:

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

2 Rewriting Minus Queries Using Not In

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 sno  =

5 Suppliers from A who supply All Parts from B snopno S1 S2 S3 S4 P1 P2 P3 P4 P1 P2 P4 A sno  = P2 P4 pno B2

6 Suppliers from A who supply All Parts from B snopno S1 S2 S3 S4 P1 P2 P3 P4 P1 P2 P4 A sno  = P1 P2 P4 pno B3

7 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. How?

8 Similar to Expression Containment Sailor S whose "set of boats reserved" contains the "set of all boats" Sailor S for which there does not exist a boat B in Boats that he did not reserve Sailor S for which there does not exist a boat B in Boats for which there is no reservation in Reserves

9 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))

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

11 Aggregation

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

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

14 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?

15 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

16 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

17 Find Average Age for each Rating

18 Sailors sidsnameratingage Dustin Lubber Rusty Fluffy Morley Popeye Sailors sidsnameratingage Dustin Fluffy Morley Lubber Rusty Popeye

19 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!! How else can this be done? Hint: >= ALL

20 What does this return? SELECT B.bid, 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?

21 Names of Boats that were not Reserved on more than 5 days Can we move the condition in the HAVING to the WHERE?

22 The Color for which there are the most boats SELECT color FROM Boats B GROUP BY color HAVING max(count(bid)) What is wrong with this? How would you fix it?

23 Aggregation Instead of Exists Aggregation can take the place of exists. Example: SELECT color FROM Boats B1 WHERE not exists( SELECT * FROM Boats B2 WHERE B1.bno<> B2.bno and B1.color=B2.color)

24 Aggregation Instead of Exists SELECT color FROM Boats B1 GROUP BY color HAVING count(bid) = 1

25 Sub-queries and Views

26 A Complex Query We would like to create a table containing 3 columns: –Sailor id –Sailor age –Age of the oldest Sailor How can this be done?

27 Attempt 1 SELECT S.sid, S.age, MAX(S.age) FROM Sailors S; Why is this wrong?

28 Attempt 2 SELECT S.sid, S.age, MAX(S.age) FROM Sailors S GROUP BY S.id, S.age; Why is this wrong?

29 Solution 1: Subquery in FROM SELECT S.sid, S.age, M.mxage FROM Sailors S,(SELECT MAX(S2.age) as mxage FROM Sailors S2) M; We can put a query in the FROM clause instead of a table The query in the FROM clause must be renamed with a range variable (M in this case).

30 Solution 2: Subquery in SELECT SELECT S.sid, S.age, (SELECT MAX(S2.age) FROM Sailors S2) FROM Sailors S; A query in the SELECT clause must return at most one value for each row returned by the outer query.

31 Another Example of a Sub-query in SELECT SELECT S.sid, S.age, (SELECT MAX(S2.age) FROM Sailors S2 WHERE S2.age<S.age) FROM Sailors S; What does this query return? Note the use of S (defined in the outer query) within the inner query.

32 Another Example of a Sub-query in FROM?? SELECT S.sid, S.age, M.mxage FROM Sailors S, (SELECT MAX(S2.age) as mxage FROM Sailors S2 WHERE S2.age<S.age); Why is this wrong?

33 Solution 3: Create a Table A View is a query that looks like a table and can be used as a table. CREATE TABLE MaxAge as SELECT MAX(S.age) as mxage FROM Sailors S; SELECT S.sid, S.age, M.mxage FROM Sailors S, MaxAge M; MUST Rename!

34 Views A view is a "virtual table" defined using a query You can use a view as if it were a table, even though it doesn't contain data The view is computed every time that it is referenced

35 Advantages and Disadvantages Advantages: –no memory used for table –update of table does not require updating views –gives query processor more choices for optimizing Disadvantages: –must be recomputed every time used –if tables that view uses are dropped, view data is lost

36 Solution 4: Views A View is a query that looks like a table and can be used as a table. CREATE OR REPLACE VIEW MaxAge as SELECT MAX(S.age) as mxage FROM Sailors S; SELECT S.sid, S.age, M.mxage FROM Sailors S, MaxAge M; MUST Rename!

37 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 as SELECT * FROM Grades WHERE Login = User; Pseudo-column which is equal to the user name.