Sunteți pe pagina 1din 22

Displaying Data

from Multiple Tables


Objectives
• After completing this lesson, you
should be able to do the following:
–– Write
Write SELECT
SELECT statements
statements toto 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 not
not meet
meet aa
join
join condition
condition by
by using
using outer
outer joins
joins
–– Join
Join aa table
table to
to itself
itself
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.
What Is a Join?
• Use a join to query data from more
than one 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.
Cartesian Product
–– A
A Cartesian
Cartesian product
product is
is 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 inin the
the first
first table
table are
are joined
joined to
to all
all
rows
rows inin the
the second
second tabletable
–– 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.
Generating
EMP (14 rows)
a Cartesian
DEPT (4 rows)
EMPNO
EMPNO ENAME
ENAME ... DEPTNOProduct
... 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
------
------ ----------
----------
KING
KING ACCOUNTING
ACCOUNTING
“Cartesian BLAKE
BLAKE ACCOUNTING
ACCOUNTING
product: ...
...
KING
KING RESEARCH
RESEARCH
14*4=56 rows” BLAKE RESEARCH
BLAKE RESEARCH
...
...
56
56 rows
rows selected.
selected.
Types of Joins
Equijoin
Equijoin Non-equijoin
Non-equijoin Outer
Outer join
join Self
Self join
join
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 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.

Foreign key Primary key


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;

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.
Qualifying Ambiguous
Column Names
– Use table prefixes to qualify column
– Use table prefixes to qualify 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.
Additional Search Conditions
EMP
Using the AND
DEPT
Operator
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.
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;
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 101
101 102
102 611
611
VOLLYRITE
VOLLYRITE 102
102 104
104 612
612
JUST TENNIS
JUST TENNIS 103
103 106
106 601
601
K+T SPORTS
K+T SPORTS 105
105 102
102 602
602 ITEM
SHAPE UP
SHAPE UP 106
106 106
106 ORDID604
604 ITEMID
WOMENS ORDID ITEMID
WOMENS SPORTS
SPORTS 107
107 106 605
106 ------605 -------
... ... ... ------ -------
... ... ... 610 33
99 rows 610
rows selected.
selected. 21 rows selected.
21 rows selected.
611 11
611
612
612 11
601
601 11
602
602 11
...
...
64
64 rows
rows selected.
selected.
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”
Retrieving Records
with Non-Equijoins
SQL> SELECT e.ename, e.sal, s.grade
2 FROM emp e, salgrade s
3 WHERE e.sal
4 BETWEEN s.losal AND s.hisal;

ENAME SAL GRADE


---------- --------- ---------
JAMES 950 1
SMITH 800 1
ADAMS 1100 1
...
14 rows selected.
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
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;

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(+);
Using Outer Joins
SQL> SELECT e.ename, d.deptno, d.dname
2 FROM emp e, dept d
3 WHERE e.deptno(+) = d.deptno
4 ORDER BY e.deptno;

ENAME DEPTNO DNAME


---------- --------- -------------
KING 10 ACCOUNTING
CLARK 10 ACCOUNTING
...
40 OPERATIONS
15 rows selected.
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

“MGR in the WORKER table is equal to EMPNO in the


MANAGER table”
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.
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
Practice Overview
–– Joining
Joining tables
tables using
using an
an equijoin
equijoin
–– Performing
Performing outer
outer and
and self
self joins
joins
–– Adding
Adding conditions
conditions

S-ar putea să vă placă și