0% found this document useful (0 votes)
2 views

SQL Tutorial (Chap15)

sql

Uploaded by

jaggu011
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL Tutorial (Chap15)

sql

Uploaded by

jaggu011
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

 Chapter 15: Joins

SQL Tutorial - What Is a Join?

 A Join is nothing but a temporary relation between the common columns of the table (or) the
column sharing the common data.
 Join is a mechanism to retrieve data from more than one data base tables.
 The purpose of a join is to combine the data across tables.
 A join is actually performed by the where clause which combines the specified rows of tables.
 JOINS are always defined only between two tables.
 When we read the data from more than one table join is mandatory.
 The default join working in the background is called CROSS JOIN.
 If ‘N’ tables are there then at least (N-1) joins are needed.
 A join is a set operation applicable only to RDBMS.
 REFERENCES is a constraint used to define the relationship between the columns of different
tables but it is a constraint validates the data only during the DML operation.
 Joins are defined at the application level, because of which they are temporary.
 Retrieving the data from more than one table without a join leads to Cartesian product i.e,. all
possible combination of rows.
 Join is a condition to get only the relevant matching rows of different tables.

Joins are categorized based on their usage, below are different types of Joins:-

 Inner Join .
 Outer Join.

Inner Join
Inner join is to retrieve the matching rows of joining tables (or) to retrieve the rows of different
tables those are having matching values.

Types of inner Joins:


 Equi Joins
 Non-Equi Joins

Outer join again has classification based on its usage


 Outer Join is used to retrieve the rows of tables that have non-matching values along with
matching values.
 ‘(+)’ is the symbol used to represent outer join.

Types Of Outer Joins:-


 Left outer join.
 Right Outer Join.
 Full Outer Join.

SQL Tutorial - Inner Join (or) Simple Join


(or) Equi Join

 Inner Join is to retrieve the rows of different tables that having matching values.

 When two tables are joined together using equality of values in one or more columns, they make an
Equi Join.

 Inner Join is also called as Simple Joins or Equi Joins.

 Relationship between the columns of different tables is established by using an operator ‘=‘.

We will be using EMP and DEPT tables for the demonstration of joins. Please get acquainted with the
EMP and DEPT table data.

SQL>SELECT * FROM dept;

SQL> SELECT * FROM emp;


EMP and DEPT table data are joined using ‘=’ operator with DEPT column on both tables.

SQL>SELECT emp. empno,


emp. ename,
emp. deptno,
dept. dname,
dept.loc
FROM EMP, DEPT
WHERE emp.deptno=dept.deptno;
Using Table Aliases:-

SQL>SELECT E.empno, E.ename, E.deptno, D.dname, D.loc FROM emp E, dept D WHERE E.deptno=
D.deptno=10;

Here E,D are alias names for EMP and DEPT Tables and these are to used to avoid the ambiguity
problems.

We can write the same query using ‘USING CLAUSE’ instead of ‘=’.

A different representation of above query, however data returned will be the same.

SQL>SELECT empno, ename, deptno, dname, loc FROM emp JOIN dept USING (deptno) WHERE deptno=1
SQL Tutorial - Left Outer Join
 Left Outer join will fetch all records from left hand side table in a join, irrespective of the
matching.

 In other words Left outer join will display the all the records which are in left hand side table but
not in right hand side table, along with the matching records.

 ‘(+)’ is the symbol used to represent outer join.

 and in Left Outer join we place this symbol ‘(+)’ on left side of join.

Examples:-

For Left outer join we will be using EMP and DEPT tables with deptno as join between the tables.

SQL>SELECT * FROM dept;

– For record with deptno = 40 there are no child records in EMP table, we will be using this record
for demonstration.
SQL>SELECT * FROM emp;
SQL>SELECT e.empno, e.ename, d.deptno, d.dname, d.loc FROM emp e, dept d WHERE e.deptno (+)
From result we can observe that there is a additional record from DEPT table because of Left
Outer Join.

The corresponding columns on EMP side will have NULL values.

<< Inner Join (or) Simple Join (or) Equ

SQL Tutorial - Right Outer Join



Right Outer join will fetch all records from Right hand side table in a join, irrespective of the
matching.

 In other words Right outer join will display the all the records which are in Right hand side table
but not in left hand side table, along with the matching records.

 ‘(+)’ is the symbol used to represent outer join.

 and in Right Outer join we place this symbol ‘(+)’ on Right side of join.

Examples:

For Right outer join we will be using EMP and DEPT tables with deptno as join between the tables.

SQL>SELECT * FROM dept;


SQL>SELECT * FROM emp;

– we can see that there is a new record in EMP table with empno = 3567 and ename = ‘SQL’ . the
same will be used for demonstration.
SQL>SELECT e.empno, e.ename, d.deptno, d.dname, d.loc FROM emp e, dept d WHERE e.deptno =d.
From result we can observe that there is a additional record from EMP table because of Right
Outer Join.

The corresponding columns on DEPT side will have NULL values.

SQL Tutorial - Full Outer Join

 Full Outer Join is to retrieve all the matching data and all additional non-matching data from both
left and right side of the tables.
 In Other words Full Outer join will display the all matching records and the non-matching records
from both tables.
 Full outer join is a combination of both Left outer and Right outer join
For full outer join we will be using the key word ‘FULL OUTER JOIN’ instead of symbol ‘(+)’.

Examples:-
For Full outer joins we will be using EMP and DEPT tables with deptno as join between the tables.
SQL>SELECT * FROM dept;

SQL>SELECT * FROM emp;


– we can see that there is a new record in EMP table with empno = 3567 and ename = ‘SQL’ . the
same will be used for demonstration.

Here we are writing full outer join by using “full outer”…


SQL>SELECT e.empno, e.ename, e.job, e.sal, e.deptno "EMP_DEPTNO", d.dname, d.loc
FROM emp e FULL OUTER JOIN DEPT d
ON (e.deptno=d.deptno);
From result we can observe that there is a additional record from both EMP and DEPT table as
this is full outer join.
The corresponding columns that don’t have data will have NULL values.

SQL Tutorial - Cross Join


 Cross join will fetch all possible combinations between the tables.
 Cross Join is same like not specifying any condition between the tables.
 Retrieving the data from more than one data base table without join condition leads to Cartesian
product
(or) cross product i.e. all possible combination of rows.

When it will be occurring:-


 When the join condition omitted.
 Condition not specified in the where clause.
By using “cross join” key word

Examples:-
SQL> SELECT * FROM dept;

SQL>SELECT * FROM emp;


SQL>SELECT empno, ename, sal, dname, loc FROM emp CROSS JOIN dept;
Or
SQL>SELECT empno,
ename,
sal,
dname,
loc
FROM emp, dept;
These queries will fetch 56 rows in total as EMP table has 14 rows and DEPT table has 5 rows.i.e.
14*4=56 rows will by displayed

By using cross join we can retrieve all possible combinations

SQL Tutorial - Non-Equi Join

 If the relation between the columns of different tables is established by using an operator other
than ‘=’ than that join is called NON- Equi join.

i.e., A join which contains an operator other than ‘=’ in the joins condition but not != (not equls
to).
In a normal case we donot use ‘Non Equi joins’ as result data will have ambiguous results.

Examples:-
Here we are using two tables EMP and SALGRADE and these tables having no primary key and
foreign key relationship
SQL>SELECT * FROM emp;

SQL> SELECT * FROM salgrade;

SQL>SELECT e.empno,
e.ename,
e.deptno,
s.grade
FROM emp e,
salgrade s
WHERE e.sal BETWEEN s.losal AND s.hisal;

You might also like