More SQL Today:  Nested Queries  More SQL Nested Queries in SQL  Queries containing other queries  Inner query:  Can appear in FROM or WHERE clause.

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 SQL: Structured Query Language (‘Sequel’) Chapter 5.
Schema BankWorld Customer(C_Name, C_Street, C_City) Depositor(Account_#, C_Name) Loan(Loan_#, B_Name, Amount) Borrower(C_Name, Loan_#)
CS 166: Database Management Systems
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL and Relational Algebra Zaki Malik September 02, 2008.
SQL.
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.
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
SQL Review.
E-R Diagram for a Banking Enterprise
CMSC424, Spring 2005 CMSC424: Database Design Lecture 5.
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.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
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.
SQL (DML) II. Review: Formal Semantics of SQL: RA* SELECT A1, A2, …, An FROM r1, r2, …, rm WHERE P Query: Semantics:  * A1, A2,..., An (  * P (r1 x*
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
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.
Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational.
1 Advanced SQL. 2 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name,
SQL I. SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language”
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Null Values It is possible for tuples to have a null value for some of their attributes The.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition.
SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language” (create.
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.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
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.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
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.
Nested Subqueries in SQL By Kenneth Cheung CS 157A Section 2 Professor Lee.
Assertions and Triggers in SQL
SQL II.
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.
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.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
Structured Query Language
COP Introduction to Database Structures
© פרופ' יהושע שגיב, האוניברסיטה העברית
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
Chapter 3: Relational Model III
SQL The Query Language R & G - Chapter 5
Database Systems October 14, 2009 Lecture #5.
Database Systems (資料庫系統)
Basic SQL Lecture 6 Fall
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
Relational Calculus.
CS4222 Principles of Database System
SQL: Structured Query Language
CS 405G: Introduction to Database Systems
Presentation transcript:

More SQL Today:  Nested Queries  More SQL

Nested Queries in SQL  Queries containing other queries  Inner query:  Can appear in FROM or WHERE clause “outer query” “inner query” Example: SELECT cname FROM borrower WHERE cname IN (SELECT cname FROM depositor) think this as a function that returns the result of the inner query

Nested Queries in SQL Another example: SELECT DISTINCT cname FROM borrower as b, loan as l WHERE b.lno = l.lno AND bname = “Central” AND (bname, cname) IN ( SELECT bname, cname FROM depositor as d, account as a WHERE d.acct_no = a.acct_no) Q: Describe what this query returns Ans: Names of borrowers at the Central branch who also have savings account at the same branch borrower (cname, lno) account (acct_no, bname, bal) loan (lno, bname, amount) depositor (cname, acct_no)

Nested Queries in SQL Another example: SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.sid = 103) Q: Describe what this query returns! Schema: Boats(bid, bname, color) Sailors(sid, sname, rating, age) Reserves( sid, bid, day) Q: What happens if I replace IN with NOT IN?

Nested Queries in SQL  What can we do with queries in the WHERE clause?  Let A = SELECT... FROM... WHERE WHERE IN A WHERE NOT IN A 2.WHERE < ANY (A) ( ) WHERE < ALL (A) ( ) Also (‘>’, ‘ =‘, ‘=‘,...) 3.WHERE EXISTS(A) ( ) WHERE UNIQUE(A)

Nested Queries in SQL ANY ( ): (or SOME) e < ANY(A)  (5< any) = true ) = false (5  any) = true (since 0  5) (read: 5 < some tuple in the relation) (5< any ) = true (5 = any (= any)  in However, (  any)  not in example: (3  any ) 3 5

Nested Queries in SQL Example query with ANY: SELECT bname FROM branch WHERE assets > ANY ( SELECT assets FROM branch WHERE bcity=‘Bkln’) Q: Describe what this query returns Ans: Find all branches that have grater assets than some branch in Brooklyn.

Nested Queries in SQL  Optimization of queries! SELECT bname FROM branch WHERE assets > ANY ( SELECT assets FROM branch WHERE bcity=‘Bkln’) Q: Can you rewrite this query to something that is equivalent, but more efficient to execute? 1) SELECT assets 2) SELECT bname INTO Temp FROM branch FROM branch WHERE assets > ANY (Temp) WHERE bname=‘Bkln’ Q: Why this is better ?

Nested Queries in SQL ALL ( ): e < ALL (A)  (5< all) = false ) = true (5  all) = true (since 5  4 and 5  6) (5< all ) = false (5 = all (  all)  not in However, (= all)  in

Nested Queries in SQL Example query with ALL: SELECT bname FROM branch WHERE assets > ALL ( SELECT assets FROM branch WHERE bcity=‘Bkln’) Returns: branches that have greater assets than all branches in Brooklyn borrower (cname, lno) account (acct_no, bname, bal) loan (lno, bname, amount) depositor (cname, acct_no)

Nested Queries in SQL Correlated Queries: when the inner query contains tuple variables of the outer query Example: Find all accounts located at branches that also hold a loan for a smaller amount SELECT DISTINCT A.cct_no FROM account as A WHERE EXISTS ( SELECT * FROM loan as L WHERE A.bname = L.bname AND A.balance > L.amt) “correlation”: inner query contains reference to table in outer query

Nested Queries with Correlation  Names of sailors who have reserved boat # 103: SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid = 103 AND S.sid = R.sid)

Nested Queries in SQL EXISTS: EXISTS(A)  (i.e., true if A is not empty) Example: Find all depositors who have accounts at all branches in Brooklyn SELECT DISTINCT cname FROM depositor as S WHERE NOT EXISTS ( ( SELECT bname FROM branch WHERE bcity = ‘Bkln’) EXCEPT ( SELECT bname FROM depositor as T, account as R WHERE T.acct_no = R.acct_no AND S.cname = T.cname)) Inner Query: (branches in Brooklyn) - (branches where S has an account)

Nested Queries in SQL UNIQUE: UNIQUE(A) = true, if A has no duplicates Example: Find all customers who have no more than one account at “Kenmore” SELECT T.cname FROM depositor as T WHERE UNIQUE( SELECT R.cname FROM account as A, depositor as R WHERE T.cname = R.cname AND R.acct_no = A.acct_no AND A.bname = “Kenmore”) Inner Query: Returns T’s cname for every acct held by T at Kenmore

Nested Queries in SQL What about query optimization? SELECT DISTINCT A.acct_no FROM account as A WHERE EXISTS ( SELECT * FROM loan as L WHERE A.bname = L.bname AND A.bal > L.amt) SELECT DISTINCT A.acct_no FROM account as A, loan as L WHERE A.bname = L.bname AND A.bal > L.amt Above an example of Query Unnesting. advantageous because joins evaluate in more ways than nested queries borrower (cname, lno) account (acct_no, bname, bal) loan (lno, bname, amount) depositor (cname, acct_no)

Nested Queries in SQL Correlation with Aggregates: SELECT bname FROM account as A WHERE bal > (SELECT AVG(amt) FROM loan as L WHERE A.bname = L.bname) Returns the branch names where accounts are held whose balances are more than the average loan taken at the same branch Kim’s technique: (1) SELECT bname, AVG(amt) as avgloan INTO Temp FROM loan GROUP BY bname (2) SELECT A. bname FROM account as A, temp as L WHERE A.bname = L.bname AND A.bal > L.avgloan

Kim’s Unnesting Technique  Why is the rewrite better than the original query? Ans: the rewrite computes the avg loans JUST ONCE per branch Is the rewrite always better than the nested query? Ans: NO: if loan has branch names not in account

Kim’s Unnesting Technique SELECT bname FROM branch as B WHERE B.numloans = ( SELECT COUNT(*) FROM loan as L WHERE B.bname = L.bname) returns branches whose loan count agrees with that specified (1) SELECT bname, COUNT(*) as numloans INTO Temp FROM Loan GROUP BY bname (2) SELECT B. bname FROM branch as B, temp as L WHERE B.bname = L.bname AND B.numloans = L.numloans Q: What is the problem with this rewrite?

Kim’s Bug (the COUNT bug) Ans: Suppose branch relation includes a branch (Kenmore) with numloans = 0 and the loan is the same. Is Kenmore in the result of the nested query? Is kenmore in the result of the unnested query? Why??? Nested query: WHERE A.x = (SELECT COUNT(*) FROM B WHERE A.y = B.z) If A.x = 0 and... no such B, then result empty COUNT(empty) = 0 So, it is in the nested query.. but not in the unnested!! What about SUM? No problem because SUM returns NULL and comparing something to NULL is never true

Nested Queries in SQL Nesting also possible in FROM clause Example: Another way to express HAVING SELECT bname, avg(balance) as avgbal FROM account GROUP BY bname HAVING AVG(balance) > 1200 SELECT * FROM (SELECT bname, AVG(balance) as avgbal FROM account GOUP BY bname) AS TempRes WHERE avgbal > 1200

Nested Queries in SQL Nested query in FROM clause, another example: Find the maximum total balance across all branches SELECT MAX(tot-balance) FROM (SELECT bname, SUM(balance) FROM account GROUP BY bname) AS branch-total(bname, tot-balance)

More SQL Examples (from the book)…  Find the sailors with the highest rating  Find the sailors who have reserved all boats  Find the name and age of the oldest sailor  Find the rating for which the average age of sailors is the minimum over all ratings Schema: Boats(bid, bname, color) Sailors(sid, sname, rating, age) Reserves( sid, bid, day)