Professional Documents
Culture Documents
Joins
1. Joins are used to extract information from more than one table.
2. Types of Joins-:
a. Inner Join
b. Cross Join
c. Natural Join
d. Self Join
e. Outer Join
Inner Join
Example
==================================================================
2. Display employee name,dname and job of those employees who are not working as
clerk.
Ans=select ename,dname,job from emp join dept on emp.deptno=dept.deptno
where job<>'CLERK';
==================================================================
3. Display employee name, location and empno of those employees who are working in
‘NEW YORK’ and getting salary more than 3000.
Ans=select ename,loc,job
from emp join dept
on emp.deptno=dept.deptno
where loc='NEW YORK' and sal>3000;
Output(Result)
==================================================================
==================================================================
5. select deptno,empno,loc
from emp join dept
on emp.deptno=dept.deptno;
Output(Result)
==================================================================
=================================================================
=================================================================
9. Display name ,job ,comm ,grade and salary of those employees who are working as
manager and getting salary of grade 2.
Ans=select ename,job,comm ,grade,sal
from emp join salgrade
on sal>=losal and sal<=hisal
where job<>'MANAGER' and grade=2;
Output(Result)
=================================================================
10. Display empno,name ,deptno ,losal ,hisal ,grade and salary of those employees who
are not working in deptno 10 and getting salary of grade 4.
Ans=select empno,ename,deptno,losal,hisal,grade,sal
from emp join salgrade
on sal>=losal and sal<=hisal
where deptno<>10 and grade=4;
Output(Result)
=================================================================
=================================================================
=================================================================
=================================================================
=================================================================
15. Display maximum salary, minimum salary and average salary for each location.
Result should not contain records of location NEW YORK. Maximum salary should
not more than 5000.
Ans=select loc,max(sal),min(sal),avg(sal)
from emp join dept
using(deptno)
where loc<>'NEW YORK'
=================================================================
Oracle 9i
Syntax of join
Select <colname1>,<colname2>
From <table1>join <table2>
on <join condition>
where…
Oracle 8i
Syntax of join
Select <colname1>,<colname2>
From <table1>,<table2>
where <join condition> and…
==================================================================
=================================================================
Ans=select count(empno),loc
from emp,dept
where emp.deptno=dept.deptno
=================================================================
==================================================================
==================================================================
Natural Join=
1. This join automatically search the common column between both
the tables.
2. Common column should be having same name and data type.
Syntax
Select <col_name>
From <table1> Natural Join<Table2>
==================================================================
1. select ename,dname from emp natural join dept;
Output(Result)
==================================================================
Note=we cannot join 2 or more tables using natural join, if they do not have
common column.
e.g.
select ename,grade from emp natural join salgrade;
==================================================================
Cross Join
1. Based on Cartesian product (Maximum Possible Combination).
2. In SQL, the CROSS JOIN is used to combine each row of the first table with each row of the
second table.
3. It is also known as the Cartesian join since it returns the Cartesian product of the sets of rows from
the joined tables.
4. The SQL CROSS JOIN produces a result set which is the number of rows in the first table
multiplied by the number of rows in the second table if no WHERE clause is used along
with CROSS JOIN. This kind of result is called as Cartesian Product.
5. Syntax-:
==================================================================
Self Join
1. Used to join a table to itself(Maximum Possible Combination)
2. We can use same tables more than one time.
3. Tables are identified by different aliases.
4. Alias for table is compulsory in self join.
5. Syntax-:
Select <col_name1>,<col_name2>
From <table1> join<table2>
On <join condition>
(self Join)
==================================================================
E K
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
==================================================================
==================================================================
3. Display details of those employees who are earning salary greater than
the salary of ford.
Ans=Select e.* from emp e join emp f on e.sal>f.sal where f.ename='FORD';
Output(Result)
==================================================================
==================================================================
==================================================================
6. Display details of employees who are getting comm more than ALLEN and
getting salary more than 3000.
Ans=Select e.* from emp e join emp A on e.comm>A.comm where
A.ename='ALLEN' and e.sal>1000;
Output(Result)
7. Display ename and manager name of those employees who are working
in NEW YORK.
Ans=Select e.ename,m.ename as MANAGER
from emp e join emp m
on e.mgr=m.empno
join dept d
on e.deptno=d.deptno
where d.loc='NEW YORK';
Output(Result)
ENAME MANAGER
CLARK KING
MILLER CLARK
==================================================================
Output(Result)
ENAME MANAGERS DNAME
BLAKE KING SALES
==================================================================
Output(Result)
==================================================================
10. Display ename ,sal of those employees who are managed by KING
Ans=Select e.ename,e.sal
from emp e join emp k
on e.mgr=k.empno
where k.ename='KING';
Output(Result)
ENAME SAL
JONES 2975
BLAKE 2850
CLARK 2450
==================================================================
11. Display employees who are working same as 'MARTINE' in king’s department
.Result should not contain records of those employees who are working as ROCK and
joined before 'ALLEN’. Employees should not get commission more than turner and
getting sal of grade 5.
Ans=Select e.*
from emp e join emp m
on e.job=m.job
Output(Result)
no rows selected
===========================================================================
Outer Join
1. This join is used to extract common +extra information from more than 1 table.
2. Types of Outer Join
a. Left Outer Join
b. Right Outer Join
c. Full Outer Join
3. Syntax
Select <colname1>,<col2>
From <table1> left/right/full outer join <table 2>
On <Join Condition>
1. Display ename,dname of all employees also display name of those employees who are
not working in any department.
Ans=Select ename,dname
from emp left outer join dept
on emp.deptno=dept.deptno ;
Output(Result)
ENAME DNAME
===========================================================================
2. Display ename,dname of all employees and also display name of that department in
which no employee is working.
Ans=Select ename,dname
from emp right outer join dept
on emp.deptno=dept.deptno;
Output(Result)
ENAME DNAME
SMITH RESEARCH
ALLEN SALES
WARD SALES
JONES RESEARCH
MARTIN SALES
BLAKE SALES
CLARK ACCOUNTING
SCOTT RESEARCH
KING ACCOUNTING
TURNER SALES
ADAMS RESEARCH
JAMES SALES
FORD RESEARCH
MILLER ACCOUNTING
ENAME DNAME
OPERATIONS
===========================================================================
3. Display ename & manager name of all employees and also display name of those
employees who are not managed by any one.
Output(Result)
ENAME MANAGER
FORD JONES
SCOTT JONES
JAMES BLAKE
TURNER BLAKE
MARTIN BLAKE
WARD BLAKE
ALLEN BLAKE
MILLER CLARK
ADAMS SCOTT
CLARK KING
BLAKE KING
JONES KING
SMITH FORD
KING
===========================================================================
4. Display ename & manager name of all employees and also display name of those
employees who are not manager of any employee.
ENAME MANAGER
SMITH FORD
ALLEN BLAKE
WARD BLAKE
JONES KING
MARTIN BLAKE
BLAKE KING
CLARK KING
SCOTT JONES
TURNER BLAKE
ADAMS SCOTT
===========================================================================
5. Display ename & manager name of all employees and also display name of those
employees who are not manager of any employee and also display name of those
employees who are not managed by any one.
===========================================================================