Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Advanced Queries.

Similar presentations


Presentation on theme: "Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Advanced Queries."— Presentation transcript:

1 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Advanced Queries

2 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 2 NESTING OF QUERIES A complete SELECT query, called a nested query, can be specified within the WHERE-clause of another query, called the outer query Many of the previous queries can be specified in an alternative form using nesting Query 1: Retrieve the name and address of all employees who work for the 'Research' department. Q1:SELECTFNAME, LNAME, ADDRESS FROM EMPLOYEE WHEREDNO IN (SELECT DNUMBER FROMDEPARTMENT WHEREDNAME='Research' )

3 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 3 NESTING OF QUERIES Query 1: Retrieve the name and address of all employees who work for the 'Research' department. Q1:SELECTFNAME, LNAME, ADDRESS FROM EMPLOYEE WHEREDNO IN (SELECT DNUMBER FROMDEPARTMENT WHEREDNAME='Research' ) Can we write this query as follows? Q2:SELECTFNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHEREDNO=DNUMBER AND DNAME='Research' )

4 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 4 NESTING OF QUERIES (contd.) The nested query selects the number of the 'Research' department The outer query select an EMPLOYEE tuple if its DNO value is in the result of either nested query The comparison operator IN compares a value v with a set (or multi-set) of values V, and evaluates to TRUE if v is one of the elements in V In general, we can have several levels of nested queries A reference to an unqualified attribute refers to the relation declared in the innermost nested query In this example, the nested query is not correlated with the outer query

5 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 5 THE EXISTS FUNCTION EXISTS is used to check whether the result of a correlated nested query is empty (contains no tuples) or not We can formulate Query 12 in an alternative form that uses EXISTS as Q12B EXISTS will take a value of ‘true’ if the subquery returns an intermediate results table which contains one or more values, and ‘false’ if no rows are returned (opposite in NOT EXISTS). For NOT EXISTS, if the inner query does not match at least one condition, the result set will return an empty set.

6 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 6 THE EXISTS FUNCTION (contd.) Query 12: Retrieve the name of each employee who has a dependent with the same first name as the employee. Q12B: SELECT FNAME, LNAME FROMEMPLOYEE WHEREEXISTS (SELECT* FROMDEPENDENT WHERESSN=ESSN AND FNAME=DEPENDENT_NAME)

7 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 7 THE EXISTS FUNCTION (contd.) Query 6: Retrieve the names of employees who have no dependents. Q6:SELECT FNAME, LNAME FROMEMPLOYEE WHERENOT EXISTS (SELECT* FROM DEPENDENT WHERE SSN=ESSN) In Q6, the correlated nested query retrieves all DEPENDENT tuples related to an EMPLOYEE tuple. If none exist, the EMPLOYEE tuple is selected EXISTS is necessary for the expressive power of SQL

8 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 8 SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite SUPPLIER SNOSNAME 101 Global Books & Maps 102Nepalese Corp. 103 All Sports Manufacturing 104 Sweatshops Unlimited 105All Points, Inc. 106 São Paulo Manufacturing EMPLOYEE ENOENAMESALARYDEPTMGR 1Alice75000Management 2Ned45000Marketing1 3Andrew25000Marketing2 4Clare22000Marketing2 5Todd38000Accounting1 6Nancy22000Accounting5 7Brier43000Purchasing1 8Sarah56000Purchasing7 9Sophie35000Personnel1 10Sanjay15000Navigation3 11Rita15000Books4 12Gigi16000Clothes4 13Maggie16000Clothes4 14Paul11000Equipment3 15James15000Equipment3 16Pat15000Furniture3 17Mark15000Recreation3 SUPPLY DELNODELQTYITEMNAMEDEPTNAMESPLNO 5150Hat-polar explorerNavigation105 5210Pocket knife-NileBooks105 5310Pocket knife-NileClothes105 5410Pocket knife-NileEquipment105 5510Pocket knife-NileFurniture105 5610Pocket knife-NileRecreation105 612SextantNavigation101 621SextantEquipment105 661SextantBooks103 675SextantRecreation102 683SextantNavigation104 695Boots-snakeproofClothes105 7015Pith helmetClothes105 711Pith helmetClothes101 721Pith helmetClothes102 731Pith helmetClothes103 741SextantClothes104 755Pith helmetBooks105 765Pith helmetBooks105 775Pith helmetEquipment105 785Pith helmetFurniture105 795Pith helmetRecreation105 8010Pocket knife-NileBooks102 865Pocket knife-AvonRecreation102 962Boots-snakeproofEquipment105 9720Pith helmetEquipment106 9820Pocket knife-NileEquipment106 991SextantEquipment106

9 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Non-correlated subqueries Data is not dependent on the outer query. The query is executed only once for all tuples processed in the outer query. For example: Find the floors of all the departments which sell sextant. select floor from dept where dname in (select dept from sales where item=sextant) Slide 8- 9

