JOINS:-Displaying Data From Multiple Tables

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 22

JOINS :-Displaying data from multiple tables

HARSH - ANGANA
Presentation Outline
What are Joins

• When data from more than one table in the database is


required, a join condition is used.

• Rows in one table can be joined to rows in another table


according to common values existing in corresponding
columns that is usually primary and foreign key columns
Cartesian product
• It is a common requirement for joining tables in database.

• A Cartesian product is obtained

1. when a join condition is omitted .


2. When condition is not a valid condition.

• Under such circumstances all ‘M’ rows of table 1 are


mapped to all 'N' rows of table 2 resulting in M*N number
or rows.
• To avoid cartesian product always include a valid join
condition in a where clause.
• In SQL Cartesian Products are obtained using cross joins
Sample tables
dept_id Dnmae Mgrid Department
10 Admin 1700 Table
20 Mrkting 1800
50 Shipping 1500
60 IT 1400
80 Sales 2500
90 Executive 1700
110 Accounting 1700
190 Designing 1700

eid Name Dept_id Dname Employee Table


100 Harsh 90 Executive
101 Aditya 90 Executive
202 Angana 20 Mrkting
205 Pankaj 110 Executive
206 Ankur 110 designing
Creating joins with using clause
•We use USING CLAUSE to match only column when more than one column
matches in multiple table

•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

113 Arif Arif Rose

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

select e.name,d.dept_id from employee e JOIN department d


on(d.dept_id=e.dept_id);

The above query works as equijoin mentioned below :

select e.name,d.dept_id from employee e , department d where


d.dept_id = e.dept_id;

The ouput will be same for both the tables:


OUTPUT FOR ON CLAUSE
Employee table
The ouput for join using on
Dept_id Name clause
112 Asha
Name Dname
113 Arif
Asha Lotus
111 Chandni
Arif Rose
119 kinjal
Chandni Lotus
114 sandhya
Sandhya Sunflower
Department table
The output using Equijoin
Dept_id Dname Name Dname
112 Lotus Asha Lotus
113 Rose Arif Rose
111 Lotus Chandni Lotus
114 Sunflower Sandhya Sunflower
Inner joins
• With an inner join, column values from one row of a table are combined with
column values from another row of another (or the same) table to form a
single row of data.

• 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 : Inner join works same as equi joins


OUTPUT FOR INNER JOINS
dept_id Dame 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
110 Accounting 1700
190 Designing 1700
Dname Dept_id Name
Mrkting 20 Angana
The output of given inner join is : Executive 90 Aditya
Executive 90 Harsh
Accounting 110 Ankur
Accounting 110 Pankaj
Right outer join
• This query returns all rows in the emp table which is the left table even if there
is no match in the dept table

SELECT e.name, e.dept_id, d.dname FROM employee e


RIGHT OUTER JOIN department d
ON (d.dept_id = e.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

Select e.dept_id,e.name,d.dname from emp e,dept d


where d.dept_id(+)=e.dept_id;

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(+);

Department table Employee table

Dname Dept_id Name Dept_id


Shipping 50 Harsh 90
IT 60 Aditya 90
Sales 80 Angana 20
executive 90 Pankaj 110
Ankur 110
Output for left outer join
Department table The output after using left outer
joins is as follows
Dname Dept_id
Shipping 50 Name Dept_id
Harsh 90
IT 60
Aditya 90
Sales 80
50
executive 90
60
80
Employee table
Name Dept_id SELECT e.name.d.dept_id from departments d left outer joins
employee e on(e.dept_id = d.dept_d);
Harsh 90
Aditya 90 NOTE: This query works same as the left outer join query
given below:
Angana 20
If we dont give outer keyword then also it works
Pankaj 110 Select e.dept_id,e.name from department d,employee e
Ankur 110 where d.dept_id =e.dept_id(+);
Full outer joins
• A FULL OUTER JOIN is neither "left" nor "right"— it's both!

• It includes all the rows from both of the tables or result sets participating in
the JOIN.

The query for the full outer join is as follows :

SELECT e.name, d.dept_id, d.dname


FROM employee e FULL OUTER JOIN department d
ON (e.dept_id = d.dept_id) ;
OUPUT FOR FULL 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 full outer join is : Accounting 110 Pankaj
Shipping 50 50
IT 60
Admin 10
Designing 190
sales 80
Tables for CROSS JOINs
dept_id Dnmae Mgrid Department
10 Admin 1700 Table
20 Mrkting 1800
50 Shipping 1500
60 IT 1400
80 Sales 2500
90 Executive 1700
110 Accounting 1700
190 Designing 1700

eid Name Dept_id Dname Employee Table


100 Harsh 90 Executive
101 Aditya 90 Executive
202 Angana 20 Mrkting
205 Pankaj 110 Executive
206 Ankur 110 designing
Creating cross joins
• Cross Joins are used for obtaining Cartesian Products
• The cross join produces cross product of two tables

Select * from employee, department;

The above query will total 40 rows


• The above query can also be solved by using CROSS JOIN
clause

Select dept_id,dname from department CROSS JOIN


employee;

The above query will total 40 rows


Creating Natural joins
• The NATURAL JOIN clause is based on all
columns in the two tables that have the
same name
• It selects rows from two tables that have
equal values in all matched columns

SQL> select dname,dept_id,mgrid,name from employee


natural join department;

NOTE : The dept_id occurs in both tables, still there will be


no confusion for oracle engine to execute this query
Output for natural joins
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
110 Accountin 1700
g
190 Designing 1700

Dname Dept_id Name


The output of given natural join is : Mrkting 20 Angana
Executive 90 Aditya
Executive 90 Harsh
Thank you

You might also like