
- •Objectivesj i
- •Obtainingi i Data from Multiplel i le Tablesl
- •What IsIs a Join?i
- •Cartesiani Product
- •Generatingi a Cartesiani Product
- •Types of Joinsi
- •What IsIs an Equijoin?ij i
- •Retrievingi i Records
- •Qualifyingli i Ambiguousi Columnl Names
- •Additionali i l Search Conditionsi i
- •Usingi Tablele Aliasesli
- •Joiningi i More Than Two Tablesl
- •Non-Equijoinsij i
- •Retrievingi i Records
- •Outer Joinsi
- •Outer Joinsi
- •Selflf Joinsi
- •Joiningi i a Tablele to ItselfI lf
- •Summary
- •Practicei Overviewi

4
Displaying Data from Multiple Tables

Objectivesj i
After completing this lesson, you should be able to do the following:
•• Writerite SELECT statementst t ts toto access datata fromfr morere thant one tablet le usingi equalitylity and nonequalitylity joinsj i
•• Viewi datata thatt t generallyr lly does nott meett a joinj in conditioniti by usingi outert r joinsj i
•• Joinin a tablet le toto itselfit lf
4-2

Obtainingi i Data from Multiplel i le Tablesl
EMP
|
|
|
|
|
|
|
EMPNO ENAME |
... DEPTNO |
|
||
|
------ |
----- ... |
------ |
|
|
|
7839 |
KING |
... |
10 |
|
|
7698 |
BLAKE ... |
30 |
|
|
|
... |
|
|
|
|
|
7934 |
MILLER ... |
10 |
|
|
|
|
|
|
|
|
DEPT
|
|
|
|
|
|
DEPTNO |
DNAME |
LOC |
|
|
------ |
---------- |
-------- |
|
|
10 |
ACCOUNTING |
NEW YORK |
|
|
20 |
RESEARCH |
DALLAS |
|
|
30 |
SALES |
CHICAGO |
|
|
40 |
OPERATIONS |
BOSTON |
|
|
|
|
|
|
EMPNO DEPTNO LOC
----- ------- --------
7839 |
10 |
NEW YORK |
7698 |
30 |
CHICAGO |
7782 |
10 |
NEW YORK |
7566 |
20 |
DALLAS |
7654 |
30 |
CHICAGO |
7499 |
30 |
CHICAGO |
... |
|
|
14 rows selected.
4-3

What IsIs a Join?i
Use a join to query data from more than one table.
SELECT table1.column, table2.column FROM table1, table2
WHERE table1.column1 = table2.column2;
•• Writerite thet joinj in conditioniti inin thet WHERE clausel ..
•• Prefixr fix thet columnl name withith thet tablet le name when thet same columnl name appearsrs inin morere thant one tablet le..
4-4

Cartesiani Product
•• A Cartesianrt i productr t isis formedf r when::
–A joinj in conditioniti isis omitteditt
–A joinj in conditioniti isis invalidi lid
–Allll rowsr inin thet firstfir t tablet le arere joinedj i toto allll rowsr inin thet second tablet le
•• To avoidid a Cartesianrt i product,r t, alwaysl includei l a validlid joinj in conditioniti inin a WHERE clausel ..
4-5

Generatingi a Cartesiani Product
EMP (14 rows)
|
|
|
|
|
|
|
|
EMPNO |
ENAME ... |
DEPTNO |
|
||
|
------ |
----- ... |
------ |
|
||
|
7839 |
KING |
... |
10 |
|
|
|
7698 |
BLAKE ... |
30 |
|
||
|
... |
|
|
|
|
|
|
7934 |
MILLER ... |
10 |
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DEPT (4 rows)
|
|
|
|
|
|
|
DEPTNO |
DNAME |
LOC |
|
|
|
------ |
---------- |
-------- |
|
|
|
10 |
ACCOUNTING |
NEW YORK |
|
|
|
20 |
RESEARCH |
DALLAS |
|
|
|
30 |
SALES |
CHICAGO |
|
|
|
40 |
OPERATIONS |
BOSTON |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ENAME |
DNAME |
|
|
|
|
|
|
|
------ |
---------- |
|
“Cartesian |
|
KING |
ACCOUNTING |
|
||||
|
BLAKE |
ACCOUNTING |
|
|||||
product: |
|
|
|
|
|
... |
|
|
|
|
|
|
|
|
|
||
14*4=56 rows” |
|
KING |
RESEARCH |
|
||||
|
|
|
|
|
|
BLAKE |
RESEARCH |
|
|
|
|
|
|
|
... |
|
|
|
|
|
|
|
|
56 rows selected. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4-6