10 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Non-correlated subqueries For example: Find the floors of all the departments which sell sextant. select floor from dept where dname in (select dept from sales where item=“sextant”) - First the inner query will be executed. The result will contain the departments which sell “sextant”. It wil be executed only once. - Second, the outer query will be executed based on the result of the inner query. It will find all the departments which match the inner query and return the floor numbers. Slide 8- 10

11 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries Uses the result of the inner query to determine the processing of the outer query. The inner query processes different for each time the outer query executes a row. For example let us revisit the previous example and try to solve it by using correlated subquery: Find the floors of all the departments which sell sextant. Slide 8- 11

12 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries Example: Find the floors of all the departments which sell sextant. select floor from dept d where exists (select * from sales s where s.item="sextant" and d.dname=s.dept) Slide 8- 12 SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 Get first row. Run the inner query for d.dname=“Management”. If query returns a null set, where clause will become false, so floor info will not be returned. Here, management department does not sell sextant hence floor 5 will not be returned.

13 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries Example: Find the floors of all the departments which sell sextant. select floor from dept d where exists (select * from sales s where s.item="sextant" and d.dname=s.dept) Slide 8- 13 SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 Get second row. Run the inner query for d.dname=“Books”. If query returns a null set, where clause will become false, so floor info will not be returned. Here, books department does not sell sextant hence floor 1 will not be returned.

14 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries Example: Find the floors of all the departments which sell sextant. select floor from dept d where exists (select * from sales s where s.item="sextant" and d.dname=s.dept) Slide 8- 14 SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 Get third row. Run the inner query for d.dname=“Clothes”. If query returns a null set, where clause will become false, so floor info will not be returned. Here, clothes department sells sextant hence floor 2 will be returned.

15 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries Example: Find the floors of all the departments which sell sextant. select floor from dept d where exists (select * from sales s where s.item="sextant" and d.dname=s.dept) Slide 8- 15 SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 Get fourth row. Run the inner query for d.dname=“Equipment”. If query returns a null set, where clause will become false, so floor info will not be returned. Here, equipment department sells sextant hence floor 3 will be returned.

16 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries Example: Find the floors of all the departments which sell sextant. select floor from dept d where exists (select * from sales s where s.item="sextant" and d.dname=s.dept) Slide 8- 16 SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 When all rows are processed, the result will be: Floor 2 -> Clothes Floor 3 -> Equipment Floor 1 -> Navigation Floor 2 -> Recreation

17 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries Example: Find the items sold by all departments on the second floor. The result will be : pocket-knife Nile, sextant SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5

18 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries Example: Find the items sold by all departments on the second floor. The result would be : pocket-knife Nile, sextant SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 select INAME from ITEM I where not exists (select * from DEPT D where FLOOR=2 and not exists (select * from SALES where ITEM=I.INAME and DEPT=D.DNAME)); ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite Get first item. Then, find departments on the second floor: clothes, recreation. The middle query is also a correlated query.

19 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries Example: Find the items sold by all departments on the second floor. The result would be : pocket-knife Nile, sextant SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 select INAME from ITEM I where not exists (select * from DEPT D where FLOOR=2 and not exists (select * from SALES where ITEM=I.INAME and DEPT=D.DNAME)); ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite Get first item. Then, find departments on the second floor: clothes, recreation. The middle query is also a correlated query. Clothes&Boots-snakeproof exists Recreation&Boots-snakeproof do not exist Not all the items exist so the query will be empty (null). Not exists return true. So we select the department. select * from DEPT D where FLOOR=2 and not exists (select * from SALES where ITEM=“Boots-snakeproof” and DEPT=D.DNAME));

20 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries Example: Find the items sold by all departments on the second floor. The result would be : pocket-knife Nile, sextant SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 select INAME from ITEM I where not exists (select * from DEPT D where FLOOR=2 and not exists (select * from SALES where ITEM=I.INAME and DEPT=D.DNAME)); ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite Get first item. Then, find departments on the second floor: clothes, recreation. The middle query is also a correlated query. Clothes&Boots-snakeproof exists Recreation&Boots-snakeproof do not exist Not all the items exist so the query will be empty (null). The outer query will return a null set since NOT EXISTS will be non empty set.

21 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries Example: Find the items sold by all departments on the second floor. The result would be : pocket-knife Nile, sextant SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 select INAME from ITEM I where not exists (select * from DEPT D where FLOOR=2 and not exists (select *from SALES where ITEM=I.INAME and DEPT=D.DNAME)); ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite Get second item. Then, find departments on the second floor: clothes, recreation. The middle query is also a correlated query. No camel saddle&Clothes No camel saddle&Recreation in Sales table No matching hence both middle and outer query will have empty set.

