Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 Inner Joins These slides are licensed.

Similar presentations


Presentation on theme: "1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 Inner Joins These slides are licensed."— Presentation transcript:

1 1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 Inner Joins These slides are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. For more information on how you may use them, please see http://www.openlineconsult.com/db

2 2 © Ellis Cohen 2001-2008 Overview of Lecture Cross Joins and Natural Joins Join Diagrams Restrictions and SemiJoins Self Joins More Complex Joins Joins with Duplicate Elimination Joins with Counting & Grouping State Assertions with Joins

3 3 © Ellis Cohen 2001-2008 Cross Joins & Natural Joins

4 4 © Ellis Cohen 2001-2008 Cross Join catid missy buffy puff Cats dogid rover spot ubu duke Dogs Suppose we want to generate all combinations of cats & dogs SELECT * FROM Cats, Dogs SELECT * FROM (Cats CROSS JOIN Dogs) 12 matches

5 5 © Ellis Cohen 2001-2008 Cross Product Joins SELECT * FROM Cats catid ----- missy buffy puff SELECT * FROM Dogs dogid ----- rover spot ubu duke SELECT * FROM Cats, Dogs catid dogid ----- missy rover missy spot missy ubu missy duke buffy rover buffy spot buffy ubu buffy duke puff rover puff spot puff ubu puff duke 12 tuples

6 6 © Ellis Cohen 2001-2008 Restricted Joins SELECT * FROM Cats, Dogs catid dogid ----- missy rover missy spot missy ubu missy duke buffy rover buffy spot buffy ubu buffy duke puff rover puff spot puff ubu puff duke SELECT * FROM Cats, Dogs WHERE length(catid) = length(dogid) catid dogid ----- missy rover buffy rover puff spot puff duke Restricted joins restrict a cross product with a join condition relating the columns from the specified tables Join condition

7 7 © Ellis Cohen 2001-2008 Restricting Matches catid missy buffy puff Cats dogid rover spot ubu duke Dogs Join conditions can also be thought of restricting which tuples are matched In SQL, this can be made explicit by writing SELECT * FROM ( Cats JOIN Dogs ON length(catid) = length(dogid) ) length(catid) = length(dogid) ON (required with plain JOIN) describes how they are joined

8 8 © Ellis Cohen 2001-2008 Qualified Names SELECT * FROM ( Cats JOIN Dogs ON length(catid) = length(dogid) ) SELECT * FROM ( Cats JOIN Dogs ON length(Cats.catid) = length(Dogs.dogid) ) In SQL, you can explicitly indicate which table an attribute comes from. Essential if joined tables have attributes with the same names

9 9 © Ellis Cohen 2001-2008 Natural Joins 30SALES 10ACCOUNTING 50SUPPORT deptno dname Depts empno ename address deptno 7499ALLEN...30 7654MARTIN…30 7777RUBY… 7839KING…10 7844TURNER…30 7986STERN…50 Emps SELECT ename, dname FROM (Emps JOIN Depts ON Emps.deptno = Depts.deptno) SELECT ename, dname FROM Emps, Depts WHERE Emps.deptno = Depts.deptno SELECT ename, dname FROM (Emps CROSS JOIN Depts) WHERE Emps.deptno = Depts.deptno SELECT ename, dname FROM (Emps NATURAL JOIN Depts) Because deptno is null, this tuple will not be part of any of these joins

10 10 © Ellis Cohen 2001-2008 Table Aliases SELECT ename, dname FROM Emps, Depts WHERE Emps.deptno = Depts.deptno SELECT ename, dname FROM Emps e, Depts d WHERE e.deptno = d.deptno SELECT ename, dname FROM (Emps e CROSS JOIN Depts d) WHERE e.deptno = d.deptno SELECT ename, dname FROM (Emps e JOIN Depts d ON e.deptno = d.deptno)

11 11 © Ellis Cohen 2001-2008 Join Exercise Show the names of the employees in the SALES department Emps( empno, ename, deptno ) Depts( deptno, dname ) Write the SQL using NATURAL JOIN CROSS JOIN

12 12 © Ellis Cohen 2001-2008 Join Exercise Answer Show the names of the employees in the SALES department SELECT ename FROM (Emps NATURAL JOIN Depts) WHERE dname = 'SALES' SELECT ename, dname FROM (Emps e CROSS JOIN Depts d) WHERE e.deptno = d.deptno AND dname = 'SALES' dname is unambiguous, so it doesn’t need a qualifier Now, use JOIN … ON

