Joins in DBMS
In DBMS, a join is a query used to combine rows from two or more tables based on a related
column between them. Joins are crucial for retrieving meaningful informa on from
rela onal databases where data is stored in separate tables.
Types of Joins in DBMS
1. Inner Join
Combines rows from two tables only when there is a match in both tables based on
the specified condi on.
Syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
Example:
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
This query retrieves employees and their respec ve department names where there
is a match.
2. Le Join (Le Outer Join)
Returns all rows from the le table and the matched rows from the right table.
Unmatched rows from the right table will have NULL values.
Syntax:
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
Example:
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
This query retrieves all employees, including those who are not assigned to any
department.
3. Right Join (Right Outer Join)
Returns all rows from the right table and the matched rows from the le table.
Unmatched rows from the le table will have NULL values.
Syntax:
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
Example:
SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;
This query retrieves all departments, including those with no employees.
4. Full Join (Full Outer Join)
Combines results of both Le and Right Joins. Returns all rows from both tables, with
NULL in places where a match is not found.
Syntax:
SELECT columns
FROM table1
FULL OUTER JOIN table2
ON table1.common_column = table2.common_column;
Example:
SELECT employees.name, departments.department_name
FROM employees
FULL OUTER JOIN departments
ON employees.department_id = departments.department_id;
This query retrieves all employees and departments, including unmatched rows from
both.
5. Cross Join
Produces a Cartesian product of two tables, meaning every row in the first table is
paired with every row in the second table.
Syntax:
SELECT columns
FROM table1
CROSS JOIN table2;
Example:
SELECT employees.name, departments.department_name
FROM employees
CROSS JOIN departments;
This query pairs every employee with every department.
6. Self Join
A table is joined with itself to compare rows within the same table.
Syntax:
SELECT a.column1, b.column2
FROM table_name a, table_name b
WHERE condi on;
Example:
SELECT a.employee_id, a.name AS Employee, b.name AS Manager
FROM employees a
INNER JOIN employees b
ON a.manager_id = b.employee_id;
This query retrieves employees and their managers from the same table.
7. Natural Join
Performs an implicit join based on all columns with the same name and data type in
both tables.
Syntax:
SELECT columns
FROM table1
NATURAL JOIN table2;
Example:
SELECT name, department_name
FROM employees
NATURAL JOIN departments;
This query retrieves employees and their departments based on common columns.
Advantages of Joins
1. Efficient way to retrieve related data from mul ple tables.
2. Reduces data redundancy by avoiding the need to store all informa on in one table.
3. Offers flexibility in querying data from normalized databases.
4. Allows for complex queries to extract meaningful rela onships between tables.
Disadvantages of Joins
1. Can become complex and hard to read, especially with mul ple joins.
2. Performance can degrade with large datasets or improper indexing.
3. Cartesian product in Cross Join may result in excessive rows if not used carefully.
4. May require detailed knowledge of table rela onships to construct accurate queries.
Characteris cs of Joins
1. Operates on two or more tables based on a condi on.
2. Relies on foreign key rela onships to combine rows logically.
3. Supports different types of joins to meet diverse query requirements.
4. Works best with proper indexing to enhance query performance.
Comparison of Join Types
Join Type Matched Rows Unmatched Rows from Table1 Unmatched Rows from Table2
Inner Join Yes No No
Le Join Yes Yes No
Right Join Yes No Yes
Full Join Yes Yes Yes
Cross Join No N/A N/A
Self Join Yes N/A N/A
Natural Join Yes No No
Prac ce Queries
1. Retrieve all students and their respec ve courses (even if some students are not
enrolled in any course):
2. SELECT students.name, courses.course_name
3. FROM students
4. LEFT JOIN courses
ON students.course_id = courses.course_id;
5. Find all orders with their customer details, including orders with no customers: