Displaying Data From Multiple Tables
Displaying Data From Multiple Tables
Displaying Data
from Multiple Tables
Objectives
After
After completing
completing thisthis lesson,
lesson, you
you should
should
be
be able
able to
to do
do the
the following:
following:
•• Write
Write SELECT
SELECT statements
statements to to access
access
data
data from
from more
more than
than one
one table
table using
using
equality
equality and
and nonequality
nonequality joins
joins
•• View
View data
data that
that generally
generally does
does notnot meet
meet aa
join
join condition
condition by by using
using outer
outer joins
joins
•• Join
Join aa table
table to
to itself
itself
4-2
Obtaining Data from Multiple Tables
EMP DEPT
EMPNO ENAME ... DEPTNO DEPTNO DNAME LOC
------ ----- ... ------ ------ ---------- --------
7839 KING ... 10 10 ACCOUNTING NEW YORK
7698 BLAKE ... 30 20 RESEARCH DALLAS
... 30 SALES CHICAGO
7934 MILLER ... 10 40 OPERATIONS BOSTON
EMPNO
EMPNO DEPTNO
DEPTNO LOC
LOC
----- ------- --------
----- ------- --------
7839
7839 10
10 NEW
NEW YORK
YORK
7698
7698 30 CHICAGO
30 CHICAGO
7782
7782 10
10 NEW
NEW YORK
YORK
7566
7566 20 DALLAS
20 DALLAS
7654
7654 30
30 CHICAGO
CHICAGO
7499
7499 30 CHICAGO
30 CHICAGO
...
...
14
14 rows
rows selected.
selected.
4-3
What Is a Join?
Use
Use aa join
join to
to query
query data
data from
from more
more than
than
one
one table.
table.
SELECT
SELECT table1.column,
table1.column, table2.column
table2.column
FROM
FROM table1,
table1, table2
table2
WHERE
WHERE table1.column1
table1.column1 == table2.column2;
table2.column2;
•• Write
Write the
the join
join condition
condition in
in the
the WHERE
WHERE
clause.
clause.
•• Prefix
Prefix the
the column
column name
name with
with the
the table
table
name
name when
when the
the same
same column
column name
name
appears
appears inin more
more than
than one
one table.
table.
4-4
Cartesian Product
•• A
A Cartesian
Cartesian product
product isis formed
formed when:
when:
–– A
A join
join condition
condition is
is omitted
omitted
–– A
A join
join condition
condition is
is invalid
invalid
–– All
All rows
rows in
in the
the first
first table
table are
are joined
joined to
to
all
all rows
rows in
in the
the second
second table
table
•• To
To avoid
avoid aa Cartesian
Cartesian product,
product, always
always
include
include aa valid
valid join
join condition
condition in
in aa
WHERE
WHERE clause.
clause.
4-5
Generating a Cartesian Product
EMP (14 rows) DEPT (4 rows)
EMPNO
EMPNO ENAME
ENAME ...
... DEPTNO
DEPTNO DEPTNO
DEPTNO DNAME
DNAME LOC
LOC
------ -----
------ ----- ...
... ------
------ ------
------ ----------
---------- --------
--------
7839
7839 KING
KING ...
... 10
10 10
10 ACCOUNTING
ACCOUNTING NEW
NEW YORK
YORK
7698
7698 BLAKE
BLAKE ...
... 30
30 20
20 RESEARCH
RESEARCH DALLAS
DALLAS
...
... 30
30 SALES
SALES CHICAGO
CHICAGO
7934
7934 MILLER
MILLER ...
... 10
10 40
40 OPERATIONS
OPERATIONS BOSTON
BOSTON
ENAME
ENAME DNAME
DNAME
------
------
----------
----------
“Cartesian KING
KING
product: ACCOUNTING
ACCOUNTING
BLAKE
BLAKE
14*4=56 rows” ACCOUNTING
ACCOUNTING
...
...
KING
KING
RESEARCH
RESEARCH
4-6 BLAKE
BLAKE
RESEARCH
Types of Joins
Equijoin
Equijoin Non-equijoin
Non-equijoin Outer
Outer join
join Self
Self join
join
4-7
What Is an Equijoin?
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
7566 JONES 20 YORK
7654 MARTIN 30 20 RESEARCH DALLAS
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 30 SALES CHICAGO
7369 SMITH 20 20 RESEARCH DALLAS
... 20 RESEARCH DALLAS
14 rows selected. ...
14 rows selected.
Foreign key Primary key
4-8
Retrieving Records
with Equijoins
SQL> SELECT emp.empno, emp.ename, emp.deptno,
2 dept.deptno, dept.loc
3 FROM emp, dept
4 WHERE emp.deptno=dept.deptno;
4-9
Qualifying Ambiguous
Column Names
•• Use
Use table
table prefixes
prefixes to
to qualify
qualify column
column
names
names that
that are
are in
in multiple
multiple tables.
tables.
•• Improve
Improve performance
performance by by using
using table
table
prefixes.
prefixes.
•• Distinguish
Distinguish columns
columns that
that have
have identical
identical
names
names but
but reside
reside in
in different
different tables
tables by
by
using
using column
column aliases.
aliases.
4-10
Additional Search Conditions
Using the AND Operator
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.
4-11
Using Table Aliases
Simplify
Simplify queries
queries by
by using
using table
table aliases.
aliases.
SQL> SELECT emp.empno, emp.ename, emp.deptno,
2 dept.deptno, dept.loc
3 FROM emp, dept
4 WHERE emp.deptno=dept.deptno;
4-12
Joining More Than Two Tables
CUSTOMER ORD
NAME
NAME CUSTID
CUSTID CUSTID
CUSTID ORDID
ORDID
-----------
----------- ------
------ -------
------- -------
-------
JOCKSPORTS
JOCKSPORTS 100
100 101
101 610
610
TKB
TKB SPORT
SPORT SHOP
SHOP 102
102 611
611
101
101 104
104 612
612
VOLLYRITE
VOLLYRITE 102
102 106
106 601
601
JUST TENNIS
JUST TENNIS 103
103 102
102 602
602 ITEM
K+T SPORTS
K+T SPORTS 105
105 106
106 ORDID604
604 ITEMID
SHAPE ORDID ITEMID
SHAPE UP
UP 106
106 106 605
106 ------605 -------
WOMENS ------ -------
WOMENS SPORTS
SPORTS 107
107 ...
... 610 33
... ... 21 rows 610
selected.
... ... 21 rows selected.
611 11
99 rows 611
rows selected.
selected. 612 11
612
601
601 11
602
602 11
...
...
64
64 rows
rows selected.
selected.
4-13
Non-Equijoins
EMP SALGRADE
EMPNO ENAME SAL GRADE LOSAL HISAL
------ ------- ------ ----- ----- ------
7839 KING 5000 1 700 1200
7698 BLAKE 2850 2 1201 1400
7782 CLARK 2450 3 1401 2000
7566 JONES 2975 4 2001 3000
7654 MARTIN 1250 5 3001 9999
7499 ALLEN 1600
7844 TURNER 1500
7900 JAMES 950
... “salary in the EMP
14 rows selected. table is between
low salary and high
salary in the SALGRADE
table”
4-14
Retrieving Records
with Non-Equijoins
SQL> SELECT e.ename, e.sal, s.grade
2 FROM emp e, salgrade s
3 WHERE e.sal
4BETWEEN s.losal AND s.hisal;
4-15
Outer Joins
EMP DEPT
ENAME DEPTNO DEPTNO DNAME
----- ------ ------ ----------
KING 10 10 ACCOUNTING
BLAKE 30 30 SALES
CLARK 10 10 ACCOUNTING
JONES 20 20 RESEARCH
... ...
40 OPERATIONS
No employee in the
OPERATIONS department
4-16
Outer Joins
•• You
You use
use an
an outer
outer join
join to
to also
also see
see rows
rows
that
that do
do not
not usually
usually meet
meet the
the join
join
condition.
condition.
•• Outer
Outer join
join operator
operator is
is the
the plus
plus sign
sign (+).
(+).
SELECT
SELECT table1.column,
table1.column, table2.column
table2.column
FROM
FROM table1,
table1, table2
table2
WHERE
WHERE table1.column(+)
table1.column(+) == table2.column;
table2.column;
4-17
Using Outer Joins
SQL> SELECT e.ename, d.deptno, d.dname
2 FROMemp e, dept d
3 WHEREe.deptno(+) = d.deptno
4 ORDER BY e.deptno;
4-18
Self Joins
EMP (WORKER) EMP (MANAGER)
EMPNO ENAME MGR EMPNO ENAME
----- ------ ---- ----- --------
7839 KING
7698 BLAKE 7839 7839 KING
7782 CLARK 7839 7839 KING
7566 JONES 7839 7839 KING
7654 MARTIN 7698 7698 BLAKE
7499 ALLEN 7698 7698 BLAKE
4-19
Joining a Table to Itself
SQL> SELECT worker.ename||' works for '||manager.ename
2 FROM emp worker, emp manager
3 WHERE worker.mgr = manager.empno;
WORKER.ENAME||'WORKSFOR'||MANAG
WORKER.ENAME||'WORKSFOR'||MANAG
-------------------------------
-------------------------------
BLAKE
BLAKE works
works for
for KING
KING
CLARK
CLARK works
works for
for KING
KING
JONES
JONES works
works for
for KING
KING
MARTIN
MARTIN works
works for
for BLAKE
BLAKE
...
...
13
13 rows
rows selected.
selected.
4-20
Summary
SELECT
SELECT table1.column,
table1.column, table2.column
table2.column
FROM
FROM table1,
table1, table2
table2
WHERE
WHERE table1.column1
table1.column1 == table2.column2;
table2.column2;
Equijoin
Equijoin Non-equijoin
Non-equijoin Outer
Outer join
join Self
Self join
join
4-21
Practice Overview
• •Joining
Joiningtables
tablesusing
usingan
anequijoin
equijoin
• •Performing
Performingouter
outerand
andself
selfjoins
joins
• •Adding
Addingconditions
conditions
4-22