SQL Tutorial (Chap15)
SQL Tutorial (Chap15)
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.
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.
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 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.
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.
– 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.
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.
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.
– 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.
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;
Examples:-
SQL> SELECT * FROM dept;
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 e.empno,
e.ename,
e.deptno,
s.grade
FROM emp e,
salgrade s
WHERE e.sal BETWEEN s.losal AND s.hisal;