Oracle Joins
Oracle Joins
Oracle Joins
>
Hyderabad
VILP Unix/C++/Oracle
Content Manual
April 2014
Version 1.0
Version No.
Author
Description
04_VILP_Oracle_Joins
Neelima/4
89810
Revision details:
Action taken Preceding
(add/del/chg page No.
)
New
page No.
Revision
description
Approved by : ________________________________
Date: _________
Date: _________
Page i
Revision
date
Revision
description
Page
No.
Prev
page
No.
Action
taken
Addenda
/New
page
Release
Notice
reference
Page ii
CONTENTS
COURSE 1 - JOINS....................................................................................................................... 1
1.1 Objective................................................................................................................................... 1
1.2 Course Content......................................................................................................................... 1
1.2.1 Introduction....................................................................................................................... 1
1.2.2 Different SQL joins............................................................................................................ 1
1.2.3 Guidelines ........................................................................................................................ 1
1.2.4 Cross Join ........................................................................................................................ 2
1.2.5 Inner Join ......................................................................................................................... 3
1.2.5.1 Equi Join..................................................................................................................... 3
1.2.5.2 Non Equi Join............................................................................................................. 3
1.2.6 Outer Join ........................................................................................................................ 4
1.2.6.1 Left Outer Join............................................................................................................ 4
1.2.6.2 Right Outer Join.......................................................................................................... 5
1.2.6.3 Full Outer Join............................................................................................................ 5
1.2.7 Self Join ........................................................................................................................... 6
1.2.8 Video1: JAVA IM Introduction to JOINS......................................................................7
1.3 Quiz Time.................................................................................................................................. 7
APPENDICES
A. xxxxxxxxxxxx
B. xxxxxxxxxxxx
Page iii
COURSE 1 - JOINS
1.1 Objective
Introduction
Different SQL Joins
Guidelines
Cross Join
Inner Join
Outer Join
Self Join
1.2.3 Guidelines
When writing a SELECT statement that joins tables , precede the column name
with the table name.
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 , a minimum of 'N-1' join conditions are required.
Consider the below tables:
Page 1
Employee:
ENO NAME
DID
------ -------------------26 SRIDHAR
102
11 NEELIMA
101
12 SAILAJA
101
16 SANTOSH
102
17 HEMANTH
103
18 DEEPTHI
102
19 PRASHANTH 103
21 SWATHI
104
22 SUMANTH
104
24 RAJU
DESIGNATION
SALARY
------------------------------ ---------EXECUTIVE
25000
I.T.ANALYST
15000
I.T.ANALYST
16000
I.T.ANALYST
17000
SYSTEMS ANALYST 23000
SYSTEMS ENGINEER 10000
SYSTEMS ENGINEER 9000
EXECUTIVE
21000
EXECUTIVE
23000
EXECUTIVE
22000
Department:
DID
DEPTNAME
LOC_ID
---------- ------------------------------ ---------101
PRODUCTION
101
102
SALES
102
103
PURCHASE
103
104
COMMERCIAL
104
105
IT
105
Grade:
GRA
------A
B
C
D
LOSAL
---------30001
20001
10001
1
HISAL
---------40000
30000
20000
10000
GRA
-----D
B
C
A
C
B
A
D
D
B
C
A
C
LOSAL
---------1
20001
10001
30001
10001
20001
30001
1
1
20001
10001
30001
10001
HISAL
---------10000
30000
20000
40000
20000
30000
40000
10000
10000
30000
20000
40000
20000
Page 2
104 COMMERCIAL
104 COMMERCIAL
104 COMMERCIAL
105 IT
105 IT
105 IT
105 IT
104
104
104
105
105
105
105
B
A
D
B
A
D
C
20001
30001
1
20001
30001
1
10001
30000
40000
10000
30000
40000
10000
20000
The above query returns 20 records. As per the department table DID 101 must
appear only once , but each row of department table is linked with each and
every row of grade table , 101 is appearing 4 times in the resultant set.
SALARY DID
------------ -----------15000
101
16000 101
17000 102
23000 103
10000 102
9000
103
21000 104
23000 104
25000 102
DEPTNAME
------------------------------PRODUCTION
PRODUCTION
SALES
PURCHASE
SALES
PURCHASE
COMMERCIAL
COMMERCIAL
SALES
The above query can also be given with the alias names provided to the table
names. Alias names can be used wherever the table names are required.
Ex: SELECT e.eno, e.name, e.salary, d.did, d.deptname FROM employee e ,
department d WHERE e.did = d.did;
1.2.5.2 Non Equi Join
It is a join condition that is executed when no column in one table is directly
Page 3
linked with a column in another table. The data in the tables are logically related
through appropriate values.
Syntax : SELECT tableA.col1,tableA.col2,tableB.col1,..... FROM
tableA,tableB WHERE tableA.col1 BETWEEN tableB.col2 AND tableB.col3;
Ex: SELECT eno,name,salary,grade FROM employee e,grade g WHERE
e.salary BETWEEN g.losal AND g.hisal;
ENO
---------11
12
16
17
18
19
21
22
24
26
NAME
SALARY GRA
------------------- ---------- -----NEELIMA
15000
C
SAILAJA
16000
C
SANTOSH
17000
C
HEMANTH
23000
B
DEEPTHI
10000
D
PRASHANTH 9000
D
SWATHI
21000
B
SUMANTH
23000
B
RAJU
22000
B
SRIDHAR
25000
B
The above query retrieves the employee details along with his salary grade
based on the join condition.
NAME
SALARY
----------------- -----------SAILAJA
16000
NEELIMA
15000
DEEPTHI
10000
DEPTNO DEPTID
------------- ----------101
101
101
101
102
102
Page 4
DEPTNAME
-----------------------------PRODUCTION
PRODUCTION
SALES
16
26
19
17
22
21
24
SANTOSH
SRIDHAR
PRASHANTH
HEMANTH
SUMANTH
SWATHI
RAJU
17000
25000
9000
23000
23000
21000
22000
102
102
103
103
104
104
102
102
103
103
104
104
SALES
SALES
PURCHASE
PURCHASE
COMMERCIAL
COMMERCIAL
The above query displays all the records which are matching with the join
condition from both the tables along with the rows from employee table which are
not matched with department table.
1.2.6.2 Right Outer Join
It is a join which returns all the rows from the right hand side of the table
specified at the join condition and only those rows from the other table which are
matching with the join condition.
Syntax : SELECT tableA.col1,tableA.col2,tableB.col1,..... FROM
tableA,tableB WHERE tableA.col1(+) = tableB.col1;
Ex: SELECT e.eno ENO, e.name NAME, e.salary SALARY, e.did
DEPTNO,d.did DEPTID , d.deptname DEPTNAME FROM employee e
,department d WHERE e.did(+) = d.did;
ENO NAME
---------- ----------------------26
SRIDHAR
11
NEELIMA
12
SAILAJA
16
SANTOSH
17
HEMANTH
18
DEEPTHI
19
PRASHANTH
21
SWATHI
22
SUMANTH
SALARY DEPTNO
------------- ---------25000
102
15000
101
16000
101
17000
102
23000
103
10000
102
9000
103
21000
104
23000
104
DEPTID
-----------102
101
101
102
103
102
103
104
104
105
DEPTNAME
-----------------------------SALES
PRODUCTION
PRODUCTION
SALES
PURCHASE
SALES
PURCHASE
COMMERCIAL
COMMERCIAL
IT
The above query displays all the records which are matching with the join
condition from both the tables along with the rows from department table which
are not matched with employee table.
1.2.6.3 Full Outer Join
It is a join which returns all the rows from both the tables which are placed on
either side of the join condition.
Syntax : SELECT tableA.col1,tableA.col2,tableB.col1,..... FROM
tableA,tableB WHERE tableA.col1 = tableB.col1(+)
UNION
Syntax : SELECT tableA.col1,tableA.col2,tableB.col1,..... FROM
Page 5
DEPTNAME
-----------------------------PRODUCTION
PRODUCTION
SALES
PURCHASE
SALES
PURCHASE
COMMERCIAL
COMMERCIAL
SALES
IT
The above query displays all the records which are matching with the join
condition from both the tables along with the rows from both employee and
department table which are not matched..
NAME
DID
DESIGNATION
SALARY
---------------------- ---------- ------------------------------ ---------SRIDHAR
102
EXECUTIVE
25000
SANTOSH
102
I.T.ANALYST
17000
DEEPTHI
102
SYSTEMS ENGINEER 10000
The above query is going to display the details of all those employees who are
working in the same department of "SRIDHAR".
Page 6
Page 7