Professional Documents
Culture Documents
JOINS:-Displaying Data From Multiple Tables
JOINS:-Displaying Data From Multiple Tables
JOINS:-Displaying Data From Multiple Tables
HARSH - ANGANA
Presentation Outline
What are Joins
•It joins tables even if datatypes of columns don’t match but the column names
need to be same
select name,dname from employee e JOIN department d
using(dept_id);
NOTE:-
1.We don’t use alias name in reference column , also in other
columns
2.IT shows records only if the column given in using clause is
present in both the tables
Output for joins by USING CLAUSE
Employee table The output for USING CLAUSE:
Dept_id Name Name Dname
112 Asha Asha Lotus
Chandni Lotus
111 Chandni
Sandhya sunflower
114 sandhya
select name,dname from employee e
Department table JOIN department d using(dept_id);
Dept_id Dname Note:- DEPT_id has decimal data type in
112 Lotus employee table and it has number
datatype in department table
113 Rose Also to join the column name need to be
111 Lotus same
114 Sunflower
Creating joins using on clause
• When we want to specify specific columns to join we use ON CLAUSE
• SQL examines both tables specified for the join to retrieve data from all the
rows that meet the search condition for the join.
SELECT e.name,e.dept_id,d.dname
from employee e INNER JOIN department d
on e.dept_id=d.dept_id;
NOTE: This query works same as the right outer join query given below:
If we dont give outer keyword then also it works
NOTE : the join will depend on the order of the two table specified in the query.
If employee table is in the right side of the keyword RIGHT OUTER JOIN
then it will be right outer join on the emplyoee table.
Output for RIGHT outer join
dept_id Dname Mgrid eid Name Dept_id Dname
10 Admin 1700 100 Harsh 90 Executive
20 Mrkting 1800 101 Aditya 90 Executive
50 Shipping 1500 202 Angana 20 Mrkting
60 IT 1400 205 Pankaj 110 Executive
80 Sales 2500 206 Ankur 110 designing
90 Executive 1700
Dname Dept_id Name
110 Accounting 1700 Mrkting 20 Angana
190 Designing 1700 Executive 90 Aditya
Executive 90 Harsh
Accounting 110 Pankaj
The output of given right outer join is : Accounting 110 Ankur
Shipping
Shipping 50
IT
Admin
Designing
sales
Left outer joins
The Left outer join returns all rows in the dept table which is the left table
even if there is no match in the emp table
SELECT e.name.d.dept_id from departments d left outer joins
employee e on(e.dept_id = d.dept_id);
NOTE: This query works same as the left outer join query given below:
If we dont give outer keyword then also it works
Select e.dept_id,e.name from department d,employee e
where d.dept_id =e.dept_id(+);
• It includes all the rows from both of the tables or result sets participating in
the JOIN.