13 13 © Ellis Cohen 2001-2008 JOIN ON Answer S how the names of the employees in the SALES department SELECT ename, dname FROM (Emps e JOIN Depts d ON e.deptno = d.deptno AND dname = 'SALES') SELECT ename, dname FROM (Emps e JOIN Depts d ON e.deptno = d.deptno) WHERE dname = 'SALES' Join condition just used to characterize matching Ordinary Restriction Everything put into the join condition Better solution OK solution

14 14 © Ellis Cohen 2001-2008 Joins Do NOT Match NULLs catid missy buffy puff Cats dogid rover spot ubu duke Dogs SELECT * FROM Cats, Dogs SELECT * FROM (Cats CROSS JOIN Dogs) NULLs do not match anything, including other NULLs!

15 15 © Ellis Cohen 2001-2008 Join Diagrams

16 16 © Ellis Cohen 2001-2008 Joins Get Extended Information 30SALES 10ACCOUNTING 50SUPPORT deptno dname Depts empno ename addr deptno 7499ALLEN...30 7654MARTIN…30 7698RUBY… 7839KING…10 7844TURNER…30 7986STERN…50 Emps Joins provide access to extended information. Suppose we want to know the name of KING's dept. Emps provides us with the # of KING's dept. The NATURAL JOIN connect's KING's tuple to the tuple of KING's dept, from which we can get the department name: SELECT dname FROM (Emps NATURAL JOIN Depts) WHERE ename = 'KING'

17 17 © Ellis Cohen 2001-2008 Choose Columns to Join empno ename addr deptno 7499ALLEN...30 7654MARTIN…30 7698RUBY… 7839KING…10 7844TURNER…30 7986STERN…50 Emps Suppose we want to know the name of the manager of the Bridge Design project. Proj's provides us with the empno of the manager of the project. To get more information about that employee, we need to look in the Emps table, in particular, in the tuple whose empno matches pmgr is the Proj's table SELECT ename FROM (Projs JOIN Emps ON pmgr = empno) WHERE pname = 'Bridge Design' 2618Payroll7499 2621Bridge Design7844 2622Update Reqs7499 Projs pno pname pmgr

18 18 © Ellis Cohen 2001-2008 Using Join Diagrams For each project, list the name of the project, and the name of its manager empno ename job mgr hiredate sal comm deptno Emps Join Diagram SELECT pname, ename FROM (Projs JOIN Emps ON pmgr = empno) Use JOIN diagrams to show which attributes need to be connected to one another, and which need to be part of the result Do JOIN Diagram first Projs pno pname pmgr persons budget pstart pend Identify attributes which are part of the result

19 19 © Ellis Cohen 2001-2008 Join Conditions empno ename job mgr hiredate sal comm deptno Depts deptno dname loc Emps Cats catid Dogs dogid = length Join condition No join condition: Assumes columns are compared for equality (Equijoins)

20 20 © Ellis Cohen 2001-2008 Relational Schema vs Join Diagrams empno ename job mgr hiredate sal comm deptno Depts deptno dname loc Emps Cats catid Dogs dogid = length Cats catid Dogs dogid empno ename job mgr hiredate sal comm deptno Depts deptno dname loc Emps Relational Schema Join Diagram Shows referential integrity constraint (use elbow connectors, show arrowheads) Shows a specific join operation (use straight connectors, no arrowheads)

21 21 © Ellis Cohen 2001-2008 Equijoins SELECT ename, dname FROM (Emps e JOIN Depts d ON e.deptno = d.deptno) The join condition of an equijoin compares columns from each of the joined tables for equality A natural join is an equijoin which additionally requires that the columns have the same name SELECT pname, ename FROM (Projs JOIN Emps ON empno = pmgr)

22 22 © Ellis Cohen 2001-2008 Restriction & SemiJoins

23 23 © Ellis Cohen 2001-2008 Using Join Diagrams to Restrict List the name of DEPTMGRs (employees whose job is 'DEPTMGR') empno ename job mgr hiredate sal comm deptno Emps SELECT ename FROM Emps WHERE job = 'DEPTMGR' DEPTMGR

24 24 © Ellis Cohen 2001-2008 SemiJoins List the names of the employees whose department is in BOSTON empno ename job mgr hiredate sal comm deptno Emps SELECT ename FROM (Emps NATURAL JOIN Depts) WHERE loc = 'BOSTON' In a semijoin (of 2 tables) The result columns do NOT come from BOTH of the tables being joined One of the tables is just used to restrict tuples from the other table Depts deptno dname loc BOSTON

