You are on page 1of 21

Joins in SQL

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

1. This Join is used to extract common information from multiple tables.


2. Syntax

Select <col1>,<col2> from <table1> join | Inner Join <table2>


On <join condition>
where <condition>
group by <column_name>
having <group_function>
order by <column_name>

Example

1. Display employee name,dname,location and empno of all employees.


Ans=select ename,dname,loc,empno,deptno
from emp join dept
using(deptno); --common column
--on emp.deptno=dept.deptno; --Not common column
output(Result)

==================================================================

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';

Page |1 Thursday, 25 November 2021


Joins in SQL
output(Result)

==================================================================

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)

==================================================================

4. Display maximum sal for each job location


Ans=select loc,max(sal)
from emp join dept
on emp.deptno=dept.deptno
group by loc;
Output(Result)

==================================================================

5. select deptno,empno,loc
from emp join dept
on emp.deptno=dept.deptno;
Output(Result)

select deptno,empno,loc from emp join dept on emp.deptno=dept.deptno


*
ERROR at line 1:
ORA-00918: column ambiguously defined

==================================================================

Page |2 Thursday, 25 November 2021


Joins in SQL
6. select dept.deptno,empno,loc
from emp join dept
on emp.deptno=dept.deptno;
Output(Result)

=================================================================

7. Display deptno ,empno and location of all employees.


Ans=select deptno,empno,loc
from emp join dept using(deptno);
Output(Result)

=================================================================

8. Display name ,salary and grade of all employees.


Ans=select ename,sal,grade
from emp join salgrade
on sal>=losal and sal<=hisal;
Output(Result)

Page |3 Thursday, 25 November 2021


Joins in SQL
=================================================================

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)

=================================================================

11. Display employee name ,dname of all the employees.


Ans=select ename,dname
from emp join dept using(deptno);
Output(Result)

=================================================================

12. Display employee name,dname and grade of all employees.

Page |4 Thursday, 25 November 2021


Joins in SQL
Ans=select ename,dname,grade
from emp join dept using(deptno) join salgrade
on sal>=losal and sal<=hisal;
Output(Result)

=================================================================

13. Display location wise count of employees.


Ans=select loc,count(empno) from emp
join dept using(deptno)
group by loc;
Output(Result)

=================================================================

14. Display total employees for each grade type.


Ans=select grade,count(empno)
from emp join salgrade
on sal>=losal and sal<=hisal
group by grade;
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'

Page |5 Thursday, 25 November 2021


Joins in SQL
having max(sal)<5000 group by loc;
Output(Result)

=================================================================

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…

==================================================================

16.Display employee name ,dname of all the employees (Syntax of Oracle


8i)
Ans=select ename,dname
from emp ,dept
where emp.deptno=dept.deptno;
Output(Result)

=================================================================

17.Display location wise count of employees. (Syntax of Oracle 8i)

Ans=select count(empno),loc
from emp,dept
where emp.deptno=dept.deptno

Page |6 Thursday, 25 November 2021


Joins in SQL
group by loc;
Output(Result)

=================================================================

18. Display name,dname and deptno of all the employees


Ans=select ename,dname,deptno
from emp join dept
using(deptno);
Output(Result)

==================================================================

Types of Inner Join


a. Equi Join= (e.g emp join dept),where commom column exists.
b. Non Equi Join = (e.g emp join salgrade),where commom column not exists.

==================================================================

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)

Page |7 Thursday, 25 November 2021


Joins in SQL

==================================================================

2. select ename,loc 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-:

Page |8 Thursday, 25 November 2021


Joins in SQL
Select <col_name1>,<col_name2>
From <table1> cross join<table2>

1. Select ename,dname from emp cross join dept;

2. Select ename,dname from emp ,dept; (Syntax of oracle 8i)


Output(Result)

Page |9 Thursday, 25 November 2021


Joins in SQL

==================================================================

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>

P a g e | 10 Thursday, 25 November 2021


Joins in SQL

(self Join)

==================================================================

1. Display employee name and manager name of all the employees.


Ans=Select e.ename,m.ename as Manager from emp e join emp m on
e.mgr=m.empno;
Output(Result)
ENAME MANAGER
SMITH FORD
ALLEN BLAKE
WARD BLAKE
JONES KING
MARTIN BLAKE
BLAKE KING
CLARK KING
SCOTT JONES
TURNER BLAKE
ADAMS SCOTT
JAMES BLAKE
FORD JONES
MILLER CLARK

E K
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800 20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975 20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

P a g e | 11 Thursday, 25 November 2021


Joins in SQL
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7782 CLARK MANAGER 7839 09-JUN-81 2450 10


7369 SMITH CLERK 7902 17-DEC-80 800 20

7788 SCOTT ANALYST 7566 19-APR-87 3000 20


7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7839 KING PRESIDENT 17-NOV-81 5000 10


7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30


7566 JONES MANAGER 7839 02-APR-81 2975 20

7876 ADAMS CLERK 7788 23-MAY-87 1100 20


7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7900 JAMES CLERK 7698 03-DEC-81 950 30


7698 BLAKE MANAGER 7839 01-MAY-81 2850 30

7902 FORD ANALYST 7566 03-DEC-81 3000 20


7782 CLARK MANAGER 7839 09-JUN-81 2450 10

7934 MILLER CLERK 7782 23-JAN-82 1300 10


7788 SCOTT ANALYST 7566 19-APR-87 3000 20

7839 KING PRESIDENT 17-NOV-81 5000 10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7876 ADAMS CLERK 7788 23-MAY-87 1100 20

