0% found this document useful (0 votes)
39 views22 pages

Unit 2 SQL Joins

The document discusses different types of joins in SQL: 1) Inner join/equi join returns rows that have matching values between columns in both tables based on the join condition. 2) Outer joins return all rows from one table and matching rows from the other table. This includes left, right, and full outer joins. 3) Natural join returns rows from multiple tables where columns in both tables with the same name are equal. It automatically joins on all common columns.

Uploaded by

Nitin Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views22 pages

Unit 2 SQL Joins

The document discusses different types of joins in SQL: 1) Inner join/equi join returns rows that have matching values between columns in both tables based on the join condition. 2) Outer joins return all rows from one table and matching rows from the other table. This includes left, right, and full outer joins. 3) Natural join returns rows from multiple tables where columns in both tables with the same name are equal. It automatically joins on all common columns.

Uploaded by

Nitin Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

JOIN/Inner Join/EQUI JOIN

• The most important and frequently used joins is the INNER


JOIN. They are also referred to as an EQUIJOIN.
• The INNER JOIN creates a new result table by combining
column values of two tables (table1 and table2) based upon the
join-predicate.
• The query compares each row of table1 with each row of table2
to find all pairs of rows which satisfy the join-predicate.
• When the join-predicate is satisfied, column values for each
matched pair of rows of A and B are combined into a result row.
• SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS
INNER JOIN ORDERS ON CUSTOMERS.ID =
ORDERS.CUSTOMER_ID;
Inner Join/ Equi Join
Inner join includes where clause in the select statement.
It is called equi join as the equality operator(=) is used in the where
to compare two columns from two tables.
The rows that satisfies condition under where clause are selected.
Syntax:
Select table1.column, table2.column from table1, table2
where table1.column = table2.column;
Write a query to display Emp_id,Ename,Salary,Dname of all employees
whose Dept_id in Employee table is equal to Dept_id in department table.

Select Emp-id, Ename, Salary,Dname from Employee,Department where Employee.Dept_id=Department.Dept_id


select * from employee lnner join department on(employee.dept_id=department.dept_id)
Emp-id Ename Salary Dept_id Dept_id Dname
01 Finance
101 Anurag 12000 01 02 Marketing
102 Anshu 10000 01 03 Packing
103 Manav 15000 02
104 Ankur 8000

Department
Employee
Emp-id Ename Salary Dname
101 Anurag 12000 Finance
102 Anshu 10000 Finance
103 Manav 15000 Marketi
ng

(Inner join)Result table


Outer Join
Outer join returns both matching as well as non-
matching rows.
Here rows in one relation having non-matching rows
in the other relation will also appear in the resultant
tables with nulls.
Outer join is of 3 types:
Left Outer Join
Right Outer Join
Full Outer Join
SQL Left Outer Join
• It returns all the rows in the first relation (i.e written on the
left side) and if no matching rows is found in the second
relation then attributes of second relation are filled with null
values.
• The SQL LEFT JOIN returns all rows from the left table, even
if there are no matches in the right table. This means that if
the ON clause matches 0 (zero) records in the right table; the
join will still return a row in the result, but with NULL in each
column from the right table.
• This means that a left join returns all the values from the left
table, plus matched values from the right table or NULL in
case of no matching join predicate.
• SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS
LEFT JOIN ORDERS ON CUSTOMERS.ID =
ORDERS.CUSTOMER_ID;
Write a query to display all details from Employee and common details
from department table(by using left join )
Select Emp-id, Ename, Salary,Dname from Employee left outer join Department on
(Employee.Dept_id=Department.Dept_id)
select * from employee left outer join department on(employee.dept_id=department.dept_id)

Emp- Enam Salary Dept_ Dept_id Dname


id e id 01 Finance
101 Anura 12000 01 02 Marketing
g
03 Packing
102 Anshu 10000 01
103 Manav 15000 02
104 Ankur 8000
Department
Employee Emp-id Ename Salary Dname
101 Anurag 12000 Finance
102 Anshu 10000 Finance
103 Manav 15000 Marketin
g
104 Ankur 8000 Null

(Left outer Join)


