Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
24 views6 pages

Joins

The document explains joins in DBMS, which are used to combine rows from multiple tables based on related columns. It details various types of joins including Inner Join, Left Join, Right Join, Full Join, Cross Join, Self Join, and Natural Join, along with their syntax and examples. Additionally, it discusses the advantages and disadvantages of using joins, their characteristics, and provides a comparison of different join types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

Joins

The document explains joins in DBMS, which are used to combine rows from multiple tables based on related columns. It details various types of joins including Inner Join, Left Join, Right Join, Full Join, Cross Join, Self Join, and Natural Join, along with their syntax and examples. Additionally, it discusses the advantages and disadvantages of using joins, their characteristics, and provides a comparison of different join types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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:

You might also like