Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © Ellis Cohen

Similar presentations


Presentation on theme: "Copyright © Ellis Cohen"— Presentation transcript:

1 Copyright © Ellis Cohen 2002-2008
Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Outer 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

2 Overview of Lecture Natural Outer Joins 1:M Outer Joins
Un-Natural Outer Joins Outer Join Conditions © Ellis Cohen

3 Natural Outer Joins © Ellis Cohen

4 Inner Joins are Restrictive
CoolEmps empno Given Emps, a table of employees, and CoolEmps, a list of the cool employees, what are the jobs of the cool employees 7782 7698 Restricts Emps information to just those employees in CoolEmps Emps empno ename job deptno 7782 CLARK CLERK 10 7369 SMITH 20 7499 ALLEN SALESMAN 30 7698 BLAKE MANAGER EMPNO JOB 7782 CLERK 7698 MANAGER SELECT empno, job FROM Emps NATURAL JOIN CoolEmps © Ellis Cohen

5 Joins Do Matching CoolEmps Emps EMPNO JOB ----- --------- 7782 CLERK
7401 7698 8315 If CoolEmps can contain employees who are no longer with the company, the join shows information only about employees who are in both tables Emps empno ename job deptno 7782 CLARK CLERK 10 7369 SMITH 20 7499 ALLEN SALESMAN 30 7698 BLAKE MANAGER EMPNO JOB 7782 CLERK 7698 MANAGER © Ellis Cohen

6 Can We Make Joins Inclusive?
CoolEmps Suppose we want our result to include a tuple for every employee in CoolEmps, whether they still are employed or not. If they are still employed, we still want to show their job. If they aren't still employed, they don't have a job, so just show NULL for their job How do we generate this? empno 7782 7401 7698 8315 Emps empno ename job deptno EMPNO JOB 7782 CLERK 7401 7698 MANAGER 8315 7782 CLARK CLERK 10 7369 SMITH 20 7499 ALLEN SALESMAN 30 7698 BLAKE MANAGER © Ellis Cohen

7 Outer Join = Inner Join + Remainder
CoolEmps EMPNO JOB 7782 CLERK 7698 MANAGER Inner Join empno 7782 7401 7698 8315 EMPNO JOB 7401 8315 CoolEmps not in Inner Join Emps empno ename job deptno EMPNO JOB 7782 CLERK 7401 7698 MANAGER 8315 7782 CLARK CLERK 10 7369 SMITH 20 7499 ALLEN SALESMAN 30 7698 BLAKE MANAGER © Ellis Cohen

8 Outer Joins are Inclusive
CoolEmps Include all tuples in the left operand empno 7782 7401 7698 8315 What are the jobs of the cool employees (showing jobs of NULL for those who are no longer with the company)? Emps EMPNO JOB 7782 CLERK 7401 7698 MANAGER 8315 empno ename job deptno 7782 CLARK CLERK 10 7369 SMITH 20 7499 ALLEN SALESMAN 30 7698 BLAKE MANAGER SELECT empno, job FROM CoolEmps NATURAL LEFT JOIN Emps © Ellis Cohen

9 Right vs Left Joins SELECT empno, job FROM Emps NATURAL RIGHT JOIN CoolEmps Equivalent SELECT empno, job FROM CoolEmps NATURAL LEFT JOIN Emps © Ellis Cohen

10 1:M Outer Joins © Ellis Cohen

11 Including all Children in 1:M
Show the names and department names of all employees. Include employees who are unassigned Emps Depts empno ename … deptno deptno dname … 7782 CLARK 10 7499 ALLEN 30 8214 JOAN 7698 BLAKE * 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS included in inner join * not included in inner join, but wanted in the result set The outer join provides extended information about a tuple (e.g. an employee), without losing information about tuples which do no match (e.g. are unassigned) © Ellis Cohen

12 1:M Child Outer Joins Emps Depts *
empno ename … deptno deptno dname … 7782 CLARK 10 7499 ALLEN 30 8214 JOAN 7698 BLAKE * 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS Show the names and department names of all employees. Include employees who are unassigned. ENAME DNAME CLARK ACCOUNTING ALLEN SALES JOAN BLAKE SALES SELECT ename, dname FROM Emps NATURAL LEFT JOIN Depts © Ellis Cohen

13 Including all Parents in 1:M
Show names and department names of the employees in each department. Include departments with no employees. Depts Emps deptno dname … empno ename … deptno 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS 7782 CLARK 10 7499 ALLEN 30 8214 JOAN 7698 BLAKE * * included in inner join * not included in inner join, but wanted in the result set © Ellis Cohen

14 1:M Parent Outer Joins Depts Emps * *
deptno dname … empno ename … deptno 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS 7782 CLARK 10 7499 ALLEN 30 8214 JOAN 7698 BLAKE * * DNAME ENAME ACCOUNTING CLARK RESEARCH SALES ALLEN SALES BLAKE OPERATIONS Show names and department names of the employees in each department. Include departments with no employees. SELECT dname, ename FROM Depts NATURAL LEFT JOIN Emps © Ellis Cohen

