1 Advanced SQL. 2 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name,

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.
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.
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.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting.
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 Table Alteration. 2 Altering Tables Table definition can be altered after its creation –Adding columns –Changing columns’ definition –Dropping columns.
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 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.
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.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
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,
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.
The Oracle Database System. Connecting to the Database At the command line prompt, write: sqlplus In the beginning your password.
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.
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.
DataBase - Check 01 DataBase 2 nd year Computer Science & Engineer 1.
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
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
SQL: Queries, Constraints, Triggers
SQL: The Query Language Part 1
קורס קבצים ובסיסי נתונים
CS4222 Principles of Database System
SQL: Structured Query Language
Presentation transcript:

1 Advanced SQL

2 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) How would you query the subjects learnt by the 3 rd grade and the teachers who teach them? select subject_name, teacher from subject where class = ‘ 3rd grade ’ Would adding distinct change the result? Only if the same tuples may appear more than once in table subject

3 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) How would you query the pupils who are taught by the teacher Nurit and in which class? select distinct pupil_name, pupil.class from pupil, subject where pupil.class = subject.class and teacher = ‘ Nurit ’

4 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) How would you query all pairs of pupils who study in the same class ? select p1.pupil_name, p2.pupil_name from pupil p1, pupil p2 where p1.class = p2.class and p1.pupil_name < p2.pupil_name

5 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) How would you query the average age of all pupils? select avg(2007  birthyear) from pupil

6 Nested Queries: reminder and more

7 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty Boats bidbnamecolor Nancy Gloria red green

8 Nested queries in WHERE Equality nested query: Select R.bid From Sailors S, Reserves R Where sid = (select sid from S where sname=‘George’); When would this work? When the subquery returns exactly one tuple

9 Nested queries in WHERE SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid = 103); Subqueries with multiple results: Names of sailors who reserved boat 103 What would happen if we wrote NOT IN? We would get names of sailors who did not reserve boat 103

10 What does this produce? SELECT S.sname FROM Sailors S WHERE S.sid NOT IN (SELECT R.sid FROM Reserves R WHERE R.bid IN (SELECT B.bid FROM Boats B WHERE B.color='red')); Names of sailors who did not reserve a red boat

11 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) How would you query pupils in the 5 th grade who have a grade higher than 95? (with a subquery) select pupil_name from pupil where class= ‘ 5 th ’ and pupil_name in (select pupil_name from grades where grade>95) can also be written without a subquery …

12 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) How would you query pupils in the 5 th grade who have a grade higher than 95? (without a subquery) select pupil_name from pupil p, grades g where p.class= ‘ 5 th ’ and p.pupil_name=g.pupil_name and g.grade>95

13 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) How would you query pupils in the 5 th grade who DO NOT have a grade higher than 95? select pupil_name from pupil where class= ‘ 5 th ’ and pupil_name not in (select pupil_name from grades where grade>95) (cannot be expressed by a simple query as before)

14 Set-Comparison Queries SELECT * FROM Sailors S1 WHERE S1.age > ANY (SELECT S2.age FROM Sailors S2); Sailors who are not the youngest We can also use op ALL (op is >, =, ).

15 Exists and Not exists SELECT S.sid FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid = 103 and S.sid = R.sid); Sid of sailors who reserved boat 103 Q: What if we wrote NOT EXISTS? A: We would get sid of sailors who did not reserve boat 103 S not in subquery, refers to outer loop

16 Exists and Not Exists Exists: For every tuple in the outer loop, the inner loop is tested. If the inner loop produces a result, the outer tuple is added to the result. Differs from In: Does not match values, but tests for existence of result

17 Rewriting Minus Queries SELECT S.sname, S.sid 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, S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘green’; Name and id of sailors that have reserved a red boat and have not reserved a green boat

18 Rewriting Minus Queries Using Not In SELECT S.sname, 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 NOT IN ( SELECT R2.sid FROM Boats B2, Reserves R2 WHERE R2.bid = B2.bid and B2.color=‘green’);

19 Rewriting Minus Queries Using Not Exists SELECT S.sname, S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ and NOT EXISTS ( SELECT * FROM Boats B2, Reserves R2 WHERE R2.sid = S.sid and R2.bid = B2.bid and B2.color = ‘green’);

20 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) What does this return? select pupil_name from pupil p where not exists (select task_no from grades where pupil_name= ‘ michal ’ and subject_name= ‘ history ’ and task_no not in (select task_no from grades g where g.pupil_name = p.pupil_name and g.subject_name = ‘ history ’ )) Pupils who submitted all History tasks that michal submitted

21 How would you query the sailors who have reserved all boats?

22 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.

23 Suppliers from A who supply All Parts from B (1) snopno S1 S2 S3 S4 P1 P2 P3 P4 P1 P2 P4 A P2 pno B1 sno  =

24 Suppliers from A who supply All Parts from B (1) snopno S1 S2 S3 S4 P1 P2 P3 P4 P1 P2 P4 A P2 pno B1 sno  = S1 S2 S3 S4

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

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

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

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

29 Sailors who Reserved all Boats Reserves(sid,bid)  Boats(bid) Sailor S whose "set of boats reserved" contains the "set of all boats" So what is the result of this?