25 25 © Ellis Cohen 2001-2008 SemiJoin Exercise empno ename job mgr hiredate sal comm deptno Projs pno pname pmgr persons budget pstart pend Emps Show Join Diagram and SQL List the name of the projects managed by DEPTMGRs (employees whose job is 'DEPTMGR')

26 26 © Ellis Cohen 2001-2008 SemiJoin Exercise Answer List the name of the projects managed by DEPTMGRs (employees whose job is 'DEPTMGR') Emps SELECT pname FROM (Projs JOIN Emps ON pmgr = empno) WHERE job = 'DEPTMGR' empno ename job mgr hiredate sal comm deptno Projs pno pname pmgr persons budget pstart pend DEPTMGR

27 27 © Ellis Cohen 2001-2008 SemiJoin w Aggregation What is the # of projects managed by DEPTMGRs SELECT count(*) AS knt (Projs JOIN Emps ON pmgr = empno) WHERE job = 'DEPTMGR' empno ename job mgr hiredate sal comm deptno Emps Projs pno pname pmgr persons budget pstart pend DEPTMGR #

28 28 © Ellis Cohen 2001-2008 Natural SemiJoin Exercise What state does the employee with empno 4677 live in? How many employees work in the SALES department Emps Zips empno ename deptno mgr city zip state Depts deptno dname Show Join Diagrams & SQL

29 29 © Ellis Cohen 2001-2008 Answers to Natural SemiJoin Exercise Emps Zips empno ename deptno mgr city zip state 4677 Depts deptno dname SALES Emps empno ename deptno mgr city zip What state does the employee with empno 4677 live in? SELECT state FROM (Emps NATURAL JOIN Zips) WHERE empno = 4677 How many employees work in the SALES department? SELECT count(*) FROM (Depts NATURAL JOIN Emps) WHERE dname = 'SALES' #

30 30 © Ellis Cohen 2001-2008 Self Joins

31 31 © Ellis Cohen 2001-2008 Self Comparison What's the meaning of this join diagram? empno ename job mgr hiredate termdate sal comm deptno Emps

32 32 © Ellis Cohen 2001-2008 Self Comparison with Dates empno ename job mgr hiredate termdate sal comm deptno Emps List the names of all employees who were terminated the same day they were hired! SELECT ename FROM Emps WHERE hiredate = termdate empno ename job mgr hiredate termdate sal comm deptno Emps What's the meaning of this?

33 33 © Ellis Cohen 2001-2008 Self Comparison with Managers List the names of all employees who manage themselves! SELECT ename FROM Emps WHERE empno = mgr empno ename job mgr hiredate termdate sal comm deptno Emps

34 34 © Ellis Cohen 2001-2008 Self Join List the name of each employee along with the name of their manager empno ename job mgr hiredate sal comm deptno Emps e Join Diagram SELECT e.ename, m.ename AS mname FROM (Emps e JOIN Emps m ON e.mgr = m.empno) empno ename job mgr hiredate sal comm deptno Emps m mname Now we simultaneously need to consider two different employees e: the employee m: their manager and match them Pick aliases to be mnemonic & useful: e for employee, m for manager

35 35 © Ellis Cohen 2001-2008 Schema vs Join Diagram empno ename job mgr hiredate sal comm deptno Emps e empno ename job mgr hiredate sal comm deptno Emps Relational SchemaJoin Diagram empno ename job mgr hiredate sal comm deptno Emps m mname Reflects relational model with emphasis on foreign keys Reflects tuples that need to be matched for SQL Join

36 36 © Ellis Cohen 2001-2008 More Complex Joins

37 37 © Ellis Cohen 2001-2008 Multi-Table Join For each project, list the name of the project, and the name of the project manager's department empno ename job mgr hiredate sal comm deptno Emps e empno ename job mgr hiredate sal comm deptno Projs pno pname pmgr persons budget pstart pend Emps Relational SchemaJoin Diagram Projs pno pname pmgr persons budget pstart pend SELECT pname, dname FROM Projs, Emps e, Depts d WHERE empno = pmgr AND e.deptno = d.deptno Depts deptno dname loc Depts d deptno dname loc

38 38 © Ellis Cohen 2001-2008 SQL for Mixed JOIN Types SELECT pname, dname FROM Projs, Emps e, Depts d WHERE empno = pmgr AND e.deptno = d.deptno SELECT pname, dname FROM Projs, (Emps NATURAL JOIN Depts) WHERE empno = pmgr SELECT pname, dname FROM (Projs JOIN (Emps NATURAL JOIN Depts) ON empno = pmgr ) SELECT pname, dname FROM ( (Projs JOIN Emps ON empno = pmgr) NATURAL JOIN Depts ) Legal, but somewhat ugly