22 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries Example: Find the items sold by all departments on the second floor. The result would be : pocket-knife Nile, sextant SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 select INAME from ITEM I where not exists (select * from DEPT D where FLOOR=2 and not exists (select *from SALES where ITEM=I.INAME and DEPT=D.DNAME)); ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite Get second item. Then, find departments on the second floor: clothes, recreation. The middle query is also a correlated query. No camel saddle&Clothes No camel saddle&Recreation in Sales table No matching hence outer query will have empty set. select * from DEPT D where FLOOR=2 and not exists (select * from SALES where ITEM=“Camel saddle” and DEPT=D.DNAME));

23 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries For example: Find the items sold by all departments on the second floor. The result would be : pocket-knife Nile, sextant SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 select INAME from ITEM I where not exists (select * from DEPT D where FLOOR=2 and not exists (select *from SALES where ITEM=I.INAME and DEPT=D.DNAME)); ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite After running third, fourth items, get fifth item. Then, find departments on the second floor: clothes, recreation. The middle query is also a correlated query. Pocket knife-Nile&Clothes Pocket knife-Nile&Recreation Both exist and so outer query will have non empty result set. So select Pocket knife-Nill.

24 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries For example: Find the items sold by all departments on the second floor. The result would be : pocket-knife Nile, sextant SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 select INAME from ITEM I where not exists (select * from DEPT D where FLOOR=2 and not exists (select *from SALES where ITEM=I.INAME and DEPT=D.DNAME)); ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite After running third, fourth items, get fifth item. Then, find departments on the second floor: clothes, recreation. The middle query is also a correlated query. Pocket knife-Nile&Clothes Pocket knife-Nile&Recreation Both exist and so outer query will have non empty result set. So select Pocket knife-Nill. select * from DEPT D where FLOOR=2 and not exists (select * from SALES where ITEM=“Pocket knife-Nile” and DEPT=D.DNAME));

25 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries List the items sold only by departments that sell all the items they are supplied. The correct items are highlighted on the Item table. Slide 8- 25 DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 Help-desk2 ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation SUPPLY DELNODELQTYITEMNAMEDEPTNAMESPLNO 5150Hat-polar explorerNavigation105 5210Pocket knife-NileBooks105 5310Pocket knife-NileClothes105 5410Pocket knife-NileEquipment105 5510Pocket knife-NileFurniture105 5610Pocket knife-NileRecreation105 612SextantNavigation101 621SextantEquipment105 661SextantBooks103 675SextantRecreation102 683SextantNavigation104 695Boots-snakeproofClothes105 7015Pith helmetClothes105 711Pith helmetClothes101 721Pith helmetClothes102 731Pith helmetClothes103 741SextantClothes104 755Pith helmetBooks105 765Pith helmetBooks105 775Pith helmetEquipment105 785Pith helmetFurniture105 795Pith helmetRecreation105 8010Pocket knife-NileBooks102 865Pocket knife-AvonRecreation102 962Boots-snakeproofEquipment105 9720Pith helmetEquipment106 9820Pocket knife-NileEquipment106 991SextantEquipment106

26 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries List the items sold only by departments that sell all the items they are supplied. The correct items are highlighted on the Item table. Slide 8- 26 DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 Help-desk2 ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation SUPPLY DELNODELQTYITEMNAMEDEPTNAMESPLNO 5150Hat-polar explorerNavigation105 5210Pocket knife-NileBooks105 5310Pocket knife-NileClothes105 5410Pocket knife-NileEquipment105 5510Pocket knife-NileFurniture105 5610Pocket knife-NileRecreation105 612SextantNavigation101 621SextantEquipment105 661SextantBooks103 675SextantRecreation102 683SextantNavigation104 695Boots-snakeproofClothes105 7015Pith helmetClothes105 711Pith helmetClothes101 721Pith helmetClothes102 731Pith helmetClothes103 741SextantClothes104 755Pith helmetBooks105 765Pith helmetBooks105 775Pith helmetEquipment105 785Pith helmetFurniture105 795Pith helmetRecreation105 8010Pocket knife-NileBooks102 865Pocket knife-AvonRecreation102 962Boots-snakeproofEquipment105 9720Pith helmetEquipment106 9820Pocket knife-NileEquipment106 991SextantEquipment106 An item X will be sold by N departments. All departments sell the items that are in the supply. For example, “Boots-snakeproof” is sold by “Clothes”. Look at all the items of “Clothes” department in the “Supply”. The department sells “Pocket knife-Nile”, “Boots-snakeproof”, “Pith Helmet”, “Sextant”. Check whether all these items are in the “Sales”. Indeed they are. Likewise items in “Navigation”department. Books and Furniture-> did not sell any Recreation->did not sell Pocket knife-Avon, pitt helmet Equipment->did not sell everything except sextant and pitt helmet “Boots-snakeproof” is only sold by Clothes but not by any other one. “Hat-polar explorer” is only sold by Navigation but not by any other one.