15 Outer Join = Inner Join + Remainder
Depts Emps deptno dname … empno ename … deptno 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS 7782 CLARK 10 7499 ALLEN 30 8214 JOAN 7698 BLAKE * * DNAME ENAME ACCOUNTING CLARK SALES ALLEN SALES BLAKE Inner Join DNAME ENAME ACCOUNTING CLARK RESEARCH SALES ALLEN SALES BLAKE OPERATIONS DNAME ENAME RESEARCH OPERATIONS Depts not in Inner Join © Ellis Cohen

16 Types of Joins Inner Join - when joining A & B, a row in A has a corresponding row in the result only if it matches some row in B (& vice versa) Outer Joins: LEFT JOIN - join A & B, but every row in A has at least one corresponding row in the result RIGHT JOIN - join A & B, but every row in B has at least one corresponding row in the result FULL JOIN - join A & B, but every row in A and every row in B has at least one corresponding row in the result © Ellis Cohen

17 Including All Parents & Children
Show the names and department names of all employees in all departments, including employees who are unassigned, and including departments with no employees Depts Emps deptno dname … empno ename … deptno 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS 7782 CLARK 10 7499 ALLEN 30 8214 JOAN 7698 BLAKE * * * © Ellis Cohen

18 1:M Full Join Depts Emps * * *
deptno dname … empno ename … deptno 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS 7782 CLARK 10 7499 ALLEN 30 8214 JOAN 7698 BLAKE * * * DNAME ENAME JOAN ACCOUNTING CLARK RESEARCH SALES ALLEN SALES BLAKE OPERATIONS SELECT dname, ename FROM Depts NATURAL FULL JOIN Emps © Ellis Cohen

19 Grouping with Outer Joins Question
SELECT deptno, count(*) AS knt FROM Emps GROUP BY deptno Counts # of employees in each department (that has employees) SELECT deptno, count(empno) AS knt FROM (Depts NATURAL LEFT JOIN Emps) GROUP BY deptno What does this return? Why use count(empno) instead of count(*)? © Ellis Cohen

20 (Just show deptno & empno) # of employees in each dept!
Outer Join & Group Depts Emps deptno dname … empno ename … deptno 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS 7782 CLARK 10 7499 ALLEN 30 8214 JOAN 7698 BLAKE * * Depts NATURAL LEFT JOIN Emps GROUP BY deptno (Just show deptno & empno) deptno count(*) count( empno) DEPTNO EMPNO 40 10 1 20 30 2 40 # of employees in each dept! © Ellis Cohen

21 Count Unassigned Employees
Depts Emps deptno dname … empno ename … deptno 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS 7782 CLARK 10 7499 ALLEN 30 8214 JOAN 7698 BLAKE * * * Depts NATURAL FULL JOIN Emps GROUP BY deptno count( empno) DEPTNO EMPNO 8214 40 deptno count(*) 1 10 20 30 2 40 © Ellis Cohen

22 Un-Natural Outer Joins
© Ellis Cohen

23 Un-Natural Outer Joins
For each project, list its name, and the name of its manager (show NULL if there is no manager) SELECT pname, ename FROM (Projs LEFT JOIN Emps ON pmgr = empno) Projs Emps pno pname pmgr persons budget pstart pend empno ename job mgr hiredate sal comm deptno © Ellis Cohen

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

25 Counts & Outer Joins SELECT count(*) AS tknt, count(pno) AS pknt, count(pmgr) AS pmknt, count(empno) AS eknt, count(DISTINCT pmgr) AS xpmknt, count(DISTINCT empno) AS xeknt FROM (Projs LEFT JOIN Emps ON pmgr = empno) PNO PMGR 1 2311 2 2955 3 4 4001 5 2311 6 2311 tknt # projects: 6 pknt # projects: 6 pmknt # managed projects: 5 eknt # managed project s: 5 xpmknt # project managers: 3 xeknt # project managers: 3 © Ellis Cohen

26 Reverse Outer Joins For each employee, list their name and the name of the projects they manage, including employees who manage no projects SELECT pname, ename FROM (Emps LEFT JOIN Projs ON pmgr = empno) empno ename job mgr hiredate sal comm deptno Emps Projs pno pname pmgr persons budget pstart pend © Ellis Cohen

27 Counts & Reverse Outer Joins?
What's the difference between the various counts? SELECT count(*) AS tknt, count(pno) AS pknt, count(pmgr) AS pmknt, count(empno) AS eknt, count(DISTINCT pmgr) AS xpmknt, count(DISTINCT empno) AS xeknt FROM (Emps LEFT JOIN Projs ON pmgr = empno) Emps PNO PMGR 1 2311 2 2955 3 4 4001 5 2311 6 2311 Projs empno ename job mgr hiredate sal comm deptno pno pname pmgr persons budget pstart pend 100 employees © Ellis Cohen