39 39 © Ellis Cohen 2001-2008 Multi-Table Self Join Exercise What are the names of employees who have managers who live in MA? Emps ZipState empno ename deptno mgr city zip state Show Join Diagram & SQL

40 40 © Ellis Cohen 2001-2008 Answer: Multi-Table Self Join Exercise What are the names of employees which have managers who live in MA? Emps m ZipState z empno ename deptno mgr city zip state Emps e empno ename deptno mgr city zip MA SELECT e.ename FROM Emps e, Emps m, ZipState z WHERE e.mgr = m.empno AND m.zip = z.zip AND z.state = 'MA' Use a Natural Join of m and z

41 41 © Ellis Cohen 2001-2008 Answer: Multi-Table Self Join Exercise What are the names of employees who have managers who live in MA? Emps m ZipState z empno ename deptno mgr city zip state Emps e empno ename deptno mgr city zip MA SELECT e.ename FROM Emps e, (Emps m NATURAL JOIN ZipState z) WHERE e.mgr = m.empno AND z.state = 'MA'

42 42 © Ellis Cohen 2001-2008 Shared-Reference Join Problem Suppose that every department has toys that can be used only by the department's employees and that Toys( toyno, deptno) indicates for each toy number, the number of the department that owns that toy Problem: List the (toyno's of the) toys that 'ALLEN' can use empno ename job mgr hiredate sal comm deptno Depts deptno dname loc Toys toyno deptno Emps Show (1) Join Diagram and (2) SQL

43 43 © Ellis Cohen 2001-2008 Non-Reference Join Answer List the toys that 'ALLEN' can use empno ename job mgr hiredate sal comm deptno Emps empno ename job mgr hiredate sal comm deptno Emps BEST Join Diagram SELECT toyno FROM (Toys NATURAL JOIN Emps) WHERE ename = 'ALLEN' Depts deptno dname loc Toys toyno deptno Toys toyno deptno ALLEN NOT needed in Join ALLEN

44 44 © Ellis Cohen 2001-2008 Explicit Toy Question empno ename address deptno 7499ALLEN...30 7654MARTIN…30 7698BLAKE…30 7839KING…10 7844TURNER…30 7986STERN…50 Emps toyno deptno 10430 10550 10640 10710 10830 10970 Toys Which employees can use which toys?

45 45 © Ellis Cohen 2001-2008 Explicit Toy Answer empno ename address deptno 7499ALLEN...30 7654MARTIN…30 7698BLAKE…30 7839KING…10 7844TURNER…30 7986STERN…50 Emps deptno toyno 30104 50105 40106 10107 30108 70109 Toys EMPNO TOYNO ----- 7499 104 7499 108 7654 104 7654 108 7698 104 7698 108 7839 107 7844 104 7844 108 SELECT empno, toyno FROM (Emps NATURAL JOIN Toys) 9 matches 9 tuples

46 46 © Ellis Cohen 2001-2008 General Theta Join: Join not based on Equality Show the names of pairs of employees whose salaries are within $200 of one another empno ename job mgr hiredate sal comm deptno Emps e1 empno ename job mgr hiredate sal comm deptno Emps Relational SchemaJoin Diagram empno ename job mgr hiredate sal comm deptno Emps e2 diff  200 nm1 nm2 Write the SQL

47 47 © Ellis Cohen 2001-2008 SQL for Theta Join SELECT e1.ename AS nm1, e2.ename AS nm2 FROM (Emps e1 JOIN Emps e2 ON abs(e1.sal - e2.sal) <= 200) Show the names of pairs of employees whose salaries are within $200 of one another empno ename job mgr hiredate sal comm deptno Emps e1 empno ename job mgr hiredate sal comm deptno Emps Relational SchemaJoin Diagram empno ename job mgr hiredate sal comm deptno Emps e2 diff  200 nm1 nm2

48 48 © Ellis Cohen 2001-2008 Joins with Duplicate Elimination