Right Outer Join, Right Join
• It returns all the rows in the second relation (i.e written on the
right side) and if no matching rows is found in the first relation
then attributes of first relation are filled with null values.
• The SQL RIGHT JOIN returns all rows from the right table,
even if there are no matches in the left table. This means that if
the ON clause matches 0 (zero) records in the left table; the
join will still return a row in the result, but with NULL in each
column from the left table.
• This means that a right join returns all the values from the
right table, plus matched values from the left table or NULL in
case of no matching join predicate
• SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS
RIGHT JOIN ORDERS ON CUSTOMERS.ID =
ORDERS.CUSTOMER_ID;
Write a query to display common details from Employee and all details
from department table (by using right join)
1)Select Emp-id, Ename, Salary,Dname from Employee right outer join Department on
(Emplyee.Dept_id=Department.Dept_id)
2)select * from employee right outer join department on(employee.dept_id=department.dept_id)

Emp- Enam Salary Dept_ Dept_id Dname


id e id 01 Finance
101 Anura 12000 01 02 Marketin
g g
102 Anshu 10000 01 03 Packing
103 Manav 15000 02
104 Ankur 8000
Department
Employee
Emp-id Ename Salary Dname
101 Anurag 12000 Finance
102 Anshu 10000 Finance
103 Manav 15000 Marketin
g
Null Null Null Packing
SQL Full Outer Join
• The SQL FULL JOIN combines the results of both left
and right outer joins.
• It returns all the rows in both first and second relation
and if no matching rows are found then it fills them with
null values.
• The joined table will contain all records from both the
tables and fill in NULLs for missing matches on either
side.
• SELECT ID, NAME, AMOUNT, DATE FROM
CUSTOMERS FULL JOIN ORDERS ON CUSTOMERS.ID
= ORDERS.CUSTOMER_ID;
Write a query to display all details from Employee and from department
table(by using full outer join )
Select Emp-id, Ename, Salary,Dname from Employee full outer join Department on
(Employee.Dept_id=Department.Dept_id)
select * from employee full outer join department on(employee.dept_id=department.dept_id)

Emp- Enam Salary Dept_ Dept_id Dname


id e id 01 Finance
101 Anura 12000 01 02 Marketin
g g
102 Anshu 10000 01 03 Packing
103 Manav 15000 02
104 Ankur 8000
Department
Employee Emp-id Ename Salary Dname
101 Anurag 12000 Finance
102 Anshu 10000 Finance
103 Manav 15000 Marketin
g
104 Ankur 8000 Null
Null Null Null Packing
Natural Join
Select * from Employee Natural join Department
Emp- Enam Salary Dept_i Dept_id Dname
id e d
01 Finance
101 Anurag 12000 01
02 Marketin
g
03 Packing
102 Anshu 10000 01
103 Manav 15000 02
104 Ankur 8000
Emp-id Ename Salary Dept_i Dname
d
101 Anurag 12000 01 Finance

102 Anshu 10000 01 Finance

103 Manav 15000 02 Marketi


ng
Select * from employee,department where
employee.empid=department.dept_id
(inner join condition must specify and duplicated record exist)
SQL Cartesian or Cross Join
• The CARTESIAN JOIN or CROSS JOIN returns the
Cartesian product of the sets of records from two or
more joined tables. Thus, it equates to an inner join
where the join-condition always evaluates to either
True or where the join-condition is absent from the
statement.
• SELECT ID, NAME, AMOUNT, DATE FROM
CUSTOMERS, ORDERS;
Example
Consider the following Department and HOD tables:
Department HOD
Select Name, HOD_Name from Department, HOD;
Output:
SQL Self Join
The SQL SELF JOIN is used to join a table to itself as
if the table were two tables; temporarily renaming at
least one table in the SQL statement.
SELECT column_names FROM Table_name T1,
Table_name T2 WHERE condition ;
S_id C_id since

S1 C1 2016

S2 C2 2017

S1 C2 2017

Find student who is enrolled in at least 2 courses


S_id C_id since S_id C_id since
S1 C1 2016 S1 C1 2016
S2 C2 2017 S2 C2 2017
S1 C2 2017 S1 C2 2017

T1 T1 T2 T2
S1 C1 S1 C1
S1 C1 S2 C2
S1 C1 S1 C2
S2 C2 S1 C1
S2 C2 S2 C2
S2 C2 S1 C2
S1 C2 S1 C1
S1 C2 S2 C2
S1 C2 S1 C2
Select T1.S_id,t2.cid from study T1, study T2 where
T1.S_id=T2.S_id and T1.C_id<>T2.C_id

You might also like