Database Systems: Sonal Pandey M.E. CSE Dept. NITTTR, Chandigarh

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Database Systems

Sonal Pandey
M.E. CSE Dept.
NITTTR, Chandigarh
1

Database Management Systems Syed Sarmad Ali


Introduction

• A JOIN is a means for merging fields from two tables by using values common to each. SQL Joins
condition is a part of the sql query that fetch/retrieve rows(data) from two or more tables.

• A SQL JOIN combines records from two tables.


• A JOIN locates related column values in the two tables.
• A query can contain zero, one, or multiple JOIN operations.
• INNER JOIN is the same as JOIN; the keyword INNER is optional.
We have two tables
tbl_employee

tbl_department
The Different Types of Joins in SQL Server

1 - INNER JOIN : A Inner Join is a type of sql join which returns all the rows from both
tables where there is a match. Means return matching records for both tables.
2 - SELF JOIN : A Self Join is a type of sql join which is used to join a table to itself. In
this join both the columns belong to the same table.

SELECT Emp1.Empid, Emp1.EmpName,Emp2.Departmentid FROM tbl_employee


Emp1 INNER JOIN tbl_employee Emp2 ON Emp1.Empid=Emp2.Empid
3 - OUTER JOIN : In this join includes rows even if they don't have related rows
in the joined table. There are three different Outer Join methods..

Right outer Join : In This join returns all the rows from the right table in
conjunction with the matching rows from the left table.
Left outer Join : In This join returns all the rows from the left table
in conjunction with the matching rows from the right table.

the matching rows from the right table.


Full Outer Join : This join combines left outer join and right outer join. It
returns row from both tables wether it's match or not.
Inner join

 An inner join is the most common join operation used in


applications and can be regarded as the default join-type.
 Inner join creates a new result table by combining column values
of
two tables (A and B) based upon the join-predicate.
 The query compares each row of A with each row of B 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.
Outer Join

 An outer join does not require each record in the two joined tables
to have a matching record. The joined table retains each record—
even if no other matching record exists. Outer joins subdivide
further into left outer joins, right outer joins, and full outer joins,
depending on which table(s) one retains the rows from (left, right, or
both).
Left Outer Join

 The result of a left outer join (or simply left join) for table A and B always contains all
records of the "left" table (A), even if the join-condition does not find any matching record
in the "right" table (B).
 This means that if the ON clause matches 0 (zero) records in B, the join will still return a
row in the result—but with NULL in each column from B.
 This means that a left outer 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).
 If the right table returns one row and the left table returns more than one matching
row for it, the values in the right table will be repeated for each distinct row on the left
table.
Employee

Employee =X Dept

Dept
Left Outer Join (Example)
Right Outer Join

 A right outer join (or right join) closely resembles a left outer join,
except with the treatment of the tables reversed. Every row from the
"right" table
(B) will appear in the joined table at least once. If no matching row from the
"left" table (A) exists, NULL will appear in columns from A for those
records that have no match in B. A right outer join returns all the values
from the right table and matched values from the left table (NULL in case
of no matching join predicate).
Employee

Employee X= Dept

Dept
Full Outer Join

 Conceptually, a full outer join combines the effect of applying both


left and right outer joins. Where records in the FULL OUTER JOINed
tables do not match, the result set will have NULL values for every
column of the table that lacks a matching row. For those records
that do match, a single row will be produced in the result set
(containing fields populated from both tables).
Right Outer Join (Example)
Employee

Employee =X= Dept

Dept
Full Outer Join (Example)
Equijoin

 An equi-join, also known as an equijoin, is a specific type of


comparator-based join, or theta join, that uses
only equality comparisons in the join-predicate. Using other
comparison operators (such as <) disqualifies a join as an equi-
join.
Equijoin (Example)
THANKYOU

You might also like