Matthew P. Johnson, OCL3, CISDD CUNY, June 20051 OCL3 Oracle 10g: SQL & PL/SQL Session #4 Matthew P. Johnson CISDD, CUNY June, 2005.

Slides:



Advertisements
Similar presentations
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Advertisements

Relational Algebra (end) SQL April 19 th, Complex Queries Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store,
DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
Relational Algebra Maybe -- SQL. Confused by Normal Forms ? 3NF BCNF 4NF If a database doesn’t violate 4NF (BCNF) then it doesn’t violate BCNF (3NF) !
1 Lecture 12: SQL Friday, October 26, Outline Simple Queries in SQL (5.1) Queries with more than one relation (5.2) Subqueries (5.3) Duplicates.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #9 M.P. Johnson Stern School of Business, NYU Spring, 2008.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #10 Matthew P. Johnson Stern School of Business, NYU Spring,
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #9 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
Matthew P. Johnson, OCL2, CISDD CUNY, January OCL2 Oracle 10g: SQL & PL/SQL Session #4 Matthew P. Johnson CISDD, CUNY Fall, 2004.
1 INTERSECT and EXCEPT: (may no be in MySQL) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A,
1 Lecture 03: SQL Friday, January 7, Administrivia Have you logged in IISQLSRV yet ? HAVE YOU CHANGED YOUR PASSWORD ? Homework 1 is now posted.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #11 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #12 M.P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #10 M.P. Johnson Stern School of Business, NYU Spring, 2008.
Matthew P. Johnson, OCL4, CISDD CUNY, Sept OCL4 Oracle 10g: SQL & PL/SQL Session #3 Matthew P. Johnson CISDD, CUNY June, 2005.
M.P. Johnson, DBMS, Stern/NYU, Spring Complex RA Expressions Scenario: 1. Purchase(pid, seller-ssn, buyer-ssn, etc.) 2. Person(ssn, name, etc.)
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Correlated Queries SELECT title FROM Movie AS Old WHERE year < ANY (SELECT year FROM Movie WHERE title = Old.title); Movie (title, year, director, length)
Union, Intersection, Difference (SELECT name FROM Person WHERE City=“Seattle”) UNION (SELECT name FROM Person, Purchase WHERE buyer=name AND store=“The.
One More Normal Form Consider the dependencies: Product Company Company, State Product Is it in BCNF?
Relational Algebra Basic Operations Algebra of Bags.
Relational Algebra.  Introduction  Relational Algebra Operations  Projection and Selection  Set Operations  Joins  Division  Tuple Relational Calculus.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
1 SQL cont.. 2 Outline Unions, intersections, differences (6.2.5, 6.4.2) Subqueries (6.3) Aggregations (6.4.3 – 6.4.6) Hint for reading the textbook:
Banner and the SQL Select Statement: Part Four (Multiple Connected Select Statements) Mark Holliday Department of Mathematics and Computer Science Western.
1 Database Systems Lecture #7 Yan Pan School of Software, SYSU 2011.
Relational Model Concepts. The relational model represents the database as a collection of relations. Each relation resembles a table of values. A table.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
Subqueries.
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: 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 Lecture 03: SQL Monday, January 9, Project t/Default.aspxhttp://iisqlsrv.cs.washington.edu/444/Projec.
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.
Basic Operations Algebra of Bags
COP Introduction to Database Structures
Lecture 04: SQL Monday, January 10, 2005.
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Relational Algebra at a Glance
Lecture 8: Relational Algebra
Database Systems Subqueries, Aggregation
Using Subqueries to Solve Queries
Basic SQL Lecture 6 Fall
Lecture 2 (cont’d) & Lecture 3: Advanced SQL – Part I
Using the Set Operators
Database Systems 10/13/2010 Lecture #4.
Introduction to Database Systems CSE 444 Lecture 03: SQL
Relational Algebra Chapter 4 - part I.
Introduction to Database Systems CSE 444 Lecture 03: SQL
Basic Operations Algebra of Bags
SQL Introduction Standard language for querying and manipulating data
Using Subqueries to Solve Queries
Lecture 12: SQL Friday, October 20, 2000.
Introduction to Database Systems CSE 444 Lecture 02: SQL
Lectures 5: Introduction to SQL 4
Lecture 4: SQL Thursday, January 11, 2001.
Lecture 3 Monday, April 8, 2002.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Lecture 03: SQL Friday, October 3, 2003.
Lecture 3: Relational Algebra and SQL
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Lecture 04: SQL Monday, October 6, 2003.
Presentation transcript:

Matthew P. Johnson, OCL3, CISDD CUNY, June OCL3 Oracle 10g: SQL & PL/SQL Session #4 Matthew P. Johnson CISDD, CUNY June, 2005

Matthew P. Johnson, OCL3, CISDD CUNY, June Agenda Go over labs 1 and 2 More SQL Lab 4

Matthew P. Johnson, OCL3, CISDD CUNY, June Review Examples from sqlzoo.netsqlzoo.net SELECT L FROM R 1, …, R n WHERE C SELECT L FROM R 1, …, R n WHERE C  L (  C (R 1 x … R n )

Matthew P. Johnson, OCL3, CISDD CUNY, June SQL e.g. Acc(name,ssn,balance) Q: Who has the largest balance? Conceptually:  name (Acc) -  a2.name (  a2.bal < Acc.bal (Acc x  a2 (Acc))) In SQL?

Matthew P. Johnson, OCL3, CISDD CUNY, June New topic: Subqueries Powerful feature of SQL: one clause can contain other SQL queries  Anywhere where a value or relation is allowed Several ways:  Selection  single constant (scalar) in SELECT  Selection  single constant (scalar) in WHERE  Selection  relation in WHERE  Selection  relation in FROM

Matthew P. Johnson, OCL3, CISDD CUNY, June Subquery motivation Consider standard multi-table example:  Purchase(prodname, buyerssn, etc.)  Person(name, ssn, etc.)  What did Christo buy? As usual, need to AND on equality identifying ssn’s row and buyerssn’s row SELECT Purchase.prodname FROM Purchase, Person WHERE buyerssn = ssn AND name = 'Christo'

Matthew P. Johnson, OCL3, CISDD CUNY, June Subquery motivation Purchase(prodname, buyerssn, etc.) Person(name, ssn, etc.) What did Conrad buy? Natural intuition:  Go find Conrad’s ssn  Then find purchases SELECT ssn FROM Person WHERE name = 'Christo' SELECT Purchase.prodname FROM Purchase WHERE buyerssn = Christo’s-ssn

Matthew P. Johnson, OCL3, CISDD CUNY, June Subqueries Subquery: copy in Conrad’s selection for his ssn: The subquery returns one value, so the = is valid If it returns more (or fewer), we get a run-time error SELECT Purchase.prodname FROM Purchase WHERE buyerssn = (SELECT ssn FROM Person WHERE name = 'Christo') SELECT Purchase.prodname FROM Purchase WHERE buyerssn = (SELECT ssn FROM Person WHERE name = 'Christo')

Matthew P. Johnson, OCL3, CISDD CUNY, June Operators on subqueries Several new operators applied to (unary) selections: 1. IN R 2. EXISTS R 3. UNIQUE R 4. s > ALL R 5. s > ANY R 6. x IN R > is just an example op Each expression can be negated with NOT

Matthew P. Johnson, OCL3, CISDD CUNY, June Subqueries with IN Product(name,maker), Person(name,ssn), Purchase(buyerssn,product) Q: Find companies Martha bought products from Strategy: 1. Find Martha’s ssn 2. Find products listed with that ssn as buyer 3. Find company names of those products SELECT DISTINCT Product.maker FROM Product WHERE Product.name IN (SELECT Purchase.product FROM Purchase WHERE Purchase.buyerssn = (SELECT ssn FROM Person WHERE name = 'Martha')) SELECT DISTINCT Product.maker FROM Product WHERE Product.name IN (SELECT Purchase.product FROM Purchase WHERE Purchase.buyerssn = (SELECT ssn FROM Person WHERE name = 'Martha'))

Matthew P. Johnson, OCL3, CISDD CUNY, June Subqueries returning relations Equivalent to: SELECT DISTINCT Product.maker FROM Product, Purchase, People WHERE Product.name = Purchase.product AND Purchase.buyerssn = ssn AND name = 'Martha' SELECT DISTINCT Product.maker FROM Product, Purchase, People WHERE Product.name = Purchase.product AND Purchase.buyerssn = ssn AND name = 'Martha'

Matthew P. Johnson, OCL3, CISDD CUNY, June FROM subqueries Motivation for another way:  suppose we’re given Martha’s purchases  Then could just cross with Products to get product makers  Substitute (named) subquery for Martha’s purchases SELECT Product.maker FROM Product, (SELECT Purchase.product FROM Purchase WHERE Purchase.buyerssn = (SELECT ssn FROM Person WHERE name = 'Martha')) Marthas WHERE Product.name = Marthas.product SELECT Product.maker FROM Product, (SELECT Purchase.product FROM Purchase WHERE Purchase.buyerssn = (SELECT ssn FROM Person WHERE name = 'Martha')) Marthas WHERE Product.name = Marthas.product

Matthew P. Johnson, OCL3, CISDD CUNY, June ALL op Employees(name, job, divid, salary) Find which employees are paid more than all the programmers SELECT name FROM Employees WHERE salary > ALL (SELECT salary FROM Employees WHERE job='programmer') SELECT name FROM Employees WHERE salary > ALL (SELECT salary FROM Employees WHERE job='programmer')

Matthew P. Johnson, OCL3, CISDD CUNY, June ANY/SOME op Employees(name, job, divid, salary) Find which employees are paid more than at least one vice president SELECT name FROM Employees WHERE salary > ANY (SELECT salary FROM Employees WHERE job='VP') SELECT name FROM Employees WHERE salary > ANY (SELECT salary FROM Employees WHERE job='VP')

Matthew P. Johnson, OCL3, CISDD CUNY, June ANY/SOME op Employees(name, job, divid, salary) Find which employees are paid more than at least one vice president SELECT name FROM Employees WHERE salary > SOME (SELECT salary FROM Employees WHERE job='VP') SELECT name FROM Employees WHERE salary > SOME (SELECT salary FROM Employees WHERE job='VP')

Matthew P. Johnson, OCL3, CISDD CUNY, June Existential/Universal Conditions Employees(name, job, divid, salary) Division(name, id, head) Find all divisions with an employee whose salary is > Existential: easy! SELECT DISTINCT Division.name FROM Employees, Division WHERE salary > AND divid=id SELECT DISTINCT Division.name FROM Employees, Division WHERE salary > AND divid=id

Matthew P. Johnson, OCL3, CISDD CUNY, June Existential/Universal Conditions Employees(name, job, divid, salary) Division(name, id, head) Find all divisions in which everyone makes > Universal: hard!

Matthew P. Johnson, OCL3, CISDD CUNY, June Existential/universal with IN 2. Select the divisions we didn’t find 1. Find the other divisions: in which someone makes <= SELECT name FROM Division WHERE id IN (SELECT divid FROM Employees WHERE salary <= SELECT name FROM Division WHERE id IN (SELECT divid FROM Employees WHERE salary <= SELECT name FROM Division WHERE id NOT IN (SELECT divid FROM Employees WHERE salary <= SELECT name FROM Division WHERE id NOT IN (SELECT divid FROM Employees WHERE salary <=

Matthew P. Johnson, OCL3, CISDD CUNY, June Acc(name,bal,type…) Q: Who has the largest balance? Can we do this with subqueries?

Matthew P. Johnson, OCL3, CISDD CUNY, June Last time: Acc(name,bal,type,…) Q: Find holder of largest account SELECT name FROM Acc WHERE bal >= ALL (SELECT bal FROM Acc) SELECT name FROM Acc WHERE bal >= ALL (SELECT bal FROM Acc) Correlated Queries

Matthew P. Johnson, OCL3, CISDD CUNY, June Correlated Queries So far, subquery executed once;  result used for higher query More complicated: correlated queries “[T]he subquery… [is] evaluated many times, once for each assignment of a value to some term in the subquery that comes from a tuple variable outside the subquery” (Ullman, p286). Q: What does this mean? A: That subqueries refer to vars from outer queries

Matthew P. Johnson, OCL3, CISDD CUNY, June Last time: Acc(name,bal,type,…) Q2: Find holder of largest account of each type SELECT name, type FROM Acc WHERE bal >= ALL (SELECT bal FROM Acc WHERE type=type) SELECT name, type FROM Acc WHERE bal >= ALL (SELECT bal FROM Acc WHERE type=type) Correlated Queries correlation

Matthew P. Johnson, OCL3, CISDD CUNY, June Last time: Acc(name,bal,type,…) Q2: Find holder of largest account of each type Note: 1. scope of variables 2. this can still be expressed as single SFW SELECT name, type FROM Acc a1 WHERE bal >= ALL (SELECT bal FROM Acc WHERE type=a1.type) SELECT name, type FROM Acc a1 WHERE bal >= ALL (SELECT bal FROM Acc WHERE type=a1.type) Correlated Queries correlation

Matthew P. Johnson, OCL3, CISDD CUNY, June EXCEPT and INTERSECT (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM R) EXCEPT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM R) EXCEPT (SELECT S.A, S.B FROM S) SELECT R.A, R.B FROM R WHERE EXISTS(SELECT * FROM S WHERE R.A=S.A and R.B=S.B) SELECT R.A, R.B FROM R WHERE EXISTS(SELECT * FROM S WHERE R.A=S.A and R.B=S.B) SELECT R.A, R.B FROM R WHERE NOT EXISTS(SELECT * FROM S WHERE R.A=S.A and R.B=S.B) SELECT R.A, R.B FROM R WHERE NOT EXISTS(SELECT * FROM S WHERE R.A=S.A and R.B=S.B)

Matthew P. Johnson, OCL3, CISDD CUNY, June More on Set-Comparison Operators We’ve already seen IN R, NOT IN R. Can also use EXISTS R, NOT EXISTS R Also available: op ANY R, op ALL R Find sailors whose rating is greater than that of some sailor called Alberto: SELECT R.SID FROM Reserves R WHERE R.rating > ANY ( SELECT R2.rating FROM Reserves R2 WHERE R2.sname=‘Alberto’)

Matthew P. Johnson, OCL3, CISDD CUNY, June Extended e.g. Scenario: 1. Purchase(pid, seller-ssn, buyer-ssn, etc.) 2. Person(ssn, name, etc.) 3. Product(pid, name, etc.) Q: Who (give names) bought gizmos from Dick? Where to start? Purchase uses pid, ssn, so must get them…

Matthew P. Johnson, OCL3, CISDD CUNY, June Last time: Complex RA Expressions Scenario: 1. Purchase(pid, seller-ssn, buyer-ssn, etc.) 2. Person(ssn, name, etc.) 3. Product(pid, name, etc.) Q: Who (give names) bought gizmos from Dick? Where to start? Purchase uses pid, ssn, so must get them…

Matthew P. Johnson, OCL3, CISDD CUNY, June Complex RA Expressions Person Purchase Person Product  name='Dick'  name='Gizmo'  pid  ssn seller-ssn=ssnpid=pidbuyer-ssn=Person.ssn  name

Matthew P. Johnson, OCL3, CISDD CUNY, June Translation to SQL We’re converting the tree on the last slide into SQL The result of the query should be the names indicated above One step at a time, we’ll make the query more complete, until we’ve translated the English-language description to an actual SQL query We’ll also simplify the query when possible (the names of the people who bought gadgets from Dick)

Matthew P. Johnson, OCL3, CISDD CUNY, June Translation to SQL Blue type = actual SQL Black italics = description of subquery Note: the subquery above consists of purchase records, except with the info describing the buyers attached  In the results, the column header for name will be 'buyer' SELECT DISTINCT name buyer FROM (the info, along with buyer names, for purchases of gadgets sold by Dick) SELECT DISTINCT name buyer FROM (the info, along with buyer names, for purchases of gadgets sold by Dick)

Matthew P. Johnson, OCL3, CISDD CUNY, June Translation to SQL Note: the subquery in this version is being given the name P2 We’re pairing our rows from Person with rows from P2 SELECT DISTINCT name buyer FROM (SELECT * FROM Person, (the purchases of gadgets from Dick) P2 WHERE Person.ssn = P2.buyer-ssn) SELECT DISTINCT name buyer FROM (SELECT * FROM Person, (the purchases of gadgets from Dick) P2 WHERE Person.ssn = P2.buyer-ssn)

Matthew P. Johnson, OCL3, CISDD CUNY, June Translation to SQL We simplified by combining the two SELECTs SELECT DISTINCT name buyer FROM Person, (the purchases of gadgets from Dick) P2 WHERE Person.ssn = P2.buyer-ssn SELECT DISTINCT name buyer FROM Person, (the purchases of gadgets from Dick) P2 WHERE Person.ssn = P2.buyer-ssn

Matthew P. Johnson, OCL3, CISDD CUNY, June Translation to SQL P2 is still the name of the subquery It’s just been filled in with a query that contains two subqueries Outer parentheses are bolded for clarity SELECT DISTINCT name buyer FROM Person, (SELECT * FROM Purchases WHERE seller-ssn = (Dick’s ssn) AND pid = (the id of gadget)) P2 WHERE Person.ssn = P2.buyer-ssn SELECT DISTINCT name buyer FROM Person, (SELECT * FROM Purchases WHERE seller-ssn = (Dick’s ssn) AND pid = (the id of gadget)) P2 WHERE Person.ssn = P2.buyer-ssn

Matthew P. Johnson, OCL3, CISDD CUNY, June Translation to SQL Now the subquery to find Dick’s ssn is filled in SELECT DISTINCT name buyer FROM Person, (SELECT * FROM Purchases WHERE seller-ssn = (SELECT ssn FROM Person WHERE name='Dick') AND pid = (the id of gadget)) P2 WHERE Person.ssn = P2.buyer-ssn SELECT DISTINCT name buyer FROM Person, (SELECT * FROM Purchases WHERE seller-ssn = (SELECT ssn FROM Person WHERE name='Dick') AND pid = (the id of gadget)) P2 WHERE Person.ssn = P2.buyer-ssn

Matthew P. Johnson, OCL3, CISDD CUNY, June Translation to SQL And now the subquery to find Gadget’s product id is filled in, too Note: the SQL simplified by using subqueries  Not used in relational algebra SELECT DISTINCT name buyer FROM Person, (SELECT * FROM Purchases WHERE seller-ssn = (SELECT ssn FROM Person WHERE name='Dick') AND pid = (SELECT pid FROM Product WHERE name='Gadget')) P2 WHERE Person.ssn = P2.buyer-ssn SELECT DISTINCT name buyer FROM Person, (SELECT * FROM Purchases WHERE seller-ssn = (SELECT ssn FROM Person WHERE name='Dick') AND pid = (SELECT pid FROM Product WHERE name='Gadget')) P2 WHERE Person.ssn = P2.buyer-ssn