Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

I18303 -RELATIONAL DATABASE

MANAGEMENT SYSTEM
UNIT – 3
JOINS
Joins
• An SQL join clause combines records from two or more tables in a
database.

• It creates a set that can be saved as a table or used as it is.

• A JOIN is a means for combining fields from two tables by using


values common to each.
Types of Joins

Inner Join
Left Outer Join
Joins

Outer Join Right Outer Join

Full Outer Join


Sample Tables
• In the following tables the DepartmentID column of the Department table (which can be
designated as Department.DepartmentID) is the primary key, while Employee.DepartmentID is a
foreign key.
Employee Table Department Table
LastName DepartmentID DepartmentID DepartmentName
Jaffer 31
31 Sales
Jones 33
Nithin 33 33 Engineering
Robin 34 34 Clerical
Smith 34 35 Marketing
John NULL

• In the Employee table above, the employee "John" has not been assigned to any department yet.
• Also, no employees are assigned to the "Marketing" department.
Inner Join
• Inner join creates a new result table by combining column values of two tables
based upon the join-predicate.
• The query compares each row of table A with each row of table B to find all pairs
of rows which satisfy the join-predicate.
Explicit Join Notation
SELECT * FROM employee
INNER JOIN department
ON employee.DepartmentID = department.DepartmentID;

Implicit Join Notation


SELECT * FROM employee, department
WHERE employee.DepartmentID = department.DepartmentID;
Inner Join - Output

Employee. Employee. Department. Department.


LastName DepartmentID DepartmentName DepartmentID
Robin 34 Clerical 34
Jones 33 Engineering 33
Smith 34 Clerical 34
Nithin 33 Engineering 33
Jaffer 31 Sales 31
Left Outer Join
• A left outer join returns all the values from an inner join plus all values in the left
table that do not match to the right table.
Explicit Join Notation
SELECT * FROM employee
LEFT OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID;

Implicit Join Notation


SELECT * FROM employee, department
WHERE employee.DepartmentID = department.DepartmentID(+)
Left Outer Join - Output

Employee. Employee. Department. Department.


LastName DepartmentID DepartmentName DepartmentID
Jones 33 Engineering 33
Jaffer 31 Sales 31
Robin 34 Clerical 34
Smith 34 Clerical 34
John NULL NULL NULL
Nithin 33 Engineering 33
Right Outer Join
• A right outer join returns all the values from an inner join plus all values in the
right table that do not match to the left table.

Explicit Join Notation


SELECT * FROM employee
RIGHT OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID;

Implicit Join Notation


SELECT * FROM employee, department
WHERE employee.DepartmentID(+) = department.DepartmentID
Right Outer Join - Output

Employee. Employee. Department. Department.


LastName DepartmentID DepartmentName DepartmentID
Smith 34 Clerical 34
Jones 33 Engineering 33
Robin 34 Clerical 34
Nithin 33 Engineering 33
Jaffer 31 Sales 31
NULL NULL Marketing 35
Full Outer Join
• A full outer join combines the effect of applying both left and right outer joins.
SELECT * FROM employee FULL OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID;

Employee. Employee. Department. Department.


LastName DepartmentID DepartmentName DepartmentID
Smith 34 Clerical 34
Jones 33 Engineering 33
Robinson 34 Clerical 34
John NULL NULL NULL
Steinberg 33 Engineering 33
Rafferty 31 Sales 31
NULL NULL Marketing 35

You might also like