The Wayback Machine - https://web.archive.org/web/20240623051201/https://www.geeksforgeeks.org/joins-in-dbms/
Open In App

Joins in DBMS

Last Updated : 13 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A join is an operation that combines the rows of two or more tables based on related columns. This operation is used for retrieving the data from multiple tables simultaneously using common columns of tables. In this article, we are going to discuss every point about joins.

What is Join?

Join is an operation in DBMS(Database Management System) that combines the row of two or more tables based on related columns between them. The main purpose of Join is to retrieve the data from multiple tables in other words Join is used to perform multi-table queries. It is denoted by ⨝.

Syntax 1

R3 <- ⨝(R1) <join_condition> (R2)

where R1 and R2 are two relations to be joined and R3 is a relation that will hold the result of the join operation.

Example

Temp <- ⨝(student) S.roll==E.roll(Exam)

where S and E are aliases of the student and exam respectively

SQL JOIN Example

Consider the two tables below as follows:

Student

Table 1 – Student

Student Course

Table 2 – StudentCourse

Both these tables are connected by one common key (column) i.e. ROLL_NO.

We can perform a JOIN operation using the given SQL query:

SELECT s.roll_no, s.name, s.address, s.phone, s.age, sc.course_id
FROM Student s
JOIN StudentCourse sc ON s.roll_no = sc.roll_no;

Output:

ROLL_NO

NAME

ADDRESS

PHONE

AGE

COURSE_ID

1

HARSH

DELHI

xxxxxxxxxx

18

1

2

PRATIK

BIHAR

xxxxxxxxxx

19

2

3

PRIYANKA

SILIGURI

xxxxxxxxxx

20

2

4

DEEp

RAMNAGAR

xxxxxxxxxx

18

3

5

SAPTARHI

KOLKATA

xxxxxxxxxx

19

1

Types of Join

 There are many types of Joins in SQL. Depending on the use case, you can use different types of SQL JOIN clauses. Here are the frequently used SQL JOIN types:

1. Inner Join

Inner Join is a join operation in DBMS that combines two or more tables based on related columns and returns only rows that have matching values among tables. Inner join of two types.

Inner Join

Inner Join

  • Equi Join
  • Natural Join

(a) Equi Join

Equi Join is a type of Inner join in which we use equivalence(‘=’) condition in the join condition

Example: Suppose there are two tables Table A and Table B

Table A

Column A

Column B

a

a

a

b

Table B

Column A

Column B

a

a

a

c

A ⨝ A.Column B = B.Column B (B)

Output

Column A

Column B

a

a

SQL Query

SELECT * FROM TableA NATURAL JOIN TableB;

Explanation – The data value “a” is available in both tables Hence we write that “a” is the table in the given output.

(b) Natural Join

Natural join is a type of inner join in which we do not need any comparison operators. In natural join, columns should have the same name and domain. There should be at least one common attribute between the two tables.

Example: Suppose there are two tables Table A and Table B

Table A

Number

Square

2

4

3

9

Table B

Number

Cube

2

8

3

27

A ⨝ B

Output

Number

Square

Cube

2

4

8

3

9

27

SQL Query

SELECT * FROM TableA NATURAL JOIN TableB;

Explanation – Column Number is available in both tables Hence we write the “Number column once ” after combining both tables.

2. Outer Join

Outer join is a type of join that retrieves matching as well as non-matching records from related tables. These three types of outer join

  • Left outer join
  • Right outer join
  • Full outer join

(a) Left Outer Join

It is also called left join. This type of outer join retrieves all records from the left table and retrieves matching records from the right table.

Example: Suppose there are two tables Table A and Table B

Table A

Number

Square

2

4

3

9

4

16

Table B

Number

Cube

2

8

3

27

5

125

A ⟕ B

Output

Number

Square

Cube

2

4

8

3

9

27

4

16

NULL

SQL Query-

SELECT * FROM TableA LEFT OUTER JOIN TableB ON TableA.Number = TableB.Number;

Explanation: Since we know in the left outer join we take all the columns from the left table (Here Table A) In the table A we can see that there is no Cube value for number 4. so we mark this as NULL.

(b) Right Outer Join

It is also called a right join. This type of outer join retrieves all records from the right table and retrieves matching records from the left table. And for the record which doesn’t lies in Left table will be marked as NULL in result Set.

Right outer join

Right Outer Join

Example: Suppose there are two tables Table A and Table B

A ⟖ B

Output:

Number

Square

Cube

2

4

8

3

9

27

5

NULL

125

SQL Query

SELECT * FROM TableA RIGHT OUTER JOIN TableB ON TableA.Number= TableB.Number;

Explanation: Since we know in the right outer join we take all the columns from the right table (Here Table B) In table A we can see that there is no square value for number 5. So we mark this as NULL.

(c) Full Outer Join

