SQL 7 (Joins)
SQL 7 (Joins)
Displaying Data
from Multiple Tables
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 1
Objectives
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 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 YORK
NEW 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.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 3
What Is a Join?
–When data from more than one table in the database is required,
a join condition is used. Rows in one table can be joined to rows
in another table according to common values existing in
corresponding columns, that is, usually primary and foreign key
columns.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 4
Guidelines:
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;
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 6
Generating a Cartesian Product
EMP (14 rows) DEPT (4 rows)
EMPNO
EMPNO ENAME
ENAME ...
... DEPTNO
DEPTNO DEPTNO
DEPTNO DNAME
DNAME LOC
LOC
------
------ -----
----- ...
... ------
------ ------ ---------- --------
------ ---------- --------
7839 KING
7839 KING ...
... 10
10 10
10 ACCOUNTING
ACCOUNTING NEW
NEW YORK
YORK
7698
7698 BLAKE
BLAKE ...
... 30
30 20 RESEARCH
20 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.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 7
Types of Joins
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 8
What Is an Equijoin?
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 9
Relation { EMP, DEPT }
• To determine the name of an employee’s department, you
compare the value in the DEPTNO column in the EMP table with
the DEPTNO values in the DEPT table.
• The relationship between the EMP and DEPT tables is an
equijoin—that is, values in the DEPTNO column on both tables
must be equal. Frequently, this type of join involves primary and
foreign key complements.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 10
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.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 11
Retrieving Records
with Equijoin
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 12
Retrieving Records
with Equijoin( using ON clause)
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 13
Qualifying Ambiguous
Column Names
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 14
Additional Search Conditions
Using the AND Operator
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 15
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;
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 16
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
• To join n tables together, you need a 601
601 11
602
602 11
minimum of n-1 join conditions. For ...
...
example, to join three tables, a 64
64 rows
rows selected.
selected.
minimum of two joins is required.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 17
Non-Equijoins
EMP SALGRADE
EMPNO ENAME SAL GRADE LOSAL HISAL
------ ------- ------ ----- ----- ------
7839 KING 5000 1 700 1200
7698 BLAKE 2850 2 1201 1400
When no column in 7782 CLARK
7566 JONES
2450
2975
3
4
1401
2001
2000
3000
the Table A 7654 MARTIN 1250 5 3001 9999
7499 ALLEN 1600
corresponds directly 7844 TURNER 1500
to a column in the 7900 JAMES
...
950
“salary in the EMP
Table B. 14 rows selected. table is between
low salary and high
salary in the SALGRADE
table”
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 18
Retrieving Records
with Non-Equijoins
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 19
Outer Joins
EMP DEPT
ENAME DEPTNO DEPTNO DNAME
----- ------ ------ ----------
KING 10 10 ACCOUNTING
BLAKE 30 30 SALES
CLARK 10 20 RESEARCH
JONES 20 ...
... 40 OPERATIONS
No employee in the
OPERATIONS department
If a row does not satisfy a join condition, the row will not appear in the query result. For
example, in the equijoin condition of EMP and DEPT tables, department OPERATIONS
does not appear because no one works in that department.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 20
– Left Outer Join. – Right Outer Join
Select all records from Table A, Select all records from Table B,
along with records from Table B for along with records from Table A for
which the join condition is met which the join condition is met
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 21
Outer Joins
– You use an outer join to also see rows that do not usually meet the
join condition.(missing rows)
– Outer join operator is the plus sign (+).
– Left Outer Join.
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;
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 22
– Left Outer Join. – Right Outer Join
SELECT
SELECT table1.column,
table1.column, table2.column
table2.column SELECT
SELECT table1.column,
table1.column, table2.column
table2.column
FROM
FROM table1,
table1, table2
table2 FROM
FROM table1,
table1, table2
table2
WHERE
WHERE table1.column
table1.column == table2.column
table2.column (+);
(+); WHERE
WHERE table1.column(+)
table1.column(+) == table2.column;
table2.column;
SELECT
SELECT table1.column,
table1.column, table2.column
table2.column SELECT
SELECT table1.column,
table1.column, table2.column
table2.column
FROM
FROM table1
table1 LEFT
LEFT OUTER
OUTER JOIN
JOIN table2
table2 FROM
FROM table1
table1 RIGHT
RIGHT OUTER
OUTER JOIN
JOIN table2
table2
ON
ON table1.column
table1.column == table2.column;
table2.column; ON
ON table1.column
table1.column == table2.column;
table2.column;
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 23
Using Right 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;
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 26
Using Left and Right Outer Join
in One Query
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 27
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
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 28
Joining a Table to Itself
–Sometimes you need to join a table to itself. To find the name of each
employee’s manager, you need to join the EMP table to itself, or perform a
self join. For example, to find the name of Blake’s manager, you need to:
• Find Blake in the EMP table by looking at the ENAME column.
• Find the manager number for Blake by looking at the MGR column. Blake’s manager
number is 7839.
• Find the name of the manager with EMPNO 7839 by looking at the ENAME column. King’s
employee number is 7839, so King is Blake’s manager.
–In this process, you look in the table twice. The first time you look in the table
to find Blake in the ENAME column and MGR value of 7839. The second time
you look in the EMPNO column to find 7839 and the ENAME column to find
King.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 29
Joining a Table to Itself
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.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 30