49 49 © Ellis Cohen 2001-2008 Simple Join Example 30SALES 10ACCOUNTING 40OPERATIONS 50SUPPORT deptno dname Depts empno ename address deptno 7499ALLEN...30 7654MARTIN…30 7698BLAKE…30 7839KING…10 7844TURNER…30 7986STERN…50 Emps ENAME DNAME ------ ALLEN SALES MARTIN SALES BLAKE SALES KING ACCOUNTING TURNER SALES STERN SUPPORT List the name & department name of every employee SELECT ename, dname FROM Emps NATURAL JOIN Depts Suppose we just select the department name? 6 matches 6 tuples

50 50 © Ellis Cohen 2001-2008 Joins with Duplication 30SALES 10ACCOUNTING 40OPERATIONS 50SUPPORT deptno dname Depts empno ename address deptno 7499ALLEN...30 7654MARTIN…30 7698BLAKE…30 7839KING…10 7844TURNER…30 7986STERN…50 Emps DNAME ------ SALES ACCOUNTING SALES SUPPORT List the department name of every employee SELECT dname FROM Emps NATURAL JOIN Depts 6 matches 6 tuples

51 51 © Ellis Cohen 2001-2008 Joins with Distinct 30SALES 10ACCOUNTING 40OPERATIONS 50SUPPORT deptno dname Depts empno ename address deptno 7499ALLEN...30 7654MARTIN…30 7698BLAKE…30 7839KING…10 7844TURNER…30 7986STERN…50 Emps List the name of departments that have employees SELECT DISTINCT dname FROM Emps NATURAL JOIN Depts DNAME ------ SALES ACCOUNTING SUPPORT

52 52 © Ellis Cohen 2001-2008 ER Models & DISTINCT Joins List the name of departments that have employees SELECT DISTINCT dname FROM Emps NATURAL JOIN Depts Depts deptno dname Emps empno ename deptno mgr city zip EmployeeDept The ER model indicates that there may be many employees per dept So, when joining Depts and Emps, # tuples in the result set > #departments If we only want information about each dept (e.g. dname), we will need to use DISTINCT distinct

53 53 © Ellis Cohen 2001-2008 DISTINCT Join Exercise List the names of employees who manage projects with budgets over 100000 empno ename job mgr hiredate sal comm deptno Projs pno pname pmgr persons budget pstart pend Emps Show Join Diagram and SQL EmployeeProject manage

54 54 © Ellis Cohen 2001-2008 Answer: DISTINCT Join Exercise List the names of employees who manage projects with budgets over 100000 empno ename job mgr hiredate sal comm deptno Emps Projs pno pname pmgr persons budget pstart pend SELECT DISTINCT ename FROM (Emps JOIN Projs ON empno = pmgr) WHERE budget > 100000 100000 > EmployeeProject manage distinct

55 55 © Ellis Cohen 2001-2008 Joins with Counting & Grouping

56 56 © Ellis Cohen 2001-2008 Counts & Joins? SELECT count(*) AS tknt, count(pno) AS pknt, count(pmgr) AS pmknt, count(DISTINCT pmgr) AS xpmknt, count(empno) AS eknt, count(DISTINCT empno) AS xeknt FROM (Projs JOIN Emps ON pmgr = empno) What's the difference between the various counts? (Assume that some projects are unmanaged) empno ename job mgr hiredate sal comm deptno Emps Projs pno pname pmgr persons budget pstart pend PNO PMGR --- ---- 1 2311 2 2955 3 4 4001 5 2311 6 2311

57 57 © Ellis Cohen 2001-2008 Counts & Joins SELECT count(*) AS tknt, count(pno) AS pknt, count(pmgr) AS pmknt, count(DISTINCT pmgr) AS xpmknt, count(empno) AS eknt, count(DISTINCT empno) AS xeknt FROM (Projs JOIN Emps ON pmgr = empno) tknt# managed projects: 5 pknt# managed projects 5 pmknt# managed projects: 5 xpmknt# project managers: 3 eknt# managed projects: 5 xeknt# project managers: 3 PNO PMGR --- ---- 1 2311 2 2955 3 4 4001 5 2311 6 2311 Joins do not match nulls!

58 58 © Ellis Cohen 2001-2008 Join with Counting Problem How many locations have analysts? empno ename job mgr hiredate sal comm deptno Emps Relational Schema Depts deptno dname loc Show (1) Join Diagram and (2) SQL

59 59 © Ellis Cohen 2001-2008 Join with Counting Answer How many locations have analysts? SELECT count(DISTINCT loc) FROM (Emps NATURAL JOIN Depts) WHERE job = 'ANALYST' empno ename job mgr hiredate sal comm deptno Emps empno ename job mgr hiredate sal comm deptno Emps Relational SchemaJoin Diagram Depts deptno dname loc Depts deptno dname loc ANALYST distinct #