FULL JOIN creates the result set by combining the results of both LEFT JOIN and RIGHT JOIN. The result set will contain all the rows from both tables. For the rows for which there is no matching, the result set will contain NULL values.

Example: Table A and Table B are the same as in the left outer join

A ⟗ B

Output:

Number

Square

Cube

2

4

8

3

9

27

4

16

NULL

5

NULL

125

SQL Query

SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.Number= TableB.Number;

Explanation: Since we know in full outer join we take all the columns from both tables (Here Table A and Table B) In the table A and Table B we can see that there is no Cube value for number 4 and No Square value for 5 so we mark this as NULL.

Conclusion

In the field of Database Management Systems, Join is a very important tool for retrieving data from multiple tables simultaneously. Learning and understanding join syntax and types of syntax including inner and outer joins is very efficient for writing queries for databases and extracting valuable understanding.

Frequently Asked Questions on Joins – FAQs

What is the primary purpose of a join operation in DBMS?

The Primary purpose of join operation in DBMS is to two or more tables based on related columns , enabling the retrival of data from multiple tables in single query.

What is the difference between an inner join and an outer join?

An inner join returns the only matching values that available in both tables while outer join returns all the data which is maching as well as non-matching values from the both tables.

When is an Equi Join used in a database query?

An Equi Join is a type of inner join that uses equivalence (usually ‘=’) conditions in the join condition. It is used when you want to combine rows where specific columns have equal values.

What is a Natural Join, and when is it used?

A Natural Join is a type of inner join where no comparison operators are needed. It is used when columns in the two tables have the same name and domain, and there is at least one common attribute between them.

In a full outer join, what does the result table include?

In a full outer join, the result table includes all the rows from both tables, whether they have matching values or not.



Similar Reads

Joins in MS SQL Server
A database comprises tables and each table in case of RDBMS is called a relation. Let us consider a sample database named University and it has two tables named Student and Marks. If a user wants to transfer a certain set of rows, insert into select statement is used along with the query. But if a user wants a whole set of rows from both tables or
2 min read
MariaDB Joins
MariaDB's ability to handle a variety of join types is one of its primary characteristics that makes it an effective tool for managing relational databases. Joins let you describe relationships between tables so you may access data from several tables. We'll go into the details of MariaDB joins in this post, looking at their kinds, syntax, and prac
6 min read
SQLite Joins
SQLite is a server-less database engine and it is written in C programming language. It is developed by D. Richard Hipp in the year 2000. The main motive for developing SQLite is to overcome the use of complex database engines like MySQL etc. It has become one of the most popularly used database engines used in Television, Mobile Phones, web browse
5 min read
Explicit vs Implicit MySQL Joins
MySQL joins are used to combine rows from two or more tables based on a related column between them. MySQL, a popular relational database management system, offers two main approaches to perform joins: explicit and implicit. In this article, we will explore these two methodologies, understanding their syntax, use cases, and the implications for cod
8 min read
SQL Joins (Inner, Left, Right and Full Join)
SQL Join operation combines data or rows from two or more tables based on a common field between them. In this article, we will learn about Joins in SQL, covering JOIN types, syntax, and examples. SQL JOINSQL JOIN clause is used to query and access data from multiple tables by establishing logical relationships between them. It can access data from
5 min read
Introduction of Relational Algebra in DBMS
Pre-Requisite: Relational Model in DBMS Relational Algebra is a procedural query language. Relational algebra mainly provides a theoretical foundation for relational databases and SQL. The main purpose of using Relational Algebra is to define operators that transform one or more input relations into an output relation. Given that these operators ac
4 min read
Need for DBMS
A Data Base Management System is a system software for easy, efficient and reliable data processing and management. It can be used for: Creation of a database.Retrieval of information from the database.Updating the database.Managing a database.Multiple User InterfaceData scalability, expandability and flexibility: We can change schema of the databa
4 min read
Introduction of Relational Model and Codd Rules in DBMS
Pre-Requisite: Relational Model in DBMS It was proposed by Dr. E.F. Codd. It uses the concept of relations to represent each and every file. Relations are Two-Dimensional Tables. It is easy to implement and easy to simplification in the operations to manipulate the data. This is the most popular data model. It is simple to implement. It uses the pr
12 min read
Thomas Write Rule in DBMS
The "write rule" in database management systems (DBMS) refers to a principle that governs how changes to the database are handled. Specifically, the write rule states that any modification to a database must be written to disk before control is returned to the user. In other words, when a user executes a command that modifies the database (such as
5 min read
Armstrong's Axioms in Functional Dependency in DBMS
Prerequisite - Functional Dependencies This article contains Armstrong's Axioms and how Armstrong's Axioms are used to decide about the functional dependency on the database. We will be also learning about the Secondary Rules and Armstrong Relations. We will learn each thing in detail. Before moving ahead, you must have a knowledge of Functional De
4 min read