27 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries List the items sold only by departments that sell all the items they are supplied. The correct items are highlighted on the Item table. Slide 8- 27 DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 Help-desk2 ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation SUPPLY DELNODELQTYITEMNAMEDEPTNAMESPLNO 5150Hat-polar explorerNavigation105 5210Pocket knife-NileBooks105 5310Pocket knife-NileClothes105 5410Pocket knife-NileEquipment105 5510Pocket knife-NileFurniture105 5610Pocket knife-NileRecreation105 612SextantNavigation101 621SextantEquipment105 661SextantBooks103 675SextantRecreation102 683SextantNavigation104 695Boots-snakeproofClothes105 7015Pith helmetClothes105 711Pith helmetClothes101 721Pith helmetClothes102 731Pith helmetClothes103 741SextantClothes104 755Pith helmetBooks105 765Pith helmetBooks105 775Pith helmetEquipment105 785Pith helmetFurniture105 795Pith helmetRecreation105 8010Pocket knife-NileBooks102 865Pocket knife-AvonRecreation102 962Boots-snakeproofEquipment105 9720Pith helmetEquipment106 9820Pocket knife-NileEquipment106 991SextantEquipment106 In the second step, consider “Navigation” and “Clothes” department. Identify an item which is only sold by one of these departments. Result: Boots-snakeproof by “Clothes” department Hat-polar explorer by “Navigation” department.

28 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries List the items sold only by departments that sell all the items they are supplied. The correct items are highlighted on the Item table. Slide 8- 28 DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 Help-desk2 ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation SUPPLY DELNODELQTYITEMNAMEDEPTNAMESPLNO 5150Hat-polar explorerNavigation105 5210Pocket knife-NileBooks105 5310Pocket knife-NileClothes105 5410Pocket knife-NileEquipment105 5510Pocket knife-NileFurniture105 5610Pocket knife-NileRecreation105 612SextantNavigation101 621SextantEquipment105 661SextantBooks103 675SextantRecreation102 683SextantNavigation104 695Boots-snakeproofClothes105 7015Pith helmetClothes105 711Pith helmetClothes101 721Pith helmetClothes102 731Pith helmetClothes103 741SextantClothes104 755Pith helmetBooks105 765Pith helmetBooks105 775Pith helmetEquipment105 785Pith helmetFurniture105 795Pith helmetRecreation105 8010Pocket knife-NileBooks102 865Pocket knife-AvonRecreation102 962Boots-snakeproofEquipment105 9720Pith helmetEquipment106 9820Pocket knife-NileEquipment106 991SextantEquipment106 Step 1: Take each item from “Item” table. Call it as X. Step 2: Find all the departments which sell X in the “Sales” table. Call it as Y. Step 3: Ensure that each entry in Y will sell all the items in “Supply” provided by themselves. Even an entry in Y does not satisfy the condition, do not count X anymore.

