Displaying Data from Multiple Tables
ObjectivesAfter 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 joinsView data that generally does not meet a join condition by using outer joinsJoin a table to itself
Obtaining Data from Multiple TablesEMP DEPT  EMPNOENAME	...	DEPTNO------	-----	...	------7839KING	...	    107698BLAKE	...	    30   ...	7934MILLER	...	    10DEPTNO DNAME     LOC     ------ ----------	--------10ACCOUNTINGNEW YORK20RESEARCHDALLAS30SALESCHICAGO40OPERATIONSBOSTONEMPNO DEPTNO LOC----- ------- -------- 783910 NEW YORK769830 CHICAGO778210 NEW YORK756620 DALLAS765430 CHICAGO749930 CHICAGO...14 rows selected.
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.SELECTtable1.column, table2.columnFROMtable1, table2WHEREtable1.column1= table2.column2;
Cartesian ProductA Cartesian product is formed when:A join condition is omittedA join condition is invalidAll rows in the first table are joined to all rows in the second tableTo avoid a Cartesian product, always include a valid join condition in a WHERE clause.
Generating a Cartesian Product“Cartesianproduct: 14*4=56 rows”EMP (14 rows) DEPT (4 rows)  EMPNOENAME	...	DEPTNO------	-----	...	------7839KING	...	    107698BLAKE	...	    30   ...	7934MILLER	...	    10DEPTNO DNAME     LOC     ------ ----------	--------10ACCOUNTINGNEW YORK20RESEARCHDALLAS30SALESCHICAGO40OPERATIONSBOSTONENAME    DNAME------ 	----------KINGACCOUNTINGBLAKEACCOUNTING ...KINGRESEARCHBLAKERESEARCH...56 rows selected.
Types of JoinsEquijoinNon-equijoinOuter joinSelf join
What Is an Equijoin?Foreign keyPrimary keyEMP DEPT  EMPNO ENAME    DEPTNO------ ------- -------7839 KING         107698 BLAKE        307782 CLARK        107566 JONES        207654 MARTIN       307499 ALLEN        307844 TURNER       307900 JAMES        307521 WARD         307902 FORD         207369 SMITH        20...14 rows selected. DEPTNO DNAME      LOC     ------- ---------- --------10 ACCOUNTING NEW YORK30 SALESCHICAGO10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS30 SALESCHICAGO30 SALESCHICAGO30 SALESCHICAGO30 SALESCHICAGO30 SALESCHICAGO20 RESEARCHDALLAS20 RESEARCHDALLAS...14 rows selected.
Retrieving Records with EquijoinsSQL> SELECT emp.empno, emp.ename, emp.deptno,2dept.deptno, dept.loc3  FROM   emp, dept4  WHERE  emp.deptno=dept.deptno;EMPNO ENAME DEPTNO DEPTNO LOC----- ------ ------ ------ ---------7839 KING1010 NEW YORK7698 BLAKE  3030 CHICAGO7782 CLARK1010 NEW YORK7566 JONES      2020 DALLAS...14 rows selected.
Qualifying Ambiguous Column NamesUse 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.
Additional Search ConditionsUsing the AND Operator EMP DEPT  EMPNO ENAME    DEPTNO------ ------- -------7839 KING         107698 BLAKE        307782 CLARK        107566 JONES        207654 MARTIN       307499 ALLEN        307844 TURNER       307900 JAMES        307521 WARD         307902 FORD         207369 SMITH        20...14 rows selected.DEPTNO DNAME     LOC     ------ ---------	--------10 ACCOUNTINGNEW YORK30SALES    CHICAGO10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS30 SALES    CHICAGO30 SALES    CHICAGO30 SALES    CHICAGO30 SALES    CHICAGO30 SALES    CHICAGO20 RESEARCHDALLAS20 RESEARCHDALLAS...14 rows selected.
Using Table AliasesSimplify queries by using table aliases.SQL> SELECT emp.empno, emp.ename, emp.deptno,  2   dept.deptno, dept.loc3  FROM   emp, dept4  WHERE  emp.deptno=dept.deptno;SQL> SELECT e.empno, e.ename, e.deptno,   2         d.deptno, d.loc3  FROM   emp e, dept d4  WHERE  e.deptno=d.deptno;
Joining More Than Two TablesORD  CUSTID   ORDID------- -------101610102611104612106601102602106604106605... 21 rows selected.ITEM  ORDID  ITEMID------ -------61036111612160116021...64 rows selected.     CUSTOMER NAMECUSTID-----------	------JOCKSPORTS100TKB SPORT SHOP101VOLLYRITE102JUST TENNIS103K+T SPORTS105SHAPE UP106WOMENS SPORTS     107...	...9 rows selected.
Non-Equijoins“salary in the EMP table is between low salary and high salary in the SALGRADEtable”EMPSALGRADE EMPNO ENAME      SAL------ ------- ------7839 KING      50007698 BLAKE     28507782 CLARK     24507566 JONES     29757654 MARTIN    12507499 ALLEN     16007844 TURNER    15007900 JAMES      950...14 rows selected.GRADE LOSAL  HISAL----- ----- ------17001200212011400314012000420013000530019999
Retrieving Records with Non-EquijoinsSQL> SELECT e.ename, e.sal, s.grade2FROMemp e, salgrade s3WHERE e.sal4BETWEEN s.losal AND s.hisal;ENAME            SAL     GRADE---------- --------- ---------JAMES            9501SMITH            8001ADAMS           11001...14 rows selected.
Outer JoinsNo employee in theOPERATIONS departmentEMP DEPT ENAMEDEPTNO-----	------KING10BLAKE30CLARK10JONES20...	DEPTNO DNAME------ ----------10ACCOUNTING30SALES10ACCOUNTING20RESEARCH...	40OPERATIONS
Outer JoinsYou 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.columnFROMtable1, table2WHEREtable1.column(+)= table2.column;SELECTtable1.column, table2.columnFROMtable1, table2WHEREtable1.column = table2.column(+);
Using Outer JoinsSQL> SELECTe.ename, d.deptno, d.dname2  FROMemp e, dept d3  WHEREe.deptno(+) = d.deptno4  ORDER BYe.deptno;ENAME         DEPTNO DNAME---------- --------- -------------KING              10 ACCOUNTINGCLARK             10 ACCOUNTING...40 OPERATIONS15 rows selected.
Self Joins“MGR in the WORKER table is equal to EMPNO in the MANAGER table”EMP (WORKER)EMP (MANAGER)EMPNOENAME MGR-----	------	----7839KING7698BLAKE78397782CLARK78397566JONES78397654MARTIN76987499ALLEN7698EMPNOENAME-----	--------7839KING7839KING7839KING7698BLAKE7698BLAKE
Joining a Table to ItselfSQL> SELECT worker.ename||' works for '||manager.ename2  FROM emp worker, emp manager3  WHERE worker.mgr = manager.empno;WORKER.ENAME||'WORKSFOR'||MANAG-------------------------------BLAKE works for KINGCLARK works for KINGJONES works for KINGMARTIN works for BLAKE...13 rows selected.
SummarySELECTtable1.column, table2.columnFROMtable1, table2WHEREtable1.column1= table2.column2;EquijoinNon-equijoinOuter joinSelf join
Practice OverviewJoining tables using an equijoinPerforming outer and self joinsAdding conditions

