Joins 2
Joins 2
EQUI JOIN
NON EQUI JOIN
SELF JOIN
INNER JOIN
OUTER JOIN
o LEFT OUTER JOIN
o RIGHT OUTER JOIN
o FULL OUTER JOIN
CROSS JOIN
NATURAL JOIN
NOTE:
When we retrieve data from multiple tables based on “WHERE” clause condition
ANSI joins.
1. EQUI JOIN
A join which contains an equal to ‘=’ operator in the joins condition. Only matching
records are displayed. Here joining conditional columns must belongs to the same data
types. The tables having the common columns then only we are using equi joins. Equi
joins are also called simple joins or inner joins.
Syntax:-
Examples:-
Sql> SELECT emp. empno, emp. ename, deptno, dept. dname, dept.loc
WHERE emp.deptno=dept.deptno;
ERROR:- column ambiguously defined
To avoid ambiguity error in future generally we must specify every column name
along
If there no common column names between the two tables the qualification is not
necessary But it is better.
Note :- using equi join we are retrieving only matching rows only.
Sql> SELECT emp. empno, emp. ename,emp. deptno, dept. dname, dept.loc
WHERE emp.deptno=dept.deptno;
Explaination:-
• T he from clause specifies the two tables that the database must access:
Emp table
Dept table
Emp.deptno=dept.deptno
Because deptno column is common to the both tables,it must be prefixed by the table
name to avoid ambiguity.
where emp.deptno=dept.deptno;
where emp.deptno=dept.deptno
and loc='CHICAGO';
Qualifying column names with the table names can be very time consuming,
particularly if table names are lengthy.we can use table aliases instead of table names.
Just as column aliases gives column another name,a table aliases gives a table another
name. Table aliases help to keep sql code smaller ,therefore using less memory. Table
alias names will improve the performance of while retrieving data through joins. The
table name is specified full ,followed by space and then the table alias. The alias can
be up to 30 characters in length,but the shorter they are the better. The table alias
should be meaningful. The table alias is valid only for the current sql statement.
Note:-the emp table has been given an alias of ‘e’,and the dept table has as alias of ‘d’.
where e.deptno=d.deptno;
where e.deptno=d.deptno
where e.deptno=d.deptno
group by dname
having sum(sal)>8000;
2. NON-EQUI JOIN
A join which contains an operator other than equal to ‘=’ in the joins condition.
The SQL NON EQUI JOIN uses comparison operator instead of the equal sign like >, <,
>=, <= along with conditions.
Syntax:-
Examples:-
Explaination:-all of the employees salaries lie within the limits provided by the
salgrade table. That is no employee earns less than the lowest value contained in the
losal column, or more than the highest value contained in the hisal column
other conditions such as <=,>= can be used ,but between is simplest. Remember to
specify the low value first and high value last when using between.
Sql> select empno,ename,job,dname,loc from emp e,dept d where e.deptno > d.deptno;
3. Self Join:-
A self join is a join of a table to itself. This table appears twice in the FROM clause
and is followed by table aliases that qualify column names in the join condition. To
perform a self join, Oracle Database combines and returns rows of the table that satisfy
the join condition.
Here joining conditional columns must by belongs to the same data type.
When ever we are working with self joins in database we should write alias
Names to tables.
Syntax:-
Examples:-
where e.mgr=m.empno;
explaination:-the above employee joins the emp table to itself. To simultate two tables in
the from clause,there two aliases ,namely e and m for the same table ,emp.in this
example ,the where clause contains the join that means “where employee (e) mgr
number matches the empno for the employee(m)
where e1.mgr=e2.empno
and e1.hiredate<e2.hiredate;
INNER JOIN:-
This will display all the records that have matched. When tables contains
common column,then only this join is used and Inner Join is also called as Simple Joins
or Equi Joins.
Syntax:-
on (e.deptno=d.deptno);
Explaination:-the on clause can be used as follows to join columns that have different
names.
sql>
select emp.ename from emp join dept on (emp.ename like 'A%') and emp.deptno =
dept.deptno;
A1 TABLE A2 TABLE
A B C A B
X Y Z X Y
OUTTER JOIN: It is an extension for the Equi join. In Equi join condition we will be
getting the matching data from the tables only. So we loss un matching data from the
tables.
To overcome the above problem we use outer join which are used to getting matching
data as well as UN matching data from the tables. This outer join again classified into
three types
LEFT OUTER JOIN: It will retrieve or get matching data from both table as well as UN
matching data from left hand side table
RIGHT OUTER JOIN: It will retrieve or get matching data from both table as well as UN
matching data from right hand side table
FULL OUTER JOIN: It will retrieve or get matching data from both table as well as UN
matching data from left hand side table plus right hand side table also.
CROSS JOIN: Cross join is used to join more than two tables without any condition we
call as a cross join. In cross join each row of the first table join with each row of the
second table.
So, if the first table contain ‘m’ rows and second table contain ‘n’ rows then output will
be ‘m*n’ rows.
Syntax:-
Examples:-
Sql>select empno,ename,job,dname,loc from emp cross join dept;
NATURAL JOIN: SQL Server not supporting Natural join whereas supports in ORACLE.
CREATE TABLE STUDENTS (SID Int, SNAME varchar (20), SMBNO char (10), CID Int)
CREATE TABLE COURSES (CID int, CNAME Varchar (20), CFEE decimal (6, 2))
INSERT COURSES VALUES (10,'c', 500), (20,'c++', 1000), (50,'sql', 1800), (60,’.net’, 3500),
(70,’sap’, 8000)
Select * from course c inner join student s on c.cid=s.cid inner join register r on
s.cid=r.cid;
Select * from student s left outer join course c on s.cid=c.cid left outer join register r on
c.cid=r.cid