0% found this document useful (0 votes)
12 views45 pages

SQL Joins

Uploaded by

Subham Jena
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)
12 views45 pages

SQL Joins

Uploaded by

Subham Jena
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/ 45

National Institute of Science & Technology

SQL Joins

Dr.Manjushree Nayak
Associate Professor,
School of CSE

Dr.Manjushree Nayak [1]


Joins in Oracle
• Oracle performs a join whenever multiple tables appear in the
National Institute of Science & Technology

query’s FROM clause.


• The query’s SELECT clause can have the columns
or
expressions from any or all of these tables.
• In order to query data from more than one table, we need to
identify common columns that relate the two tables.
• In the WHERE clause, we define the relationship between the
tables listed in the FROM clause using comparison operators.

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

is the equality operator (=).


• If we relate two tables using an equality operator, it
is an equality join, also known as an equijoin.
• This type of join combines rows from two tables that
have equivalent values for the specified columns.

Dr.Manjushree Nayak [4]


Equijoin
Query1:
National Institute of Science & Technology

Find the employee number, name, department name of all the


employees.
Select empno, ename, dname
from emp, dept
where emp.deptno = dept.deptno;
• Qualifying column names avoids ambiguity and
increases
the readability of the query.
• If the same column name belongs to more than one table
each column name must be qualified (except when using
SQL ANSI syntax).

Dr.Manjushree [5]
Nayak
Equijoin
Query2: Find the employee number, name,
National Institute of Science & Technology

department name of all the employees those who are


working in SALES department.
Select empno, name, dname,
from emp, dept
where emp.deptno=dept.deptno and
dname=‘SALES’;

Predicate Clause

Dr.Manjushree [6]
Nayak
Equijoin
Query3: Find the employee number, name,
National Institute of Science & Technology

department name, department number those who are


working in the department located in Boston.
Select empno, name, dname, emp.deptno
from emp, dept
where emp.deptno=dept.deptno and
loc=‘Boston’;

Dr.Manjushree [7]
Nayak
Equijoins
To execute a join of three or
National Institute of Science & Technology

more tables, Oracle takes these steps:


 Oracle joins two of the tables based on the
join conditions, comparing their columns.
 Oracle joins the result to another table, based
on join conditions.
 Oracle continues this process until all tables
are joined into the result.

Dr.Manjushree [8]
Nayak
Using Table Alias
• Like columns, tables can also have alias names.
National Institute of Science & Technology

Specify the table alias name next to the table,


separated with a space.
So, Query3 can be written as:
Select empno, name, dname, e.deptno
from emp e, dept d
where e.deptno=d.deptno and
loc=‘Boston’;
• Table aliases increase the
readability of the query.
Dr.Manjushree [9]
Nayak
ANSI Syntax
• The difference between traditional Oracle join syntax and
National Institute of Science & Technology

the ANSI syntax is that in ANSI, the join type is specified


explicitly in the FROM clause.
Simple joins can have the following forms:
 <table name> NATURAL [INNER] JOIN <table name>
 <table name> [INNER] JOIN <table name>
USING
(<columns>)
 <table name> [INNER] JOIN <table name>
ON
<condition>

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

where the join is based on all columns with same name


in both tables.
• It selects rows from the two tables that have
same(equal) values in the common columns.
• Syntax: tablename1 NATURAL JOIN tablename2
• In this type of join, we should not qualify the column
names with the table name or table alias name.
• When specifying NATURAL JOIN, the columns with
the same name in both tables should also have same
datatype.

Dr.Manjushree [12]
Nayak
Natural Join
National Institute of Science & Technology

Select * from Animal natural join fruit;

AA BB DD
200 BAT APPLE
400 RAT PEACH

Dr.Manjushree [13]
Nayak
Select * from FOO natural join BAR;
National Institute of Science & Technology

ACOL BCOL CCOL GCOL


101 AA 100 200

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

department name in which they are working for


all the employees.
Select empno, ename, dname from emp natural join
dept;

In this query, the common column between both the


tables is deptno and that column is used to join the
tables.

Dr.Manjushree [16]
Nayak
NATURAL JOIN
• We cannot qualify the column names when
National Institute of Science & Technology

using the NATURAL JOIN clause.


Select empno, ename, dname,
e.deptno from emp e natural join dept;
Select e.empno, ename, dname, e.deptno from emp
e natural join dept;
*
ERROR at line 1:
ORA-25155: column used in NATURAL
join cannot have qualifier

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

the using clause will generate an error.


• WHERE clause can be used to limit the number of rows and
orderby clause to sort the rows retrieved along with any type of
join operation.

[22]
Join..ON
• When we do not have common column names between tables
National Institute of Science & Technology

to make a join or if we want to specify arbitrary join


conditions, we may use the JOIN ON syntax.
• We may qualify column names with a table name or alias
name.
• If the column name is common to multiple tables involved in
the query, those column names must be qualified.
• Syntax: table1 join table2 on (condition);
• Condition mentioned in ON clause will be used to join rows
from both the table.
• Condition can be equality like previous cases: equijoin. If the
condition is based on other operators it is non-equijoin
Dr.Manjushree [23]
Nayak
Join..On
Select * from FOO join BAR on
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

FOO.ACOL = BAR.ACOL and FOO.BCOL =


BAR.BCOL;

