Copyright Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables
4-2 Copyright Oracle Corporation, All rights reserved. Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using equality and nonequality joins View data that generally does not meet a join condition by using outer joins Join a table to itself After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using equality and nonequality joins View data that generally does not meet a join condition by using outer joins Join a table to itself
4-3 Copyright Oracle Corporation, All rights reserved. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS CHICAGO CHICAGO rows selected. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS CHICAGO CHICAGO rows selected. Obtaining Data from Multiple Tables EMPDEPT EMPNOENAME...DEPTNO KING BLAKE MILLER DEPTNO DNAME LOC ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON
4-4 Copyright Oracle Corporation, All rights reserved. What Is a Join? Use a join to query data from more than one table. Write the join condition in the WHERE clause. Prefix the column name with the table name when the same column name appears in more than one table. Use a join to query data from more than one table. Write the join condition in the WHERE clause. Prefix the column name with the table name when the same column name appears in more than one table. SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2;
4-5 Copyright Oracle Corporation, All rights reserved. Cartesian Product A Cartesian product is formed when: – A join condition is omitted – A join condition is invalid – All rows in the first table are joined to all rows in the second table To avoid a Cartesian product, always include a valid join condition in a WHERE clause. A Cartesian product is formed when: – A join condition is omitted – A join condition is invalid – All rows in the first table are joined to all rows in the second table To avoid a Cartesian product, always include a valid join condition in a WHERE clause.
4-6 Copyright Oracle Corporation, All rights reserved. Generating a Cartesian Product ENAME DNAME KINGACCOUNTING BLAKE ACCOUNTING... KINGRESEARCH BLAKE RESEARCH rows selected. ENAME DNAME KINGACCOUNTING BLAKE ACCOUNTING... KINGRESEARCH BLAKE RESEARCH rows selected. EMP (14 rows) DEPT (4 rows) EMPNOENAME...DEPTNO KING BLAKE MILLER EMPNOENAME...DEPTNO KING BLAKE MILLER DEPTNO DNAME LOC ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON DEPTNO DNAME LOC ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON “Cartesian product: 14*4=56 rows”
4-7 Copyright Oracle Corporation, All rights reserved. Types of Joins Equijoin Non-equijoin Outer join Self join
4-8 Copyright Oracle Corporation, All rights reserved. What Is an Equijoin? EMP DEPT EMPNO ENAME DEPTNO KING BLAKE CLARK JONES MARTIN ALLEN TURNER JAMES WARD FORD SMITH rows selected. DEPTNO DNAME LOC ACCOUNTING NEW YORK 30 SALES CHICAGO 10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS 30 SALES CHICAGO 20 RESEARCHDALLAS rows selected. Foreign key Primary key
4-9 Copyright Oracle Corporation, All rights reserved. Retrieving Records with Equijoins SQL> SELECT emp.empno, emp.ename, emp.deptno, 2dept.deptno, dept.loc 3 FROM emp, dept 4 WHERE emp.deptno=dept.deptno; EMPNO ENAME DEPTNO DEPTNO LOC KING NEW YORK 7698 BLAKE CHICAGO 7782 CLARK NEW YORK 7566 JONES DALLAS rows selected.
4-10 Copyright Oracle Corporation, All rights reserved. Qualifying Ambiguous Column Names Use table prefixes to qualify column names that are in multiple tables. Improve performance by using table prefixes. Distinguish columns that have identical names but reside in different tables by using column aliases. Use table prefixes to qualify column names that are in multiple tables. Improve performance by using table prefixes. Distinguish columns that have identical names but reside in different tables by using column aliases.
4-11 Copyright Oracle Corporation, All rights reserved. Additional Search Conditions Using the AND Operator EMPDEPT EMPNO ENAME DEPTNO KING BLAKE CLARK JONES MARTIN ALLEN TURNER JAMES WARD FORD SMITH rows selected. DEPTNO DNAME LOC ACCOUNTINGNEW YORK 30SALES CHICAGO 10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS 30 SALES CHICAGO 20 RESEARCHDALLAS rows selected.
4-12 Copyright Oracle Corporation, All rights reserved. Using Table Aliases Simplify queries by using table aliases. SQL> SELECT emp.empno, emp.ename, emp.deptno, 2 dept.deptno, dept.loc 3 FROM emp, dept 4 WHERE emp.deptno=dept.deptno; SQL> SELECT e.empno, e.ename, e.deptno, 2 d.deptno, d.loc 3 FROM emp e, dept d 4 WHERE e.deptno=d.deptno;
4-13 Copyright Oracle Corporation, All rights reserved. Joining More Than Two Tables NAMECUSTID JOCKSPORTS 100 TKB SPORT SHOP 101 VOLLYRITE 102 JUST TENNIS 103 K+T SPORTS 105 SHAPE UP 106 WOMENS SPORTS rows selected. NAMECUSTID JOCKSPORTS 100 TKB SPORT SHOP 101 VOLLYRITE 102 JUST TENNIS 103 K+T SPORTS 105 SHAPE UP 106 WOMENS SPORTS rows selected. CUSTOMER CUSTID ORDID rows selected. CUSTID ORDID rows selected.ORD ORDID ITEMID rows selected. ORDID ITEMID rows selected.ITEM
4-14 Copyright Oracle Corporation, All rights reserved. Non-Equijoins EMPSALGRADE “salary in the EMP table is between low salary and high salary in the SALGRADE table” EMPNO ENAME SAL KING BLAKE CLARK JONES MARTIN ALLEN TURNER JAMES rows selected. GRADE LOSAL HISAL
4-15 Copyright Oracle Corporation, All rights reserved. Retrieving Records with Non-Equijoins ENAME SAL GRADE JAMES SMITH ADAMS rows selected. SQL> SELECT e.ename, e.sal, s.grade 2FROMemp e, salgrade s 3WHERE e.sal 4BETWEEN s.losal AND s.hisal;
4-16 Copyright Oracle Corporation, All rights reserved. Outer Joins EMP DEPT No employee in the OPERATIONS department ENAMEDEPTNO KING10 BLAKE30 CLARK10 JONES20... DEPTNO DNAME ACCOUNTING 30 SALES 10 ACCOUNTING 20RESEARCH... 40OPERATIONS
4-17 Copyright Oracle Corporation, All rights reserved. Outer Joins You use an outer join to also see rows that do not usually meet the join condition. Outer join operator is the plus sign (+). You use an outer join to also see rows that do not usually meet the join condition. Outer join operator is the plus sign (+). SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column(+) = table2.column; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column(+) = table2.column; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column = table2.column(+); SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column = table2.column(+);
4-18 Copyright Oracle Corporation, All rights reserved. Using Outer Joins SQL> SELECTe.ename, d.deptno, d.dname 2 FROMemp e, dept d 3 WHEREe.deptno(+) = d.deptno 4 ORDER BYe.deptno; ENAME DEPTNO DNAME KING 10 ACCOUNTING CLARK 10 ACCOUNTING OPERATIONS 15 rows selected.
4-19 Copyright Oracle Corporation, All rights reserved. Self Joins EMP (WORKER) EMP (MANAGER) “MGR in the WORKER table is equal to EMPNO in the MANAGER table” EMPNOENAME MGR KING 7698BLAKE CLARK JONES MARTIN ALLEN7698 EMPNOENAME KING 7698BLAKE
4-20 Copyright Oracle Corporation, All rights reserved. Joining a Table to Itself WORKER.ENAME||'WORKSFOR'||MANAG BLAKE works for KING CLARK works for KING JONES works for KING MARTIN works for BLAKE rows selected. WORKER.ENAME||'WORKSFOR'||MANAG BLAKE works for KING CLARK works for KING JONES works for KING MARTIN works for BLAKE rows selected. SQL> SELECT worker.ename||' works for '||manager.ename 2 FROM emp worker, emp manager 3 WHERE worker.mgr = manager.empno;
4-21 Copyright Oracle Corporation, All rights reserved. Summary Equijoin Non-equijoin Outer join Self join SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2;
4-22 Copyright Oracle Corporation, All rights reserved. SQL:1999 Compliant Join Cross Joins Natural Joins Using Clause ON Clause Full or two sided outer joins. Cross Joins Natural Joins Using Clause ON Clause Full or two sided outer joins.
4-23 Copyright Oracle Corporation, All rights reserved. Cross Join: Same as Cartesian product Select empno,dname From emp Cross join dept; Select empno,dname from emp,dept; Select empno,dname From emp Cross join dept; Select empno,dname from emp,dept;
4-24 Copyright Oracle Corporation, All rights reserved. Natural Join or Join (Equi Join) Select empno,dname from emp Natural join dept; [where deptno in (10,30); Select empno,dname from emp,dept Where emp.deptno=dept.deptno; Select empno,dname from emp Natural join dept; [where deptno in (10,30); Select empno,dname from emp,dept Where emp.deptno=dept.deptno;
4-25 Copyright Oracle Corporation, All rights reserved. Join with USING clause Select empno,dname From emp join dept Using (deptno) [where deptno<>10] Select empno,dname From emp join dept Using (deptno) [where deptno<>10]
4-26 Copyright Oracle Corporation, All rights reserved. Join with ON clause Select empno,dname from emp e join dept d On (e.deptno=d.deptno); [where deptno in (10,30); [where deptno in (10,30); Can also be used for Self join: Select e.ename,m.ename From emp e join emp m On (e.mgr=m.empno) Select empno,dname from emp e join dept d On (e.deptno=d.deptno); [where deptno in (10,30); [where deptno in (10,30); Can also be used for Self join: Select e.ename,m.ename From emp e join emp m On (e.mgr=m.empno)
4-27 Copyright Oracle Corporation, All rights reserved. 3 way joins with ON clause Select empno,ename,dname,sal,grade from emp e Join dept d On e.deptno=d.deptno Join salgrade l On (sal between losal and hisal) Select empno,ename,dname,sal,grade from emp e Join dept d On e.deptno=d.deptno Join salgrade l On (sal between losal and hisal)
4-28 Copyright Oracle Corporation, All rights reserved. Left outer Join Select ename,d.deptno,dnamefrom emp Left outer join dept d On (emp.deptno=d.deptno); Select ename,d.deptno,dnamefrom emp Left outer join dept d On (emp.deptno=d.deptno);
4-29 Copyright Oracle Corporation, All rights reserved. Right outer Join Select ename,d.deptno,dname from dept d right outer join emp e On (e.deptno=d.deptno); Select ename,d.deptno,dname from dept d right outer join emp e On (e.deptno=d.deptno);
4-30 Copyright Oracle Corporation, All rights reserved. Full outer Join Select ename,d.deptno,dname from dept d full outer join emp e On (e.deptno=d.deptno); Select ename,d.deptno,dname from dept d full outer join emp e On (e.deptno=d.deptno);