Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright  Oracle Corporation, 1998. All rights reserved. 3 Displaying Data from Multiple Tables.

Similar presentations


Presentation on theme: "Copyright  Oracle Corporation, 1998. All rights reserved. 3 Displaying Data from Multiple Tables."— Presentation transcript:

1 Copyright  Oracle Corporation, All rights reserved. 3 Displaying Data from Multiple Tables

2 3-2 Copyright  Oracle Corporation, All rights reserved. Objectives At the end of this lesson, you should be able to: 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 At the end of this lesson, you should be able to: 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

3 3-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 3-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. SELECTtable.column, table.column FROMtable1, table2 WHEREtable1.column1 = table2.column2; SELECTtable.column, table.column FROMtable1, table2 WHEREtable1.column1 = table2.column2;

5 3-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.

6 3-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”

7 3-7 Copyright  Oracle Corporation, All rights reserved. Types of Joins Equijoin Non-equijoin Outer join Self join

8 3-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 Primarykey Primary key

9 3-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.

10 3-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.

11 3-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.

12 3-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;

13 3-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

14 3-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

15 3-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;

16 3-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

17 3-17 Copyright  Oracle Corporation, All rights reserved. Outer Joins You use an outer join to see rows that do not usually meet the join condition. Outer join operator is the plus sign (+). You use an outer join to see rows that do not usually meet the join condition. Outer join operator is the plus sign (+). SELECTtable.column, table.column FROMtable1, table2 WHEREtable1.column(+) = table2.column; SELECTtable.column, table.column FROMtable1, table2 WHEREtable1.column(+) = table2.column; SELECTtable.column, table.column FROMtable1, table2 WHEREtable1.column = table2.column(+); SELECTtable.column, table.column FROMtable1, table2 WHEREtable1.column = table2.column(+);

18 3-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.

19 3-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

20 3-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;

21 3-21 Copyright  Oracle Corporation, All rights reserved. Summary Equijoin Non-equijoin Outer join Self join SELECTtable.column, table.column FROMtable1, table2 WHEREtable1.column1 = table2.column2; SELECTtable.column, table.column FROMtable1, table2 WHEREtable1.column1 = table2.column2;

22 3-22 Copyright  Oracle Corporation, All rights reserved. Practice Overview Joining tables using an equijoin Performing outer and self joins Adding additional conditions Joining tables using an equijoin Performing outer and self joins Adding additional conditions

23 3-23 Copyright  Oracle Corporation, All rights reserved.

24 3-24 Copyright  Oracle Corporation, All rights reserved.

25 3-25 Copyright  Oracle Corporation, All rights reserved.

26 3-26 Copyright  Oracle Corporation, All rights reserved.

27 3-27 Copyright  Oracle Corporation, All rights reserved.

28 3-28 Copyright  Oracle Corporation, All rights reserved.


Download ppt "Copyright  Oracle Corporation, 1998. All rights reserved. 3 Displaying Data from Multiple Tables."

Similar presentations


Ads by Google