29 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries List the items sold only by departments that sell all the items they are supplied. The correct items are highlighted on the Item table. Slide 8- 29 DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 Help-desk2 ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation SUPPLY DELNODELQTYITEMNAMEDEPTNAMESPLNO 5150Hat-polar explorerNavigation105 5210Pocket knife-NileBooks105 5310Pocket knife-NileClothes105 5410Pocket knife-NileEquipment105 5510Pocket knife-NileFurniture105 5610Pocket knife-NileRecreation105 612SextantNavigation101 621SextantEquipment105 661SextantBooks103 675SextantRecreation102 683SextantNavigation104 695Boots-snakeproofClothes105 7015Pith helmetClothes105 711Pith helmetClothes101 721Pith helmetClothes102 731Pith helmetClothes103 741SextantClothes104 755Pith helmetBooks105 765Pith helmetBooks105 775Pith helmetEquipment105 785Pith helmetFurniture105 795Pith helmetRecreation105 8010Pocket knife-NileBooks102 865Pocket knife-AvonRecreation102 962Boots-snakeproofEquipment105 9720Pith helmetEquipment106 9820Pocket knife-NileEquipment106 991SextantEquipment106 We need to check every item iteratively. Let us check “sextant”. We need to find all the departments which sell it. Select Iname From Item I Where …. { select * from Sales S1 where I.Iname=S1.Item…. Aim to have “Clothes, Navigation, Equipment, Recreation” on hand.

30 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries List the items sold only by departments that sell all the items they are supplied. The correct items are highlighted on the Item table. Slide 8- 30 DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 Help-desk2 ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation SUPPLY DELNODELQTYITEMNAMEDEPTNAMESPLNO 5150Hat-polar explorerNavigation105 5210Pocket knife-NileBooks105 5310Pocket knife-NileClothes105 5410Pocket knife-NileEquipment105 5510Pocket knife-NileFurniture105 5610Pocket knife-NileRecreation105 612SextantNavigation101 621SextantEquipment105 661SextantBooks103 675SextantRecreation102 683SextantNavigation104 695Boots-snakeproofClothes105 7015Pith helmetClothes105 711Pith helmetClothes101 721Pith helmetClothes102 731Pith helmetClothes103 741SextantClothes104 755Pith helmetBooks105 765Pith helmetBooks105 775Pith helmetEquipment105 785Pith helmetFurniture105 795Pith helmetRecreation105 8010Pocket knife-NileBooks102 865Pocket knife-AvonRecreation102 962Boots-snakeproofEquipment105 9720Pith helmetEquipment106 9820Pocket knife-NileEquipment106 991SextantEquipment106 We will have “Clothes, Navigation, Equipment, Recreation” on hand. Then, for each department, we need to check whether all items are found in both “sales” and “supply”. Each department should be checked iteratively. Correlation should be based on “S1.dept”. Consider “Navigation” for example. But we also need to ensure that each item in “Supply” should be in “Sales” as well. Otherwise, we will not be sure about whether the query satisfies “all” criteria. So, items in “Supply” should be correlated. This should be checked via “Sales” table. But this will be a new copy of table. (no relation with the previous one). Select Iname From Item I Where …. exists { select * from Sales S1, Supply SU where I.Iname=S1.Item and S1.dept=SU.deptname Where..exists { select * from Sales S2 where S2.item= SU.itemname and S2.dept=S1.dept)

31 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries List the items sold only by departments that sell all the items they are supplied. The correct items are highlighted on the Item table. Slide 8- 31 DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 Help-desk2 ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation SUPPLY DELNODELQTYITEMNAMEDEPTNAMESPLNO 5150Hat-polar explorerNavigation105 5210Pocket knife-NileBooks105 5310Pocket knife-NileClothes105 5410Pocket knife-NileEquipment105 5510Pocket knife-NileFurniture105 5610Pocket knife-NileRecreation105 612SextantNavigation101 621SextantEquipment105 661SextantBooks103 675SextantRecreation102 683SextantNavigation104 695Boots-snakeproofClothes105 7015Pith helmetClothes105 711Pith helmetClothes101 721Pith helmetClothes102 731Pith helmetClothes103 741SextantClothes104 755Pith helmetBooks105 765Pith helmetBooks105 775Pith helmetEquipment105 785Pith helmetFurniture105 795Pith helmetRecreation105 8010Pocket knife-NileBooks102 865Pocket knife-AvonRecreation102 962Boots-snakeproofEquipment105 9720Pith helmetEquipment106 9820Pocket knife-NileEquipment106 991SextantEquipment106 “Exists” returns true even one of the departments is found as selling all items in “Supply”. But we want all. So we need to use “not exists”. Select Iname From Item I Where not exists { select * (below will be non-empty incorrect set, so it will be “not exists”. False will return.) from Sales S1, Supply SU where I.Iname=S1.Item and S1.dept=SU.deptname Where not exists { select * (true when the criteria is not met) from Sales S2 where S2.item= SU.itemname “Not exists” will return true if the set is null. If one of the departments does not follow the criteria, then t the result will be empty set. In order not to return that item, the first part will be also “not exists”. and S2.dept=S1.dept)

32 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries List the items sold only by departments that sell all the items they are supplied. The correct items are highlighted on the Item table. Slide 8- 32 DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 Help-desk2 ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite SALES SALENOVOLUMEITEMDEPT 10012Boots-snakeproofClothes 10021Pith helmetClothes 10031SextantNavigation 10043Hat-polar explorerNavigation 10055Pith helmetEquipment 10061Pocket knife-NileClothes 10071Pocket knife-NileRecreation 10161SextantClothes 10171SextantEquipment 10181SextantRecreation SUPPLY DELNODELQTYITEMNAMEDEPTNAMESPLNO 5150Hat-polar explorerNavigation105 5210Pocket knife-NileBooks105 5310Pocket knife-NileClothes105 5410Pocket knife-NileEquipment105 5510Pocket knife-NileFurniture105 5610Pocket knife-NileRecreation105 612SextantNavigation101 621SextantEquipment105 661SextantBooks103 675SextantRecreation102 683SextantNavigation104 695Boots-snakeproofClothes105 7015Pith helmetClothes105 711Pith helmetClothes101 721Pith helmetClothes102 731Pith helmetClothes103 741SextantClothes104 755Pith helmetBooks105 765Pith helmetBooks105 775Pith helmetEquipment105 785Pith helmetFurniture105 795Pith helmetRecreation105 8010Pocket knife-NileBooks102 865Pocket knife-AvonRecreation102 962Boots-snakeproofEquipment105 9720Pith helmetEquipment106 9820Pocket knife-NileEquipment106 991SextantEquipment106 Select Iname From Item I Where not exists { select * from Sales S1, Supply SU where I.Iname=S1.Item and S1.dept=SU.deptname Where not exists { select * from Sales S2 where S2.item= SU.itemname and S2.dept=S1.dept) 1. Select I.name = Books-snakeproof”. 2. The middle query returns this tuple. 3. For each item from “Clothes” department should be found in Sales S3 table. Check each row iteratively. If at least one does not satisfy, it will be null set.

33 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries List the items supplied to all departments on the second floor by all suppliers. Answer: sextant. Do not forget to select which fields should be checked iteratively and in what order. Slide 8- 33 ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite SUPPLIER SNOSNAME 101 Global Books & Maps 102Nepalese Corp. 105 All Sports Manufacturing SUPPLY DELNODELQTYITEMNAMEDEPTNAMESPLNO 5150Hat-polar explorerNavigation105 5210Pocket knife-NileBooks105 5310Pocket knife-NileClothes105 5410Pocket knife-NileEquipment105 5510Pocket knife-NileFurniture105 5610SextantRecreation102 612SextantClothes101 621SextantRecreation102 661SextantClothes102 675SextantRecreation101 683SextantNavigation105 695Boots-snakeproofClothes105 7015Pith helmetClothes105 711Pith helmetClothes101 721Pith helmetClothes102 731Pith helmetClothes105 741SextantClothes105 755Pith helmetRecreatıon105 765Pith helmetBooks105 775Pith helmetEquipment105 785Pith helmetFurniture105 795Pith helmetRecreation101 8010Pocket knife-NileBooks102 865Pocket knife-AvonRecreation102 962Boots-snakeproofEquipment105 9720Pith helmetEquipment105 9820Pocket knife-NileEquipment105 991SextantRecreation105 DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5 Table values are revised in order to demonstrate the query effects.

34 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries List the items supplied to all departments on the second floor by all suppliers. Answer: sextant. Item will be the top dependent. As all dept and suppliers should be checked on “Supply” table, they will be the second dependent items. Slide 8- 34 ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite SUPPLIER SNOSNAME 101 Global Books & Maps 102Nepalese Corp. 105 All Sports Manufacturing SUPPLY DELNODELQTYITEMNAMEDEPTNAMESPLNO 5150Hat-polar explorerNavigation105 5210Pocket knife-NileBooks105 5310Pocket knife-NileClothes105 5410Pocket knife-NileEquipment105 5510Pocket knife-NileFurniture105 5610SextantRecreation102 612SextantClothes101 621SextantRecreation102 661SextantClothes102 675SextantRecreation101 683SextantNavigation105 695Boots-snakeproofClothes105 7015Pith helmetClothes105 711Pith helmetClothes101 721Pith helmetClothes102 731Pith helmetClothes105 741SextantClothes105 755Pith helmetRecreatıon105 765Pith helmetBooks105 775Pith helmetEquipment105 785Pith helmetFurniture105 795Pith helmetRecreation101 8010Pocket knife-NileBooks102 865Pocket knife-AvonRecreation102 962Boots-snakeproofEquipment105 9720Pith helmetEquipment105 9820Pocket knife-NileEquipment105 991SextantRecreation105 DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5

35 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Correlated subqueries List the items supplied to all departments on the second floor by all suppliers. Answer: sextant. Select Iname From Item I Where not exists (select * from Dept D, Supplier S where d.floor=2 and not exists (select * from Supply where Deptname=D.Dname and Splno=S.Sno and Itemname=I.Iname )); Slide 8- 35 ITEM INAMETYPECOLOR Boots-snakeproofCGreen Camel saddleRBrown Pith helmetCKhaki Pocket knife-AvonEBrown Pocket knife-NileEBrown Safari chairFKhaki Safari cooking kitF-- SextantN-- Hat-polar explorerCWhite SUPPLIER SNOSNAME 101 Global Books & Maps 102Nepalese Corp. 105 All Sports Manufacturing SUPPLY DELNODELQTYITEMNAMEDEPTNAMESPLNO 5150Hat-polar explorerNavigation105 5210Pocket knife-NileBooks105 5310Pocket knife-NileClothes105 5410Pocket knife-NileEquipment105 5510Pocket knife-NileFurniture105 5610SextantRecreation102 612SextantClothes101 621SextantRecreation102 661SextantClothes102 675SextantRecreation101 683SextantNavigation105 695Boots-snakeproofClothes105 7015Pith helmetClothes105 711Pith helmetClothes101 721Pith helmetClothes102 731Pith helmetClothes105 741SextantClothes105 755Pith helmetRecreatıon105 765Pith helmetBooks105 775Pith helmetEquipment105 785Pith helmetFurniture105 795Pith helmetRecreation101 8010Pocket knife-NileBooks102 865Pocket knife-AvonRecreation102 962Boots-snakeproofEquipment105 9720Pith helmetEquipment105 9820Pocket knife-NileEquipment105 991SextantRecreation105 DEPT DNAMEFLOOR Management5 Books1 Clothes2 Equipment3 Furniture4 Navigation1 Recreation2 Accounting5 Purchasing5 Personnel5 Marketing5

36 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 36 CORRELATED NESTED QUERIES If a condition in the WHERE-clause of a nested query references an attribute of a relation declared in the outer query, the two queries are said to be correlated The result of a correlated nested query is different for each tuple (or combination of tuples) of the relation(s) the outer query Query 12: Retrieve the name of each employee who has a dependent with the same first name as the employee. Q12: SELECT E.FNAME, E.LNAME FROMEMPLOYEE AS E WHEREE.SSN IN (SELECT ESSN FROMDEPENDENT WHEREESSN=E.SSN AND E.FNAME=DEPENDENT_NAME)

37 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 37 CORRELATED NESTED QUERIES (contd.) In Q12, the nested query has a different result in the outer query A query written with nested SELECT... FROM... WHERE... blocks and using the = or IN comparison operators can always be expressed as a single block query. For example, Q12 may be written as in Q12A Q12A:SELECT E.FNAME, E.LNAME FROMEMPLOYEE E, DEPENDENT D WHEREE.SSN=D.ESSN AND E.FNAME=D.DEPENDENT_NAME

38 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 38 EXPLICIT SETS It is also possible to use an explicit (enumerated) set of values in the WHERE- clause rather than a nested query Query 13: Retrieve the social security numbers of all employees who work on project number 1, 2, or 3. Q13:SELECT DISTINCT ESSN FROMWORKS_ON WHEREPNO IN (1, 2, 3)

39 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 39 NULLS IN SQL QUERIES SQL allows queries that check if a value is NULL (missing or undefined or not applicable) SQL uses IS or IS NOT to compare NULLs because it considers each NULL value distinct from other NULL values, so equality comparison is not appropriate. Query 14: Retrieve the names of all employees who do not have supervisors. Q14:SELECT FNAME, LNAME FROMEMPLOYEE WHERESUPERSSN IS NULL Note: If a join condition is specified, tuples with NULL values for the join attributes are not included in the result

40 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 40 Joined Relations Feature in SQL2 Can specify a "joined relation" in the FROM- clause Looks like any other relation but is the result of a join Allows the user to specify different types of joins (regular "theta" JOIN, NATURAL JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, CROSS JOIN, etc)

41 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 41 Joined Relations Feature in SQL2 (contd.) Examples: Q8:SELECTE.FNAME, E.LNAME, S.FNAME, S.LNAME FROM EMPLOYEE E S WHEREE.SUPERSSN=S.SSN can be written as: Q8:SELECTE.FNAME, E.LNAME, S.FNAME, S.LNAME FROM (EMPLOYEE E LEFT OUTER JOIN EMPLOYEE S ON E.SUPERSSN=S.SSN)

42 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 42 Joined Relations Feature in SQL2 (contd.) Examples: Q1:SELECTFNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHEREDNAME='Research' AND DNUMBER=DNO could be written as: Q1:SELECTFNAME, LNAME, ADDRESS FROM (EMPLOYEE JOIN DEPARTMENT ON DNUMBER=DNO) WHEREDNAME='Research’ or as: Q1:SELECTFNAME, LNAME, ADDRESS FROM (EMPLOYEE NATURAL JOIN DEPARTMENT AS DEPT(DNAME, DNO, MSSN, MSDATE) WHEREDNAME='Research’

43 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 43 Joined Relations Feature in SQL2 (contd.) Another Example: Q2 could be written as follows; this illustrates multiple joins in the joined tables Q2:SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS FROM(PROJECT JOIN DEPARTMENT ON DNUM=DNUMBER) JOIN EMPLOYEE ON MGRSSN=SSN) ) WHERE PLOCATION='Stafford’

44 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 44 AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the average salary among all employees. Q15:SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROMEMPLOYEE Some SQL implementations may not allow more than one function in the SELECT-clause

45 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 45 AGGREGATE FUNCTIONS (contd.) Query 16: Find the maximum salary, the minimum salary, and the average salary among employees who work for the 'Research' department. Q16: SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROMEMPLOYEE, DEPARTMENT WHEREDNO=DNUMBER AND DNAME='Research'

46 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 46 AGGREGATE FUNCTIONS (contd.) Queries 17 and 18: Retrieve the total number of employees in the company (Q17), and the number of employees in the 'Research' department (Q18). Q17:SELECT COUNT (*) FROMEMPLOYEE Q18:SELECT COUNT (*) FROMEMPLOYEE, DEPARTMENT WHEREDNO=DNUMBER AND DNAME='Research’

47 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 47 GROUPING In many cases, we want to apply the aggregate functions to subgroups of tuples in a relation Each subgroup of tuples consists of the set of tuples that have the same value for the grouping attribute(s) The function is applied to each subgroup independently SQL has a GROUP BY-clause for specifying the grouping attributes, which must also appear in the SELECT-clause

48 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 48 GROUPING (contd.) Query 20: For each department, retrieve the department number, the number of employees in the department, and their average salary. Q20:SELECT DNO, COUNT (*), AVG (SALARY) FROMEMPLOYEE GROUP BYDNO In Q20, the EMPLOYEE tuples are divided into groups- Each group having the same value for the grouping attribute DNO The COUNT and AVG functions are applied to each such group of tuples separately The SELECT-clause includes only the grouping attribute and the functions to be applied on each group of tuples A join condition can be used in conjunction with grouping

49 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 49 GROUPING (contd.) Query 21: For each project, retrieve the project number, project name, and the number of employees who work on that project. Q21:SELECT PNUMBER, PNAME, COUNT (*) FROMPROJECT, WORKS_ON WHEREPNUMBER=PNO GROUP BYPNUMBER, PNAME In this case, the grouping and functions are applied after the joining of the two relations

50 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 50 THE HAVING-CLAUSE Sometimes we want to retrieve the values of these functions for only those groups that satisfy certain conditions The HAVING-clause is used for specifying a selection condition on groups (rather than on individual tuples)

51 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 51 THE HAVING-CLAUSE (contd.) Query 22: For each project on which more than two employees work, retrieve the project number, project name, and the number of employees who work on that project. Q22: SELECT PNUMBER, PNAME, COUNT(*) FROMPROJECT, WORKS_ON WHEREPNUMBER=PNO GROUP BYPNUMBER, PNAME HAVINGCOUNT (*) > 2

52 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 52 SUBSTRING COMPARISON The LIKE comparison operator is used to compare partial strings Two reserved characters are used: '%' (or '*' in some implementations) replaces an arbitrary number of characters, and '_' replaces a single arbitrary character

53 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 53 SUBSTRING COMPARISON (contd.) Query 25: Retrieve all employees whose address is in Houston, Texas. Here, the value of the ADDRESS attribute must contain the substring 'Houston,TX‘ in it. Q25:SELECT FNAME, LNAME FROMEMPLOYEE WHEREADDRESS LIKE '%Houston,TX%'

54 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 54 SUBSTRING COMPARISON (contd.) Query 26: Retrieve all employees who were born during the 1950s. Here, '5' must be the 8th character of the string (according to our format for date), so the BDATE value is '_______5_', with each underscore as a place holder for a single arbitrary character. Q26:SELECT FNAME, LNAME FROMEMPLOYEE WHEREBDATE LIKE'_______5_’ The LIKE operator allows us to get around the fact that each value is considered atomic and indivisible Hence, in SQL, character string attribute values are not atomic

55 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 55 ARITHMETIC OPERATIONS The standard arithmetic operators '+', '-'. '*', and '/' (for addition, subtraction, multiplication, and division, respectively) can be applied to numeric values in an SQL query result Query 27: Show the effect of giving all employees who work on the 'ProductX' project a 10% raise. Q27:SELECT FNAME, LNAME, 1.1*SALARY FROMEMPLOYEE, WORKS_ON, PROJECT WHERESSN=ESSN AND PNO=PNUMBER AND PNAME='ProductX’

56 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 56 ORDER BY The ORDER BY clause is used to sort the tuples in a query result based on the values of some attribute(s) Query 28: Retrieve a list of employees and the projects each works in, ordered by the employee's department, and within each department ordered alphabetically by employee last name. Q28: SELECT DNAME, LNAME, FNAME, PNAME FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT WHEREDNUMBER=DNO AND SSN=ESSN AND PNO=PNUMBER ORDER BYDNAME, LNAME

57 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 57 ORDER BY (contd.) The default order is in ascending order of values We can specify the keyword DESC if we want a descending order; the keyword ASC can be used to explicitly specify ascending order, even though it is the default

58 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 58 Summary of SQL Queries A query in SQL can consist of up to six clauses, but only the first two, SELECT and FROM, are mandatory. The clauses are specified in the following order: SELECT FROM [WHERE ] [GROUP BY ] [HAVING ] [ORDER BY ]

59 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Assignment The SQL database consists of the following relations: SALES(DEPT,ITEM,VOLUME) DEPT(DNAME,FLOOR) ITEM(INAME,TYPE,COLOR) EMPLOYEE(ENO,ENAME,MGR,DEPT,SALARY,JOB- STATUS) SUPPLIER(SNO,SNAME) SUPPLY(SUPPLIER,DEPT,ITEM,VOLUME) Note: Keys are given bold. For the queries you do not need them. Slide 8- 59

60 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Query 1: Find the items sold by no department on the second floor. Query 2: Find the items sold by all departments on the second floor. Query 3: Find the items sold by at least two departments on the second floor. Query 4: Find the departments where all the employees earn less than their manager. Query 5: Find the names of the employees who are in the same department as their manager (as an employee) Query 6: List the name and salary of the managers who earn more than ten employees. Query 7:List the items supplied by all suppliers that supply all items of type A. Query 8: Find the suppliers that supply pens. Query 9: Find the suppliers that do no supply pens. Query 10: List the departments that do not sell all the items of type A. Slide 8- 60


Download ppt "Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Advanced Queries."

Similar presentations


Ads by Google