SQL Joins
SQL Joins
SQL Joins
Dr.Manjushree Nayak
Associate Professor,
School of CSE
Dr.Manjushree [2]
Nayak
Joins
There are two syntax styles in Oracle 11g:
National Institute of Science & Technology
a) Oracle syntax
b) ANSI syntax
Dr.Manjushree [3]
Nayak
Simple Joins
• The most common operator used to relate two tables
National Institute of Science & Technology
Dr.Manjushree [5]
Nayak
Equijoin
Query2: Find the employee number, name,
National Institute of Science & Technology
Predicate Clause
Dr.Manjushree [6]
Nayak
Equijoin
Query3: Find the employee number, name,
National Institute of Science & Technology
Dr.Manjushree [7]
Nayak
Equijoins
To execute a join of three or
National Institute of Science & Technology
Dr.Manjushree [8]
Nayak
Using Table Alias
• Like columns, tables can also have alias names.
National Institute of Science & Technology
Dr.Manjushree [10]
Nayak
Types of Joins
There are 6 types of Joins in ANSI syntax:
National Institute of Science & Technology
1. Natural Join
2. Inner Join
3. LEFT Outer Join
4. RIGHT Outer Join
5. FULL Outer Join
6. Cross Join
Dr.Manjushree [11
Nayak ]
Natural Join
• The NATURAL keyword indicates a natural join,
National Institute of Science & Technology
Dr.Manjushree [12]
Nayak
Natural Join
National Institute of Science & Technology
AA BB DD
200 BAT APPLE
400 RAT PEACH
Dr.Manjushree [13]
Nayak
Select * from FOO natural join BAR;
National Institute of Science & Technology
Dr.Manjushree [14]
Nayak
Select * from foo natural join small;
National Institute of Science & Technology
Dr.Manjushree [15]
Nayak
Natural Join
• Find the employee number, name and their
National Institute of Science & Technology
Dr.Manjushree [16]
Nayak
NATURAL JOIN
• We cannot qualify the column names when
National Institute of Science & Technology
Dr.Manjushree [17]
Nayak
INNER JOIN
JOIN..USING
• If there are many columns that have the same names in the
National Institute of Science & Technology
tables we are joining and they do not have the same datatype,
or we want to specify the columns, we can use
JOIN..USING syntax.
Syntax: tablename1 inner join tablename2 using(columnname);
• Inner keyword is optional.
• The USING clause specifies the column names that should
be used to join the tables.
• The column names should not be qualified with a table name
or table alias.
• Columns participating in join will appear once in a result set.
Dr.Manjushree [18]
Nayak
Join..Using
Select * from FOO inner join BAR using(ACOL);
National Institute of Science & Technology
Dr.Manjushree [19]
Nayak
Select * from FOO join BAR using(BCOL);
National Institute of Science & Technology
Dr.Manjushree [20]
Nayak
Join..Using
• Find the employee number, name, and their
National Institute of Science & Technology
department
name in which they are working.
Select empno,ename, dname from emp join
• dept
Find the employee number, name. department number,
using(deptno);
salary those who are working in ACCOUNTING
department.
Select empno, ename, emp.deptno, sal from emp join dept
using(deptno) where dname=‘ACCOUNTING’;
Dr.Manjushree [21]
Nayak
Join..using
• If there is no common column then specifying column name in
National Institute of Science & Technology
[22]
Join..ON
• When we do not have common column names between tables
National Institute of Science & Technology
foo.acol=bar.acol;
Dr.Manjushree [24]
Nayak
JOIN..ON
Select * from FOO join BAR on
National Institute of Science & Technology
Dr.Manjushree [25]
Nayak
Examples
• Find the employee number, name, department name, department number
National Institute of Science & Technology
• Find the annual salary, maximum and minimum salary of all the
employees those who are working in “Research” department.
Select sal*12 “Annual Salary”, max(sal) “Maximum”, min(sal)
“Minimum” from emp e join dept d on e.deptno=d.deptno where
d.dname=‘Research’;
Dr.Manjushree [26]
Nayak
Examples
• Find the total number of employees working
National Institute of Science & Technology
Dr.Manjushree [27]
Nayak
Outer Joins
• In natural join, we cannot retrieve all the unmatched
National Institute of Science & Technology
Dr.Manjushree [28]
Nayak
Types of Outer Join
• LEFT OUTER JOIN: For all rows in A that have
National Institute of Science & Technology
Dr.Manjushree [29]
Nayak
LEFT OUTER JOIN
Select * from FOO F left outer join
National Institute of Science & Technology
Dr.Manjushree [30]
Nayak
Left Outer Join
Select * from FOO F left join BAR B on F.ACOL=
National Institute of Science & Technology
Dr.Manjushree [31]
Nayak
Left Outer Join
• Oracle Syntax:
National Institute of Science & Technology
Dr.Manjushree [32]
Nayak
RIGHT OUTER JOIN
SELECT * from FOO F RIGHT OUTER JOIN BAR B on
National Institute of Science & Technology
Dr.Manjushree [33]
Nayak
RIGHT OUTER JOIN
National Institute of Science & Technology
Dr.Manjushree [34]
Nayak
RIGHT OUTER JOIN
• Oracle Syntax:
National Institute of Science & Technology
Dr.Manjushree [36]
Nayak
FULL OUTER JOIN
SELECT * from FOO F FULL OUTER JOIN BAR B on
National Institute of Science & Technology
Dr.Manjushree [37]
Nayak
FULL OUTER JOIN
• Oracle Syntax:
National Institute of Science & Technology
Dr.Manjushree [38]
Nayak
FULL OUTER JOIN
Select * from FOO F, BAR B
National Institute of Science & Technology
Dr.Manjushree [39]
Nayak
CROSS JOIN
• A Cartesian join occurs when data is selected from
National Institute of Science & Technology
Dr.Manjushree [40]
Nayak
CROSS JOIN
Syntax: table1 cross join table2
National Institute of Science & Technology
Dr.Manjushree [41]
Nayak
Self Join
• When a table is joined by itself it is called as self
National Institute of Science & Technology
join.
• The table name appears in the FROM clause
twice, with different alias names.
• The two aliases are treated as two different tables,
and they are joined as we would join any other
tables, using one or more related columns.
Dr.Manjushree [42]
Nayak
Self-Join
Find the employee name and their supervisor names.
National Institute of Science & Technology
Dr.Manjushree [43]
Nayak
Self Join
Select e.empno, e.ename, e.mgr, s.empno, s.ename
National Institute of Science & Technology
Dr.Manjushree [44]
Nayak
National Institute of Science & Technology
[45]