Dr.Manjushree [25]
Nayak
Examples
• Find the employee number, name, department name, department number
National Institute of Science & Technology

for all the employees who work in department located at “DALLAS”.


Select empno,ename, dname, e.deptno from emp e join dept d
on
e.deptno=d.deptno where d.loc=‘Dallas’;

• 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

in department located at “New York”.


Select count(*) “Total employees” from emp e
join dept d on e.deptno=d.deptno where loc=‘New
• York’;
Find the employees those who are working
“Research” department and in whose
unknown. commission
Select empno,ename from emp e join is dept d on
e.deptno=d.deptno where dname=‘RESEARCH’ and
comm IS NULL;

Dr.Manjushree [27]
Nayak
Outer Joins
• In natural join, we cannot retrieve all the unmatched
National Institute of Science & Technology

data from both the tables.


• An outer join extends the result of a simple join.
• An outer join returns all rows that satisfy the join
condition and also returns some or all of those rows
from one table for which no rows from the other
satisfy the join condition.

Dr.Manjushree [28]
Nayak
Types of Outer Join
• LEFT OUTER JOIN: For all rows in A that have
National Institute of Science & Technology

no matching rows in B, the query returns NULL


values for the columns in B.
• RIGHT OUTER JOIN: For all rows in B that have
no matching rows in A, the query returns NULL
values for the columns in A.
• FULL OUTER JOIN: Returns all rows from both
A and B embedded with NULL for those rows
which do not satisfy the join condition.

Dr.Manjushree [29]
Nayak
LEFT OUTER JOIN
Select * from FOO F left outer join
National Institute of Science & Technology

BAR B on F.ACOL= B.ACOL and F.BCOL =


B.BCOL;

Dr.Manjushree [30]
Nayak
Left Outer Join
Select * from FOO F left join BAR B on F.ACOL=
National Institute of Science & Technology

B.ACOL and F.BCOL = B.BCOL;


• The keyword OUTER is optional.

Dr.Manjushree [31]
Nayak
Left Outer Join
• Oracle Syntax:
National Institute of Science & Technology

Select * from FOO F, BAR B where


F.ACOL = B.ACOL(+);

• Place (+) operator on the left or right side of the


conditional for right or left outer join respectively.
• In the above example, + operator is place in right
hand side of conditional operator in order to add
NULL values for non-matching records found.

Dr.Manjushree [32]
Nayak
RIGHT OUTER JOIN
SELECT * from FOO F RIGHT OUTER JOIN BAR B on
National Institute of Science & Technology

F.ACOL=B.ACOL and F.BCOL=B.BCOL;

Dr.Manjushree [33]
Nayak
RIGHT OUTER JOIN
National Institute of Science & Technology

SELECT * from FOO F RIGHT OUTER JOIN


BAR B on F.ACOL=B.ACOL and
F.BCOL=B.BCOL;

Dr.Manjushree [34]
Nayak
RIGHT OUTER JOIN
• Oracle Syntax:
National Institute of Science & Technology

Select * from FOO F, BAR B where F.ACOL(+) =


B.ACOL;

• In the above example, + operator is place in left


hand side of conditional operator in order to add
NULL values for non-matching records found.

Dr.Manjushree Nayak [35]


FULL OUTER JOIN
National Institute of Science & Technology

SELECT * from FOO F FULL OUTER


JOIN BAR B on F.ACOL=B.ACOL and
F.BCOL=B.BCOL;

Dr.Manjushree [36]
Nayak
FULL OUTER JOIN
SELECT * from FOO F FULL OUTER JOIN BAR B on
National Institute of Science & Technology

F.ACOL=B.ACOL and F.BCOL=B.BCOL;

Dr.Manjushree [37]
Nayak
FULL OUTER JOIN
• Oracle Syntax:
National Institute of Science & Technology

Select * from FOO F, BAR B


where F.ACOL(+) = B.ACOL(+);
• In the above example, + operator is placed on both
left and right hand side of conditional operator raises
an error.
• The full outer join can be achieved using the UNION
operator and the outer-join operator.

Dr.Manjushree [38]
Nayak
FULL OUTER JOIN
Select * from FOO F, BAR B
National Institute of Science & Technology

where F.ACOL = B.ACOL(+)


UNION
Select * from FOO F, BAR B
where F.ACOL(+) = B.ACOL;

Dr.Manjushree [39]
Nayak
CROSS JOIN
• A Cartesian join occurs when data is selected from
National Institute of Science & Technology

two or more tables and there is no common relation


specified in the WHERE clause.
• If we do not specify a join condition for the tables
listed in the FROM clause, Oracle joins each row
from the first table to every row in the second table.
• If the first table has 3 rows and the second table has
4 rows, the result will have 12 rows.
• If we add another table with 2 rows without
specifying a join condition, the result will have 24
rows.

Dr.Manjushree [40]
Nayak
CROSS JOIN
Syntax: table1 cross join table2
National Institute of Science & Technology

Select * from AA cross join BB;

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

Select e.empno, e.ename, e.mgr, s.empno, s.ename


from emp e join emp s
on e.mgr=s.empno;

Dr.Manjushree [43]
Nayak
Self Join
Select e.empno, e.ename, e.mgr, s.empno, s.ename
National Institute of Science & Technology

from emp e left join emp s


on e.mgr=s.empno;

Dr.Manjushree [44]
Nayak
National Institute of Science & Technology
[45]

You might also like