7900 JAMES CLERK 7698 03-DEC-81 950 30

7902 FORD ANALYST 7566 03-DEC-81 3000 20

7934 MILLER CLERK 7782 23-JAN-82 1300 10

==================================================================

2. Display employee name, joining date,manager name and joining date of


manager of those employees who have joined before their manager.
Ans=Select e.ename,e.hiredate ,m.ename,m.hiredate from emp e join emp m on
e.mgr=m.empno where e.hiredate<m.hiredate;
Output(Result)
ENAME HIREDATE ENAME HIREDATE
SMITH 17-DEC-80 FORD 03-DEC-81
ALLEN 20-FEB-81 BLAKE 01-MAY-81
WARD 22-FEB-81 BLAKE 01-MAY-81
JONES 02-APR-81 KING 17-NOV-81
BLAKE 01-MAY-81 KING 17-NOV-81
CLARK 09-JUN-81 KING 17-NOV-81

==================================================================

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)

P a g e | 12 Thursday, 25 November 2021


Joins in SQL
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7839 KING PRESIDENT 17-NOV-81 5000 10

==================================================================

4. Display details of those employees who are working in king’s


department.
Ans=Select e.* from emp e join emp k on e.deptno=k.deptno where
k.ename='KING';
Output(Result)

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10

==================================================================

5. Display details of those employees who have joined before BLAKE.


Ans=Select e.*
from emp e join emp b
on e.hiredate<b.hiredate
where b.ename='BLAKE';
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)

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

P a g e | 13 Thursday, 25 November 2021


Joins in SQL
==================================================================

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

==================================================================

8. Display ename and manager name and departname of those employees


who are working as Manager in sales department.

Ans=Select e.ename,m.ename as MANAGERS,dname


from emp e join emp m
on e.mgr=m.empno
join dept d
on e.deptno=d.deptno
where d.dname='SALES' and e.job='MANAGER';

Output(Result)
ENAME MANAGERS DNAME
BLAKE KING SALES

==================================================================

9. Display ename and manager name,grade and loc of all employees.


Ans=Select e.ename,m.ename ,dname,grade,loc
from emp e join emp m
on e.mgr=m.empno
join dept d
on e.deptno=d.deptno
join salgrade
on e.sal>=losal and e.sal<=hisal;

Output(Result)

P a g e | 14 Thursday, 25 November 2021


Joins in SQL
ENAME ENAME DNAME GRADE LOC
SMITH FORD RESEARCH 1 DALLAS
ADAMS SCOTT RESEARCH 1 DALLAS
JAMES BLAKE SALES 1 CHICAGO
WARD BLAKE SALES 2 CHICAGO
MARTIN BLAKE SALES 2 CHICAGO
MILLER CLARK ACCOUNTING 2 NEW YORK
ALLEN BLAKE SALES 3 CHICAGO
TURNER BLAKE SALES 3 CHICAGO
JONES KING RESEARCH 4 DALLAS
BLAKE KING SALES 4 CHICAGO
CLARK KING ACCOUNTING 4 NEW YORK
SCOTT JONES RESEARCH 4 DALLAS
FORD JONES RESEARCH 4 DALLAS

==================================================================

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

P a g e | 15 Thursday, 25 November 2021


Joins in SQL
join emp k
on e.deptno=k.deptno
join emp b
on e.job<>b.job
join emp a
on e.hiredate<a.hiredate
join emp t
on e.comm <t.comm
join salgrade
on e.sal between losal and hisal
where m.ename='MARTINE' and k.ename='KING' and
r.ename='BLAKE' and a.ename='ALLEN' and t.ename='TURNER' and
grade=5;

Output(Result)
no rows selected

===========================================================================

P a g e | 16 Thursday, 25 November 2021


Joins in SQL

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

P a g e | 17 Thursday, 25 November 2021


Joins in SQL
MILLER ACCOUNTING
KING ACCOUNTING
CLARK ACCOUNTING
FORD RESEARCH
ADAMS RESEARCH
SCOTT RESEARCH
JONES RESEARCH
SMITH RESEARCH
JAMES SALES
TURNER SALES
BLAKE SALES
MARTIN SALES
WARD SALES
ALLEN SALES

===========================================================================

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.

P a g e | 18 Thursday, 25 November 2021


Joins in SQL
Ans=Select e.ename,m.ename as 'MANAGER'
from emp e left outer join emp m
on e.mgr=m.empno;

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.

Ans=select e.ename,m.ename Manager from emp e right outer join emp


m on e.mgr=m.empno;
Output(Result)

ENAME MANAGER
SMITH FORD
ALLEN BLAKE
WARD BLAKE
JONES KING
MARTIN BLAKE
BLAKE KING
CLARK KING
SCOTT JONES
TURNER BLAKE
ADAMS SCOTT

P a g e | 19 Thursday, 25 November 2021


Joins in SQL
JAMES BLAKE
FORD JONES
MILLER CLARK
TURNER
ENAME MANAGER
WARD
MARTIN
ALLEN
MILLER
SMITH
ADAMS
JAMES

===========================================================================

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.

Ans=select e.ename,m.ename Manager from emp e full outer join emp m


on e.mgr=m.empno;
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
ENAME MANAGER
TURNER

P a g e | 20 Thursday, 25 November 2021


Joins in SQL
WARD
MARTIN
ALLEN
MILLER
SMITH
ADAMS
JAMES

===========================================================================

P a g e | 21 Thursday, 25 November 2021

You might also like