Download presentation
Presentation is loading. Please wait.
Published byPosy Ellis Modified over 9 years ago
1
4 Displaying Data from Multiple Tables Important Legal Notice: Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published by Oracle Corp. For further information, visit www.oracle.comwww.oracle.com This presentation must be used for only education purpose for students at Central Washington University which is a member of Oracle Academic Initiatives (OAI) and has used Oracle systems for HRIS & Accounting Systems as a database platform embedded on its PeopleSoft ERP system, since 1999.
2
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
3
Obtaining Data from Multiple Tables EMPNOENAME... DEPTNO --------------------... ------------ 7839KING...10 7698BLAKE...30... 7934MILLER...10 EMP DEPTNODNAME LOC ------------------------------- ------------ 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON DEPT EMPNODEPTNOLOC -------------------------------------- 783910NEW YORK 769830CHICAGO 778210NEW YORK 756620DALLAS 765430CHICAGO 749930CHICAGO... 14 rows selected.
4
What is a Join? SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2; 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.
5
Cartesian Product (bad guy) 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
Generating a Cartesian Product EMPNOENAME... DEPTNO --------------------... ------------ 7839KING...10 7698BLAKE...30... 7934MILLER...10 EMP (14 rows) DEPTNODNAME LOC ------------------------------- ------------ 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON DEPT (4 rows) ENAMEDNAME ----------------------- KINGACCOUNTING BLAKEACCOUNTING... KINGRESEARCH BLAKERESEARCH... 56 rows selected. “Cartesian product: 14*4=56 rows”
7
Types of Joins EquijoinNon-equijoinOuter joinSelf join
8
What is an Equijoin? EMPNOENAMEDEPTNO -------------------- ----------- 7839KING10 7698BLAKE30 7782CLARK10 7566JONES20 7654MARTIN30 7499ALLEN30 7844TURNER30 7900JAMES30 7521WARD30 7902FORD20 7369SMITH20... 14 rows selected. EMP DEPTNODNAME LOC ------------------------------- ------------ 10ACCOUNTINGNEW YORK 30SALESCHICAGO 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 30SALESCHICAGO 30SALESCHICAGO 30SALESCHICAGO 30SALESCHICAGO 20RESEARCHDALLAS 20RESEARCHDALLAS... 14 rows selected. DEPT Foreign key Primary key
9
Retrieving Records with Equijoins SQL>SELECT emp.empno,emp.ename,emp.deptno, 2 dept.deptno, dept.loc 3FROM emp, dept 4WHERE emp.deptno = dept.deptno; EMPNOENAME DEPTNO DEPTNOLOC --------------------- ------------ ----------------------------- 7839KING1010NEW YORK 7698BLAKE3030CHICAGO 7782CLARK1010NEW YORK 7566JONES2020DALLAS... 14 rows selected.
10
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 using column aliases.
11
Additional Search Conditions Using the AND Operator EMPNOENAMEDEPTNO -------------------- ----------- 7839KING10 7698BLAKE30 7782CLARK10 7566JONES20 7654MARTIN30 7499ALLEN30 7844TURNER30 7900JAMES30 7521WARD30 7902FORD20 7369SMITH20... 14 rows selected. EMP DEPTNODNAME LOC ------------------------------- ------------ 10ACCOUNTINGNEW YORK 30SALESCHICAGO 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 30SALESCHICAGO 30SALESCHICAGO 30SALESCHICAGO 30SALESCHICAGO 20RESEARCHDALLAS 20RESEARCHDALLAS... 14 rows selected. DEPT
12
Using Table Aliases SQL>SELECT emp.empno,emp.ename,emp.deptno, 2 dept.deptno, dept.loc 3FROM emp, dept 4WHERE emp.deptno=dept.deptno; SQL>SELECT e.empno,e.ename, e.deptno, 2 d.deptno, d.loc 3FROM emp e, dept d 4WHERE e.deptno = d.deptno; Simplify queries by using table aliases.
13
Joining More Than Two Tables CUSTOMER NAMECUSTID --------------- ----------- -JOCKSPROTS100 TKB SPORT SHOP101 VOLLYRITE102 JUST TENNIS103 K+T SPORTS105 SHAPE UP106 WOMENS SPORTS107...... 9 rows selected. ORD CUSTIDORDID ----------- --------- 101610 102611 104612 106601 102602 106 106... 21 rows ITEM ORDIDITEMID --------------------- 6103 6111 6121 6011 6021... 64 rows selected.
14
Non-Equijoins EMPNOENAMESAL -------------------- ----------- 7839KING5000 7698BLAKE2850 7782CLARK2450 7566JONES2975 7654MARTIN1250 7499ALLEN1600 7844TURNER1500 7900JAMES950... 14 rows selected. EMP GRADELOSALHISAL -------------------- ----------- 17001200 212011400 314012000 420013000 530019999 SALGRADE “Salary in the EMP table is between low salary and high salary in the SALGRADE table”
15
Retrieving Records with Non-Equijoins SQL> SELECT e.ename, e.sal, s.grade 2 FROM emp e, salgrade s 3 WHEREe.sal 4 BETWEEN s.losal AND s.hisal; ENAME SAL GRADE ------------------------------ JAMES9501 SMITH8001... 14 rows selected.
16
Outer Joins ENAME DEPTNO ----------- ------------ KING 10 BLAKE 30 CLARK 10 JONES 20... DEPTNO DNAME ------------ ------------------------- 10 ACCOUNTING 30 SALES 10 ACCOUNTING 20 RESEARCH... 40 OPERATIONS EMPDEPT No employee in the OPERATIONS department
17
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 (+) SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column(+) = table2.column; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column = table2.column(+);
18
Using Outer Joins SQL>SELECT e.ename, e.deptno,d.dname 2FROMemp e, dept d 3WHEREe.deptno(+) = d.deptno 4ORDER BYe.deptno; ENAMEDEPTNODNAME ------------------------------- KING10ACOUNTING CLARK10ACCOUNTING... 40OPERATIONS 15 rows selected.
19
Self Joins EMPNOENAMEMGR -------------------- ----------- 7839KING 7698BLAKE7839 7782CLARK7839 7566JONES7839 7654MARTIN7698 7499ALLEN7698 EMP(“WORKER” Table) EMPNOENAME ----------- ------------ 7839KING 7839KING 7839KING 7698BLAKE 7698BLAKE EMP(“MANAGER” Table) “MGR in the WORKER table is equal to EMPNO in the MANAGER table”
20
Joining a Table to Itself SQLSELECT worker.ename | | ‘ works for ‘| | manager.ename 2FROM emp worker, emp manager 3WHERE worker.mgr = manager.empno; WORKER.ENAME | | ‘WORKSFOR’ | | MANAG ------------------------------------------------------------- BLAKE works for KING CLARK works for KING JONES works for KING MARTIN works for BLAKE... 13 rows selected
21
Summary SELECTtable1.column, table2.column FROMtable1, table 2 WHEREtable1.column1 = table2.column2 EquijoinNon-equijoinOuter joinSelf join
22
Practice Overview Joining tables using an equijoin Performing outer and self joins Adding conditions
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.