Join Operations
Join Operations
Join Operations
A Join operation combines related tuples from different relations, if and only if a given join condition is
satisfied. It is denoted by ⋈.
In a relational database management system (RDBMS), the join operation combines rows from two or more
tables based on a related column between them
Example:
EMPLOYEE
EMP_CODE EMP_NAME
101 Stephan
102 Jack
103 Harry
SALARY
EMP_CODE SALARY
101 50000
102 30000
103 25000
2 Murali 22
3 Divyansh 19
Courses Table:
101 1 Math
102 2 Science
103 4 History
SQL Query:
SELECT Students.name, Courses.course_name FROM Students INNER JOIN Courses ON Students.student_id =
Courses.student_id;
Output:
name course_name
Neha Math
Murali Science
In this example, we get only the students who have enrolled in courses. Students without courses are not
included.
Theta Join
• Theta join is more flexible than the inner join. It allows us to join tables based on any condition, not
just equality.
• We can use any comparison operator such as >, <, >=, <=, or !=.
Example:
Consider two tables, Employees and Departments:
Employees Table:
1 Divyansh 50000 10
2 Krish 60000 20
3 Neha 55000 30
Departments Table:
10 HR 45000
20 IT 55000
30 Sales 52000
SQL Query:
SELECT Employees.name, Departments.dept_name FROM Employees JOIN Departments
ON Employees.salary >= Departments.min_salary;
Output:
name dept_name
Divyansh HR
Krish IT
Neha Sales
Here, we get employees who meet or exceed the minimum salary requirement for their department.
Equi Join
• Equi join is a type of theta join where the condition is always based on equality. It’s similar to an inner
join but explicitly uses the equality operator.
Example:
Consider two tables, Authors and Books:
Authors Table:
author_id author_name
1 Mark Twain
2 Jane Austen
3 J.K. Rowling
Books Table:
SQL Query:
SELECT Authors.author_name, Books.title FROM Authors INNER JOIN Books ON Authors.author_id =
Books.author_id;
Output:
author_name title
In this example, we get a list of authors and their books. Only matching rows based on author_id are included.
Natural Join
• Natural join combines tables based on columns with the same name and domain. It’s like matching
puzzle pieces that fit perfectly.
Example:
Consider two tables, Employees and Departments:
Employees Table:
1 Suman 10
2 Sonali 20
3 Aditi 30
Departments Table:
dept_id dept_name
10 HR
20 IT
30 Sales
SQL Query:
SELECT Employees.name, Departments.dept_name FROM Employees NATURAL JOIN Departments;
Output:
name dept_name
Suman HR
Sonali IT
Aditi Sales
In this example, we get a list of employees with their department names. Natural join uses the dept_id
column to match rows from both tables.
2.Outer Joins
• Outer joins help us retrieve matching records from related tables and include non-matching rows.
• The outer join operation is an extension of the join operation. It is used to deal with missing
information.
Example:
EMPLOYEE
Input:
(EMPLOYEE ⋈ FACT_WORKERS)
Output:
EMP_NAME STREET CITY BRANCH SALARY
customer_id customer_name
1 Murali
2 Divyansh
3 Anupriya
Orders Table:
101 1 Laptop
102 2 Phone
103 4 Tablet
SQL Query:
SELECT Customers.customer_name, Orders.product_name FROM Customers LEFT JOIN Orders
ON Customers.customer_id = Orders.customer_id;
Output:
customer_name product_name
Murali Laptop
Divyansh Phone
Anupriya NULL
In this example, we see that Alex has no matching order, so the product name is NULL.
Right Outer Join
• The right outer join returns all rows from the right table and matching rows from the left table. Non-
matching rows in the left table are filled with NULL.
Example:
Consider two tables, Products and Suppliers:
Products Table:
product_id product_name
1 Laptop
2 Phone
3 Tablet
Suppliers Table:
101 1 Supplier A
102 2 Supplier B
103 4 Supplier C
SQL Query:
SELECT Products.product_name, Suppliers.supplier_name FROM Products RIGHT JOIN Suppliers
ON Products.product_id = Suppliers.product_id;
Output:
product_name supplier_name
Laptop Supplier A
Phone Supplier B
NULL Supplier C
Here, we see that Supplier C has no matching product, so the product name is NULL.
Full outer join:
• Full outer join is like a left or right join except that it contains all rows from both tables.
• In full outer join, tuples in R that have no matching tuples in S and tuples in S that have no matching
tuples in R in their common attribute name.
• It is denoted by ⟗.
Example: Using the above EMPLOYEE table and FACT_WORKERS table
FACT_WORKERS