30 Sailors who reserved all boats (Division 1) SELECT sid FROM Sailors S WHERE NOT EXISTS (SELECT B.bid FROM Boats B WHERE B.bid NOT IN (SELECT R.bid FROM Reserves R WHERE R.sid = S.sid)); Sailors for which there does not exist a boat that they did not reserve

31 Sailors who reserved all boats (Division 2) SELECT S.sid 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)) Sailors for which there does not exist a boat that they did not reserve

32 Sailors who reserved all boats (Division 3) SELECT S.sid FROM Sailors S WHERE NOT EXISTS((SELECT B.bid FROM Boats B) MINUS (SELECT R.bid FROM Reserves R WHERE R.sid = S.sid)); Sailors for which there does not exist a boat for which there is no reservation in Reserves

33 Aggregation

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

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

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

37 Find Average Age for each Rating SELECT AVG(age) FROM Sailors GROUP BY rating;

38 Basic SQL Query SELECT [Distinct] attributes FROM relation-list WHERE condition GROUP BY grouping-attributes HAVING group-condition; attributes: must appear in grouping-attributes or aggregation operators group-condition: Constrains groups. Can only constrain attributes appearing in grouping-attributes or aggregation operators

39 Evaluation- important! 1.Compute cross product of relation-list 2.Tuples failing condition are thrown away 3.Tuples are partitioned into groups by values of grouping-attributes 4.The group-condition is applied to eliminate groups 5.One answer in generated for each group! SELECT [Distinct] attributes FROM relation-list WHERE condition GROUP BY grouping-attributes HAVING group-condition;

40 Sailors sidSnameratingage Dustin Lubber Rusty Fluffy Morley Popeye Sailors sidsnameratingage Dustin Fluffy Morley Lubber Rusty Popeye SELECT AVG(age) FROM Sailors GROUP BY rating;

41 SELECT AVG(age) FROM Sailors Where age<50 GROUP BY rating Having count(*)>2; SidSnameratingage Dustin Lubber Rusty Fluffy Morley Popeye Step 1 SidSnameratingage Dustin Rusty Fluffy Morley Popeye

42 SELECT AVG(age) FROM Sailors Where age<50 GROUP BY rating Having count(*)>2; Step 2 Sidsnameratingage Dustin Fluffy Morley Rusty Popeye

43 SELECT AVG(age) FROM Sailors Where age<50 GROUP BY rating Having count(*)>2; Step 3 Sidsnameratingage Dustin Fluffy Morley Rusty Popeye Sidsnameratingage Dustin Fluffy Morley

44 SELECT AVG(age) FROM Sailors Where age<50 GROUP BY rating Having count(*)>2; Step 4 Sidsnameratingage Dustin Fluffy Morley Final Answer:

45 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) For each subject, query its name and average task grade select subject_name, avg (grade) from grades group by subject_name

46 grades (pupil_name, subject_name, task_no, grade) For each subject, query its name and average task grade, where only passing grades (>=60) are included in the average select subject_name, avg (grade) from grades where grade>=60 group by subject_name For each subject, query its name and average task grade, where only subjects with passing average are displayed select subject_name, avg (grade) from grades group by subject_name Having avg(grade)>=60

47 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name, subject_name, task_no, grade) –tasks (subject_name, task_no) What is wrong with the follwing? select grade from grades where grade>=60 group by subject_name We cannot select an attribute which is not grouped by (unless it is an aggregation operator)

48 Find name and age of oldest Sailor..? SELECT S.sname, MAX(S.age) FROM Sailors S Wrong! SELECT S.sname, MAX(S.age) FROM Sailors S GROUP BY S.sname Wrong: we don’t obtain what we want

49 Find name and age of oldest Sailor SELECT S.sname, S.age FROM Sailors S WHERE S.age >= ALL (SELECT S2.age FROM Sailors S2) SELECT S.sname, S.age FROM Sailors S WHERE S.age = (SELECT MAX(S2.age) FROM Sailors S2) Right!! How else can this be done?

50 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 every reserved red boat : (id,number of reservations) What would happen if we put the condition about the color in the HAVING clause?

51 What would happen if we put the condition about the color in the HAVING clause? SELECT B.bid, COUNT(*) FROM Boats B, Reserves R WHERE R.bid=B.bid GROUP BY B.bid, B.color HAVING B.color=‘red’ It ’ s ok, but we also have to put the color in the grouping list!

52 What does this return? Can we move the condition in the HAVING to the WHERE? SELECT bname FROM Boats B, Reserves R WHERE R.bid=B.bid GROUP BY bid, bname HAVING count(DISTINCT day) <= 5 No! Aggregate functions are not allowed in WHERE Names of Boats that were not Reserved on more than 5 days

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

54 The Color for which there are the most boats SELECT color FROM Boats B GROUP BY color HAVING count(bid) >= ALL (SELECT count(bid) FROM Boats GROUP BY Color)

55 Aggregation Instead of Exists Aggregation can take the place of exists. What does this return? SELECT color FROM Boats B1 WHERE NOT EXISTS( SELECT * FROM Boats B2 WHERE B1.bid <> B2.bid AND B1.color=B2.color) The color of boats which have a unique color (no other boats with the same color)

56 Aggregation Instead of Exists SELECT color FROM Boats B1 GROUP BY color HAVING count(bid) = 1 Somewhat simpler …