Les04 Displaying Data From Multiple Table

  • 1.
    Displaying Data fromMultiple Tables
  • 2.
    ObjectivesAfter completing thislesson, you should be able to do the following:Write SELECT statements to access data from more than one table using equality and nonequality joinsView data that generally does not meet a join condition by using outer joinsJoin a table to itself
  • 3.
    Obtaining Data fromMultiple TablesEMP DEPT EMPNOENAME ... DEPTNO------ ----- ... ------7839KING ... 107698BLAKE ... 30 ... 7934MILLER ... 10DEPTNO DNAME LOC ------ ---------- --------10ACCOUNTINGNEW YORK20RESEARCHDALLAS30SALESCHICAGO40OPERATIONSBOSTONEMPNO DEPTNO LOC----- ------- -------- 783910 NEW YORK769830 CHICAGO778210 NEW YORK756620 DALLAS765430 CHICAGO749930 CHICAGO...14 rows selected.
  • 4.
    What Is aJoin?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.columnFROMtable1, table2WHEREtable1.column1= table2.column2;
  • 5.
    Cartesian ProductA Cartesianproduct is formed when:A join condition is omittedA join condition is invalidAll rows in the first table are joined to all rows in the second tableTo avoid a Cartesian product, always include a valid join condition in a WHERE clause.
  • 6.
    Generating a CartesianProduct“Cartesianproduct: 14*4=56 rows”EMP (14 rows) DEPT (4 rows) EMPNOENAME ... DEPTNO------ ----- ... ------7839KING ... 107698BLAKE ... 30 ... 7934MILLER ... 10DEPTNO DNAME LOC ------ ---------- --------10ACCOUNTINGNEW YORK20RESEARCHDALLAS30SALESCHICAGO40OPERATIONSBOSTONENAME DNAME------ ----------KINGACCOUNTINGBLAKEACCOUNTING ...KINGRESEARCHBLAKERESEARCH...56 rows selected.
  • 7.
  • 8.
    What Is anEquijoin?Foreign keyPrimary keyEMP DEPT EMPNO ENAME DEPTNO------ ------- -------7839 KING 107698 BLAKE 307782 CLARK 107566 JONES 207654 MARTIN 307499 ALLEN 307844 TURNER 307900 JAMES 307521 WARD 307902 FORD 207369 SMITH 20...14 rows selected. DEPTNO DNAME LOC ------- ---------- --------10 ACCOUNTING NEW YORK30 SALESCHICAGO10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS30 SALESCHICAGO30 SALESCHICAGO30 SALESCHICAGO30 SALESCHICAGO30 SALESCHICAGO20 RESEARCHDALLAS20 RESEARCHDALLAS...14 rows selected.
  • 9.
    Retrieving Records withEquijoinsSQL> SELECT emp.empno, emp.ename, emp.deptno,2dept.deptno, dept.loc3 FROM emp, dept4 WHERE emp.deptno=dept.deptno;EMPNO ENAME DEPTNO DEPTNO LOC----- ------ ------ ------ ---------7839 KING1010 NEW YORK7698 BLAKE 3030 CHICAGO7782 CLARK1010 NEW YORK7566 JONES 2020 DALLAS...14 rows selected.
  • 10.
    Qualifying Ambiguous ColumnNamesUse 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.
  • 11.
    Additional Search ConditionsUsingthe AND Operator EMP DEPT EMPNO ENAME DEPTNO------ ------- -------7839 KING 107698 BLAKE 307782 CLARK 107566 JONES 207654 MARTIN 307499 ALLEN 307844 TURNER 307900 JAMES 307521 WARD 307902 FORD 207369 SMITH 20...14 rows selected.DEPTNO DNAME LOC ------ --------- --------10 ACCOUNTINGNEW YORK30SALES CHICAGO10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS30 SALES CHICAGO30 SALES CHICAGO30 SALES CHICAGO30 SALES CHICAGO30 SALES CHICAGO20 RESEARCHDALLAS20 RESEARCHDALLAS...14 rows selected.
  • 12.
    Using Table AliasesSimplifyqueries by using table aliases.SQL> SELECT emp.empno, emp.ename, emp.deptno, 2 dept.deptno, dept.loc3 FROM emp, dept4 WHERE emp.deptno=dept.deptno;SQL> SELECT e.empno, e.ename, e.deptno, 2 d.deptno, d.loc3 FROM emp e, dept d4 WHERE e.deptno=d.deptno;
  • 13.
    Joining More ThanTwo TablesORD CUSTID ORDID------- -------101610102611104612106601102602106604106605... 21 rows selected.ITEM ORDID ITEMID------ -------61036111612160116021...64 rows selected. CUSTOMER NAMECUSTID----------- ------JOCKSPORTS100TKB SPORT SHOP101VOLLYRITE102JUST TENNIS103K+T SPORTS105SHAPE UP106WOMENS SPORTS 107... ...9 rows selected.
  • 14.
    Non-Equijoins“salary in theEMP table is between low salary and high salary in the SALGRADEtable”EMPSALGRADE EMPNO ENAME SAL------ ------- ------7839 KING 50007698 BLAKE 28507782 CLARK 24507566 JONES 29757654 MARTIN 12507499 ALLEN 16007844 TURNER 15007900 JAMES 950...14 rows selected.GRADE LOSAL HISAL----- ----- ------17001200212011400314012000420013000530019999
  • 15.
    Retrieving Records withNon-EquijoinsSQL> SELECT e.ename, e.sal, s.grade2FROMemp e, salgrade s3WHERE e.sal4BETWEEN s.losal AND s.hisal;ENAME SAL GRADE---------- --------- ---------JAMES 9501SMITH 8001ADAMS 11001...14 rows selected.
  • 16.
    Outer JoinsNo employeein theOPERATIONS departmentEMP DEPT ENAMEDEPTNO----- ------KING10BLAKE30CLARK10JONES20... DEPTNO DNAME------ ----------10ACCOUNTING30SALES10ACCOUNTING20RESEARCH... 40OPERATIONS
  • 17.
    Outer JoinsYou usean outer join to also see rows that do not usually meet the join condition.Outer join operator is the plus sign (+).SELECTtable1.column, table2.columnFROMtable1, table2WHEREtable1.column(+)= table2.column;SELECTtable1.column, table2.columnFROMtable1, table2WHEREtable1.column = table2.column(+);
  • 18.
    Using Outer JoinsSQL>SELECTe.ename, d.deptno, d.dname2 FROMemp e, dept d3 WHEREe.deptno(+) = d.deptno4 ORDER BYe.deptno;ENAME DEPTNO DNAME---------- --------- -------------KING 10 ACCOUNTINGCLARK 10 ACCOUNTING...40 OPERATIONS15 rows selected.
  • 19.
    Self Joins“MGR inthe WORKER table is equal to EMPNO in the MANAGER table”EMP (WORKER)EMP (MANAGER)EMPNOENAME MGR----- ------ ----7839KING7698BLAKE78397782CLARK78397566JONES78397654MARTIN76987499ALLEN7698EMPNOENAME----- --------7839KING7839KING7839KING7698BLAKE7698BLAKE
  • 20.
    Joining a Tableto ItselfSQL> SELECT worker.ename||' works for '||manager.ename2 FROM emp worker, emp manager3 WHERE worker.mgr = manager.empno;WORKER.ENAME||'WORKSFOR'||MANAG-------------------------------BLAKE works for KINGCLARK works for KINGJONES works for KINGMARTIN works for BLAKE...13 rows selected.
  • 21.
    SummarySELECTtable1.column, table2.columnFROMtable1, table2WHEREtable1.column1=table2.column2;EquijoinNon-equijoinOuter joinSelf join
  • 22.
    Practice OverviewJoining tablesusing an equijoinPerforming outer and self joinsAdding conditions