DBMSPractical - Join-Updated
DBMSPractical - Join-Updated
JOIN
Dr. Rabindra Kumar Barik
Associate Professor,
School of Computer Application,
KIIT University, Odisha - India
2
Displaying Data
from Multiple Tables
Obtaining Data from Multiple Tables
EMPLOYEES DEPARTMENTS
…
What is a Join ?
5
Cartesian Product
6
Generating a Cartesian Product
EMPLOYEES DEPARTMENTS
ENAME DNAME
----------- --------------
KING ACCOUNTING
BLAKE ACCOUNTING
…….
KING RESEARCH
BLAKE RESEARCH
....
56 rows selected “ Cartesian Product :
14 * 4 = 56 rows ”
8
Types of Joins
◦ Cross Joins
◦ Equi joins
◦ Non Equi Joins
◦ Semi Join
◦ Outer joins
◦ Anti Join
◦ Self Joins
What is an Equijoin ?
DEPARTMENTS
EMPLOYEES
Primary Key
Foreign Key
10
Retrieving Records with Equijoin
Select
employees.employee_id,employees.last_name,employees.departme
nt_id,departments.department_name from employees ,departments
where employees.department_id=departments.department_id
Sample Output
……
11
Additional Search Conditions Using And
Operator
Select
employees.employee_id,employees.last_name,employees.department_id,depa
rtments.department_name ,salary from employees ,departments where
employees.department_id=departments.department_id and
department_name='Executive'or department_name='IT' and salary>12000
Sample Output
12
Using Table Alias
Select
employees.employee_id,employees.last_name,employ
ees.department_id,departments.department_name,sal
ary from employees ,departments where
employees.department_id=departments.department_id
select
e.employee_id,e.last_name,e.department_id,d.department_nam
e ,e.salary from employees e ,departments d where
e.department_id=d.department_id
13
Joining More Than Two Tables
EMPLOYEES DEPARTMENTS
LOCATIONS
select
e.employee_id,e.last_name,e.department_id,d.department_name
, l.location_id,l.city from employees e ,departments d ,locations l
where e.department_id=d.department_id and
d.location_id=l.location_id
Sample Output
……….
15
What is a Non- Equijoin ?
EMPLOYEES JOBS
…………..
17
Semi Joins
• Semijoin is used infrequently for
join operations
• Semijoin is alike equi join with the
only exception that attributes in
the first relation are returned in
the result
18
Example of Semi Joins
select
e.employee_id,e.last_name,e.department_id,e.salar
y from employees e ,departments d where
e.department_id=d.department_id and
department_name = 'IT'
Sample Output
19
Outer Joins
1. To see rows that do not usually meet the join
condition
2. Outer join operator is (+) sign
3. Three types of Outer Joins:
•Left Outer Joins
•Right Outer Joins
•Full Outer Joins
20
What is Outer Join ?
EMPLOYEES DEPARTMENTS
EMPNO ENAME . . . DEPTNO DEPTNO DNAME LOC
----------- ----------- ------------- ------------ --------------------- -----------
7839 KING 10 10 ACCOUNTING NEW YORK
7698 BLAKE 30 20 RESEARCH DALLAS
7782 CLARK 10 30 SALES CHICAGO
7566 JONES 20 40 OPERATIONS BOSTON
7654 MARTIN 30
7499 ALLEN 30
7844 TURNER 30
7900 JAMES 30
7521 WARD 30
7902 FORD 20
7369 SMITH 20 No employee in the
7001 RAVI ---- Operation Department
...
14 rows selected.
No department assign
to the Employee
21
Left Outer Joins
Syntax
22
table1.column = Is the condition that joins (or
relates) the tables together.
23
Using Left Outer Joins
CREATE TABLE DEPT11 (DEPTNO NUMBER(4) PRIMARY KEY, DEPTNAME VARCHAR2(20));
24
Using Left Outer Joins
SELECT * FROM EMP11; SELECT * FROM DEPT11;
25
Right Outer Joins
Syntax
26
table1.column (+) is the outer join symbol, which
can be placed either side of the
WHERE clause condition, but not
both sides. (Place the outer join
symbol following the name of the
column in the table without the
matching rows.)
27
Using Right Outer Joins
CREATE TABLE DEPT11 (DEPTNO NUMBER(4) PRIMARY KEY, DEPTNAME VARCHAR2(20));
28
Using Right Outer Joins
SELECT * FROM EMP11; SELECT * FROM DEPT11;
29
Full Outer Joins
Syntax
SELECT table1.column, table2.column
FROM table1,table2
WHERE table1.column(+) = table2.column;
union
SELECT table1.column, table2.column
FROM table1,table2
WHERE table1.column = table2.column (+);
30
Using Full Outer Joins
CREATE TABLE DEPT11 (DEPTNO NUMBER(4) PRIMARY KEY, DEPTNAME VARCHAR2(20));
32
Anti Joins
• An antijoin between two tables returns rows from the
first table where no matches are found in the second
table.
• Anti-joins are written using the NOT EXISTS or NOT IN
constructs
• To find un-matched data between two tables Anti-Join
is better than using not in or not exists.
• It is the opposite work of semi joins.
Anti Joins
DEPARTMENTS EMPLOYEES
36
Self Joins
WORKER MANAGER
Employee_ID First_Name Manager_ID Employee_ID First_Name
----------- ----------- ------------- ----------- -----------
7839 KING 7839 KING
7698 BLAKE 7839 7698 BLAKE
7782 CLARK 7839 7782 CLARK
7566 JONES 7839 7566 JONES
7654 MARTIN 7839 7654 MARTIN
7499 ALLEN 7839 7499 ALLEN
Utpal/SCA/SQL-5 37
Self Joins
38
Joining a Table to itself
SELECT worker.first_name || 'works for' || manager.
first_name FROM employees worker, employees
manager WHERE
worker.manager_id=manager.employee_id;
Sample Output
………………
39
40