Types of Joinsi
Equijoinij in Non--equijoinij in Outert r joinj in Selflf joinj in
4-7

What IsIs an Equijoin?ij i
EMP |
|
|
|
|
DEPT |
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
EMPNO |
ENAME |
DEPTNO |
|
|
|
DEPTNO DNAME |
LOC |
|
|
|
------ |
------- ------- |
|
|
|
------- ---------- -------- |
|
|||
|
7839 |
KING |
10 |
|
|
|
10 |
ACCOUNTING NEW YORK |
|
|
|
7698 |
BLAKE |
30 |
|
|
|
30 |
SALES |
CHICAGO |
|
|
7782 |
CLARK |
10 |
|
|
|
10 |
ACCOUNTING |
NEW YORK |
|
|
7566 |
JONES |
20 |
|
|
|
20 |
RESEARCH |
DALLAS |
|
|
7654 |
MARTIN |
30 |
|
|
|
30 |
SALES |
CHICAGO |
|
|
7499 |
ALLEN |
30 |
|
|
|
30 |
SALES |
CHICAGO |
|
|
7844 |
TURNER |
30 |
|
|
|
30 |
SALES |
CHICAGO |
|
|
7900 |
JAMES |
30 |
|
|
|
30 |
SALES |
CHICAGO |
|
|
7521 |
WARD |
30 |
|
|
|
30 |
SALES |
CHICAGO |
|
|
7902 |
FORD |
20 |
|
|
|
20 |
RESEARCH |
DALLAS |
|
|
7369 |
SMITH |
20 |
|
|
|
20 |
RESEARCH |
DALLAS |
|
|
... |
|
|
|
|
|
... |
|
|
|
|
14 rows selected. |
|
|
|
14 rows selected. |
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Primary key |
|
|
Foreign key |
|
|
4-8

Retrievingi i Records
withi Equijoinsij i
|
|
|
|
|
|
SQL> |
SELECT |
emp.empno, emp.ename, emp.deptno, |
|
|
2 |
|
dept.deptno, dept.loc |
|
|
3 |
FROM |
emp, dept |
|
|
4 |
WHERE |
emp.deptno=dept.deptno; |
|
|
|
|
|
|
|
|
|
|
|
EMPNO |
ENAME |
DEPTNO DEPTNO LOC |
||
----- |
------ ------ ------ --------- |
|||
7839 |
KING |
10 |
10 |
NEW YORK |
7698 |
BLAKE |
30 |
30 |
CHICAGO |
7782 |
CLARK |
10 |
10 |
NEW YORK |
7566 |
JONES |
20 |
20 |
DALLAS |
... |
|
|
|
|
14 rows selected.
4-9

Qualifyingli i Ambiguousi Columnl Names
•• Use tablet le prefixesr fi toto qualifylify columnl names thatt t arere inin multiplelti le tablest l ..
•• ImproveI r performancerf r by usingi tablet le prefixesr fi ..
•• Distinguishi ti i columnsl thatt t have identicali ti l names butt resider i inin differentiff r t tablest l by usingi columnl aliasesli ..
4-10

Additionali i l Search Conditionsi i
Usingi the AND Operator
EMP
EMPNO |
ENAME |
DEPTNO |
------ |
------- ------- |
|
7839 |
KING |
10 |
7698 |
BLAKE |
30 |
7782 |
CLARK |
10 |
7566 |
JONES |
20 |
7654 |
MARTIN |
30 |
7499 |
ALLEN |
30 |
7844 |
TURNER |
30 |
7900 |
JAMES |
30 |
7521 |
WARD |
30 |
7902 |
FORD |
20 |
7369 |
SMITH |
20 |
... |
|
|
14 rows selected.
DEPT |
|
|
DEPTNO |
DNAME |
LOC |
------ |
--------- |
-------- |
10 |
ACCOUNTING |
NEW YORK |
30 |
SALES |
CHICAGO |
10 |
ACCOUNTING |
NEW YORK |
20 |
RESEARCH |
DALLAS |
30 |
SALES |
CHICAGO |
30 |
SALES |
CHICAGO |
30 |
SALES |
CHICAGO |
30 |
SALES |
CHICAGO |
30 |
SALES |
CHICAGO |
20 |
RESEARCH |
DALLAS |
20 |
RESEARCH |
DALLAS |
... |
|
|
14 rows selected. |
|
4-11