28 Counts & Reverse Outer Joins
SELECT count(*) AS tknt, count(pno) AS pknt, count(pmgr) AS pmknt, count(empno) AS eknt, count(DISTINCT pmgr) AS xpmknt, count(DISTINCT empno) AS xeknt FROM (Emps LEFT JOIN Projs ON pmgr = empno) tknt # employees: 100 pknt # managed projects: 5 pmknt # managed projects: 5 eknt # employees: 100 xpmknt # project managers: 3 xeknt # employees: 100 © Ellis Cohen

29 Grouped Counts & Reverse Outer Joins?
What's the difference between the various counts? (Assume that some projects are unmanaged) SELECT deptno, count(*) AS tknt, count(pmgr) AS pmknt, count(DISTINCT pmgr) AS xpmknt FROM (Emps LEFT JOIN Projs ON pmgr = empno) GROUP BY deptno Emps Projs empno ename job mgr hiredate sal comm deptno pno pname pmgr persons budget pstart pend © Ellis Cohen

30 Grouped Counts & Reverse Outer Joins
SELECT deptno, count(*) AS tknt, count(pmgr) AS pmknt, count(DISTINCT pmgr) AS xpmknt FROM (Emps LEFT JOIN Projs ON pmgr = empno) GROUP BY deptno For each department tknt # employees in that department pknt # projects managed by employees in that department xpmknt # project managers in that department © Ellis Cohen

31 Outer Join Conditions © Ellis Cohen

32 Outer Join Conditions vs. Restrictions?
For each budgeted project, list its name, and the name of its manager (show NULL if there is no manager) SELECT pname, ename FROM (Projs LEFT JOIN Emps ON pmgr = empno) WHERE budget IS NOT NULL What is the meaning of this? SELECT pname, ename FROM (Projs LEFT JOIN Emps ON (pmgr = empno) AND (budget IS NOT NULL)) © Ellis Cohen

33 Projs LEFT JOIN Emps ON pmgr = empno WHERE budget IS NOT NULL
JOIN + WHERE Projs Emps pno pname budget pmgr empno ename 1 P1 1000 1111 2 P2 2000 3 P3 3333 4 P4 1111 ALLEN 2222 MARTIN 3333 BLAKE 4444 KING Projs LEFT JOIN Emps ON pmgr = empno PNAME BUDGET ENAME P ALLEN P P BLAKE P4 PNAME ENAME P1 ALLEN P2 WHERE budget IS NOT NULL © Ellis Cohen

34 Projs LEFT JOIN Emps ON pmgr = empno AND budget IS NOT NULL
Just JOIN ON Projs Emps pno pname budget pmgr empno ename 1 P1 1000 1111 2 P2 2000 3 P3 3333 4 P4 1111 ALLEN 2222 MARTIN 3333 BLAKE 4444 KING Projs LEFT JOIN Emps ON pmgr = empno AND budget IS NOT NULL PNAME ENAME P1 ALLEN P2 P3 P4 Lists all projects ENAME shown only if project is managed project has budget © Ellis Cohen

35 Outer Join Conditions vs. Restrictions
For each budgeted project, list its name, and the name of its manager (show NULL if there is no manager) SELECT pname, ename FROM (Projs LEFT JOIN Emps ON pmgr = empno) WHERE budget IS NOT NULL For each project, list its name. If the project is budgeted, also show the name of the manager if there is one (else shown NULL) SELECT pname, ename FROM (Projs LEFT JOIN Emps ON (pmgr = empno) AND (budget IS NOT NULL)) © Ellis Cohen

36 Aggregation Problem Write SQL
For each employee, list the # of budgeted projects they manage (show 0 for employees that don't manage budgeted projects) © Ellis Cohen

37 Answer: Aggregation Problem
For each employee, list the # of budgeted projects they manage (show 0 for employees that don't manage budgeted projects) SELECT empno, count(pno) AS knt FROM (Emps LEFT JOIN Projs ON empno = pmgr AND budget IS NOT NULL) GROUP BY empno SELECT empno, count( CASE WHEN budget IS NOT NULL THEN pno END) FROM (Emps LEFT JOIN Projs ON empno = pmgr) GROUP BY empno © Ellis Cohen

38 Related Problems & Answers
For every employee that manages budgeted projects, list the # they manage SELECT pmgr, count(*) FROM Projs WHERE budget IS NOT NULL GROUP BY pmgr For every employee that manages projects, list the # of budgeted ones they manage SELECT pmgr, count( CASE WHEN budget IS NOT NULL THEN pno END) FROM Projs GROUP BY pmgr © Ellis Cohen


Download ppt "Copyright © Ellis Cohen"

Similar presentations


Ads by Google