60 60 © Ellis Cohen 2001-2008 Join with Grouping List the average salary of each department by name SELECT dname, avg(sal) AS avgsal FROM (Emps NATURAL JOIN Depts) GROUP BY dname empno ename job mgr hiredate sal comm deptno Emps empno ename job mgr hiredate sal comm deptno Emps Relational Schema Join Diagram Depts deptno dname loc Depts deptno dname loc avg by dname

61 61 © Ellis Cohen 2001-2008 Grouped Counts & Joins? SELECT count(*) AS tknt, count(pno) AS pknt, count(pmgr) AS pmknt, count(DISTINCT pmgr) AS xpmknt, count(empno) AS eknt, count(DISTINCT empno) AS xeknt FROM (Projs JOIN Emps ON pmgr = empno) GROUP BY deptno What's the difference between the various counts? (Assume that some projects are unmanaged) empno ename job mgr hiredate sal comm deptno Emps Projs pno pname pmgr persons budget pstart pend PNO PMGR --- ---- 1 2311 2 2955 3 4 4001 5 2311 6 2311 EMPNO DEPTNO ----- 2311 10 2522 10 2955 20 4001 20 6182 30 6189 30

62 62 © Ellis Cohen 2001-2008 Counts & Joins SELECT count(*) AS tknt, count(pno) AS pknt, count(pmgr) AS pmknt, count(DISTINCT pmgr) AS xpmknt, count(empno) AS eknt, count(DISTINCT empno) AS xeknt FROM (Projs JOIN Emps ON pmgr = empno) GROUP BY deptno tknt# projects managed by employees in each dept (3,2) pknt# projects managed by employees in each dept (3,2) pmknt# projects managed by employees in each dept (3,2) xpmknt# project managers in each dept (1,2) eknt# projects managed by employees in each dept (3,2) xeknt# project managers in each dept (1,2) Groups represent: departments with project managers

63 63 © Ellis Cohen 2001-2008 Join with Grouping Exercise List the number of project managers in each department (by name) empno ename job mgr hiredate sal comm deptno Projs pno pname pmgr persons budget pstart pend Emps Depts deptno dname loc Show (1) Join Diagram and (2) SQL

64 64 © Ellis Cohen 2001-2008 Join with Grouping Exercise Answer SELECT dname, count(DISTINCT empno) FROM (Depts NATURAL JOIN Emps), Projs WHERE empno = pmgr GROUP BY dname empno ename job mgr hiredate sal comm deptno Projs pno pname pmgr persons budget pstart pend Emps Depts deptno dname loc List the number of project managers in each named department (by name) distinct # by dname

65 65 © Ellis Cohen 2001-2008 State Assertions with Joins

66 66 © Ellis Cohen 2001-2008 Analyzing Complex Assertions There is an analyst at every department location empno ename job mgr hiredate sal comm deptno Emps empno ename job mgr hiredate sal comm deptno Emps Relational SchemaJoin Diagram Depts deptno dname loc Depts deptno dname loc ANALYST the # dept locations that have analysts =the # dept locations

67 67 © Ellis Cohen 2001-2008 Joins with Distinct Counts (SELECT count(DISTINCT loc) FROM (Emps NATURAL JOIN Depts) WHERE job = 'ANALYST') =(SELECT count(DISTINCT loc) FROM Depts) empno ename job mgr hiredate sal comm deptno Emps Depts deptno dname loc ANALYST the # dept locations that have analysts =the # dept locations distinct #

68 68 © Ellis Cohen 2001-2008 State Assertion Exercise Every department manager (i.e. employee with job of DEPTMGR) manages someone empno ename job mgr hiredate sal comm deptno Emps Compare the dept mgrs the dept mgrs who are also managers of some employee HINT Write the state assertion in Extended SQL

69 69 © Ellis Cohen 2001-2008 Answer to Assertion Exercise Every dept mgr manages someone empno ename job mgr hiredate sal comm deptno Emps m empno ename job mgr hiredate sal comm deptno Emps e (SELECT count(*) FROM Emps WHERE job = 'DEPTMGR') = (SELECT count(DISTINCT empno) FROM (Emps e JOIN Emps m ON e.mgr = m.empno) WHERE m.job = 'DEPTMGR') Compare the # of DEPTMGRS, and the # of DEPTMGRs who are managers of some employee DEPTMGR distinct #


Download ppt "1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 Inner Joins These slides are licensed."

Similar presentations


Ads by Google