0% found this document useful (0 votes)
54 views30 pages

SQL 7 (Joins)

The document discusses joining data from multiple database tables. It describes using equality and nonequality joins to select data from two or more tables based on matching column values. Outer joins are discussed to also view data that does not meet the join condition. Self joins allow a table to be joined to itself. Guidelines are provided for writing basic SELECT statements to perform joins, including using table aliases to qualify column names and including the proper number of join conditions. Cartesian products from omitting or using invalid join conditions are also covered.

Uploaded by

misterfarhan0307
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views30 pages

SQL 7 (Joins)

The document discusses joining data from multiple database tables. It describes using equality and nonequality joins to select data from two or more tables based on matching column values. Outer joins are discussed to also view data that does not meet the join condition. Self joins allow a table to be joined to itself. Guidelines are provided for writing basic SELECT statements to perform joins, including using table aliases to qualify column names and including the proper number of join conditions. Cartesian products from omitting or using invalid join conditions are also covered.

Uploaded by

misterfarhan0307
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

(SQL)

Displaying Data
from Multiple Tables

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 1
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 by using a self join

© 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?

–Join is a combination of a Cartesian product followed by a


selection process. A Join operation pairs two tuples from
different relations, if and only if a given join condition is
satisfied

–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;

• Write the join condition in the WHERE clause


• When writing a SELECT statement that joins tables, precede the column
name with the table name for clarity and to enhance database access.
• If the same column name appears in more than one table, the column name
must be prefixed with the table name.
• To join n tables together, you need a minimum of (n-1) join conditions.
Therefore, to join four tables, a minimum of three joins are required. This rule
may not apply if your table has a concatenated primary key, in which case
more than one column is required to uniquely identify each row.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 5
Cartesian Product

– 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.

© 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

Equijoin Non-equijoin Outer join Self join

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 8
What Is an Equijoin?

The inner JOIN(equijoin) is used


to return rows from both tables
that satisfy the given condition.

© 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.

Foreign key Primary key

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 11
Retrieving Records
with Equijoin

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.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 12
Retrieving Records
with Equijoin( using ON clause)

SQL> SELECT emp.empno, emp.ename, emp.deptno,


2 dept.deptno, dept.loc
3 FROM emp INNER JOIN dept
4 ON 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.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 13
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 by using column
aliases.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 14
Additional Search Conditions
Using the AND Operator

SQL> SELECT emp.empno, emp.ename, emp.deptno,


2 dept.deptno, dept.loc
3 FROM emp, dept
4 WHERE emp.deptno=dept.deptno
5 AND emp.ename like ‘KING’;

To display employee King’s employee number, name, department number, and


department location
SQL> SELECT emp.empno, emp.ename, emp.deptno,
2 dept.deptno, dept.loc
3 FROM emp INNER JOIN dept
4 ON emp.deptno = dept.deptno
5 AND emp.ename LIKE UPPER(‘king’);

© 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;

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;

© 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

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.

© 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(+);

– Right 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;
© 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;

ENAME DEPTNO DNAME


---------- --------- -------------
KING 10 ACCOUNTING
CLARK 10 ACCOUNTING
...
40 OPERATIONS
15 rows selected.

The query will also retrieve those departments against


which there is no employee.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 24
Using Left Outer Joins
To retrieve the employees having not assigned to a
Particular department.

A condition involving an outer join


SQL>
2
SELECT
FROM
e.ename, d.deptno, d.dname
emp e, dept d
3 cannot use the IN operator or be
WHERE e.deptno = d.deptno(+)
4 ORDER BY e.deptno;
linked to another condition by the
OR operator.
ENAME DEPTNO DNAME
---------- --------- -------------
KING 10 ACCOUNTING
CLARK 10 ACCOUNTING
...
BABU KHAN
15 rows selected.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 25
Illegal Queries
Using Outer Joins

SQL> SELECT e.ename, d.deptno, d.dname


er
2 FROM emp e, dept d
o ut
3 WHERE e.deptno(+) = d.deptno(+) h t
i g
r ly.
4 ORDER BY e.deptno;
d
n us
f t a o
e
l tan e
se u l
u
t sim
o
n in
n
Ca jo

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 26
Using Left and Right Outer Join
in One Query

SQL> SELECT e.ename, d.deptno, d.dname


2 FROM emp e, dept d
3 WHERE e.deptno(+) = d.deptno
4 UNION
5 SELECT e.ename, d.deptno, d.dname
6 FROM emp e, dept d
3 WHERE e.deptno = d.deptno(+)

• Similarly, We can use INTERSECT and MINUS to combine


two or more select statements into a single query, provided that
the select-list of all the queries remain the same.

© 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

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


MANAGER table”

© 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

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.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 30

You might also like