×

DBMS Joins: Inner, Outer, Natural and Self Join

Joins are the combination of related tuples from the two different relations or tables into a single type. It is similar to the Cartesian product except the fact that in Cartesian product operation, a user gets all the possible combinations of relations. While in joins operation use

r gets only those combinations that satisfy some matching conditions between two relations. A relation can also join to itself, which is called as a self-join.

Join operation combines two tuples from different relations if and only if the following conditions are satisfied:

  • There must be a common attribute in both the relation.
  • Join condition must be satisfied.
Join = Cross Product + Condition

There are following different type of joins:

  1. Inner Join or Equi Join
  2. Outer Join
  3. Natural join
  4. Self-join

1. Inner Join or Equi Join

Inner Join or Equi Join is represented by (=) equal sign and displays all the records which are common between two relations. If no record is selected, return NULL. The query of Inner join compares each tuple of Relation1 with each tuple of Relation2 to find all pairs of rows which satisfy the join condition. When the join condition is satisfied, attribute values for each matched pair of tuples of A and B are combined into a resultant tuple.

DBMS Joins

Syntax:

Relation1.attribute_name = Relation2.attribute_name

Example:

Table: student

Student_id Name Age
101 RS 20
102 TK 21
103 Sk 20

Table: Course

Course_id Student_id Course_name
401 101 B.tech
402 102 MCA
403 103 MCA

Query: Display the name of student who study in each course

  • ? Name(?(student.Student_id =Course.Student_id)(student x course))

Output:

Name
RS
TK
Sk

2. Outer Join

The Outer Join displays all records from both the participating relation which satisfy the join condition along with tuples which do not satisfy the join condition.

An outer join is mainly of three types:

  1. Left Outer join
  2. Right Outer join
  3. Full Outer join

Left Outer Join

The left outer join is represented by (?) symbol and displays all the tuples from the left relation and the matching tuples from the right relation. If there is exists no matching record in the right relation, it displays the NULL value.

DBMS Joins 1

Syntax:

Relation1 ? Relation2

Example:

Table: student

Student_id Name Age
101 RS 20
102 TK 21
103 Sk 20
104 Pk 21

Table: Course

Course_id Student_id Course_name
401 101 B.tech
402 102 MCA
403 103 MCA
  • (Student ? Course)

Output:

Student_id Name Age Course_id Course_name
101 RS 20 401 B.tech
102 TK 21 402 MCA
103 Sk 20 403 MCA
104 Pk 21 NULL NULL

Right Outer Join

The right outer join is represented by (?) symbol and displays all the tuples from the right relation and the matching tuples from the left relation. If there is exists no matching record in the left relation, it displays the NULL value.

DBMS Joins 2

Syntax:

Relation1 ?Relation2

Example:

Table: student

Student_id Name Age
101 RS 20
102 TK 21
103 Sk 20

Table: Course

Course_id Student_id Course_name
401 101 B.tech
402 102 MCA
403 103 MCA
404   MBA
  • (student ? Course)

Output:

Student_id Name Age Course_id Course_name
101 RS 20 401 B.tech
102 TK 21 402 MCA
103 Sk 20 403 MCA
NULL NULL NULL 404 MBA

Full Outer Join

The full outer join is represented by(?) symbol and combines the results of both right and left outer joins and returns all (matching or unmatching) records from both the relation.

DBMS Joins 3

Syntax:

Relation1 ? Relation2

Example:

Table: student

Student_id Name Age
101 RS 20
102 TK 21
103 Sk 20

Table: Course

Course_id Student_id Course_name
401 101 B.tech
402 102 MCA
404   MBA
  • (student ? Course)

Output:

Student_id Name Age Course_id Course_name
101 RS 20 401 B.tech
102 TK 21 402 MCA
103 Sk 20 NULL NULL
NULL NULL NULL 404 MBA

3. Natural Join

Natural Join is represented by a (?) symbol, and it is a type of Inner join which is based on attributes having the same name and datatype present in both the relations to get joined.

Difference between Inner join and Natural join

  • Inan inner join, a user has to specify a join condition to join the two relations. Whereas in the natural join, a user doesn't specify a join condition. Users just write the two relation names without any condition. Then the natural join will automatically check for equality between the records for every column existing in both relation.
DBMS Joins 4

Syntax:

Relation1 ?Relation2

Example:

Table: student

Student_id Name Age
101 RS 20
102 TK 21
103 Sk 20

Table: Course

Course_id Student_id Course_name
401 101 B.tech
402 102 MCA
  • (student ? Course)

Output:

Student_id Name Age Course_id Course_name
101 RS 20 401 B.tech
102 TK 21 402 MCA

4. Self-Join

A self-join is a join in which a relation is joined with itself (which is also known as Unary relationship), especially when the relation has a foreign key which references to its own primary key. To join a relation to itself means that each tuple of the relation is combined with itself and with every other tuple of the relation. It can be viewed as a join of two copies of the same relation.

DBMS Joins 5

Example:

Table: student

student_id Name Course Class_representative
101 RS MCA 101
102 TK MBA NULL
103 Sk MCA 101
104 Pk MBA NULL

Query: Display the name of student who are class representative

  • ? a.student_id, a.Name,b.Course, b.Name(? a.student_id = b.Class_representative) (a.student x b.student))

Output:

student_id Name Course name
101 RS MCA RS
101 RS MCA SK


Related Topics

ER Diagram of Company Database in DBMS

Entity Relationship Diagram Entity Relationship Diagrams, or ER Diagrams for short, are diagrams that show the relationships among entity sets that are stored in databases. Alternatively said, ER diagrams assist in...

3 minutes read.

File Organization in DBMS

File Organization in DBMS: A database contains a huge amount of data, which is stored is in the physical memory in the form of files. A file is a set...

9 minutes read.

Meta Data in DBMS

Metadata: Metadata is simply described as information about information. It indicates that the data is described as well as its context. It makes data easier to find, interpret, and organize. I'll...

4 minutes read.

Primary Key in DBMS

Primary Key in DBMS: A primary key is the minimal set of columns in the database table, which uniquely identifies each row or tuple in that table. Any table in...

2 minutes read.

Components of an ER Diagram

An ER Diagram consists of the following components: Entity AttributesRelationships 1. Entity An entity may be an object, place, person, or an event which stores data in the database. In an entity-relationship diagram, an entity...

4 minutes read.

Difference between Relational and Non-Relational Databases

Databases have become an integral part of our lives, from powering big websites like Amazon and Netflix to everyday tasks like remembering our passwords. With such an indispensable role in...

7 minutes read.

Domain in DBMS

Constraints: Constraints in DBMS are a set of guidelines that guarantee that authorized users who modify the database do not alter the consistency of the data. Constraints are expressed in DDL...

3 minutes read.

Types of DBMS

Database Management System A software that is programmed to enable the user to create and maintain a database. Using DBMS, we can implement create, read, update and delete operations on the...

4 minutes read.

States of Transaction in DBMS

Transaction States A transaction passes through many different states in its life cycle. These states are known as transaction states. A transaction can be in one of the following states in the database: Active statePartially...

3 minutes read.

Advantages of B-Tree in DBMS

A self-balancing search tree is the B-Tree. It is assumed that everything is in the main memory in most other self-balancing search trees (such as AVL and Red-Black Trees). We must...

3 minutes read.

Red-Black Tree Properties in DBMS

What is a Red-Black Tree? A red-black tree is a kind of self-balancing binary search tree. The extra bit that each node keeps denoting "color"-either "red" or "black"-is needed to keep...

3 minutes read.

Checkpoint in DBMS

A checkpoint in DBMS is used to define a point of the transaction that is in a consistent state and then all the transactions will be in the committed state...

4 minutes read.

Specialization in DBMS

Specialization in DBMS In the database management system, specialization breaks the higher-level entity into two or more than two lower entities. The main motive of this concept is to share the...

2 minutes read.

ER Diagram for Banking System in DBMS

Entity Relationship Diagram Entity Relationship Diagrams, or ER Diagrams for short, are diagrams that show the relationships among entity sets that are stored in databases. Alternatively said, ER diagrams assist in...

3 minutes read.

Red-Black Tree Visualization in DBMS

Introduction to Red Black Tree A red-black tree is a self-balanced binary search tree with an extra bit per node, usually read as a color (red or black). The tree is...

4 minutes read.

B Tree in DBMS

How to perform Insertion operation in B Tree A new value is inserted at the leaf node. Same like in a binary search tree we traverse from starting root node to...

10 minutes read.

Serializability in DBMS

Serializability in DBMS Serializability is the concept in a transaction that helps to identify which non-serial schedule is correct and will maintain the database consistency. It relates to the isolation property of transaction in...

3 minutes read.

DBMS Schedule

A schedule is a process of combining the multiple transactions into one and executing the operations of these transactions in a predefined order. A schedule can have multiple transactions in it, each transaction...

2 minutes read.

Database for library management system

The database can be considered the container where the data or information can be stored electronically in a computer system. Most mobile applications and websites run our day-to-day activities by...

5 minutes read.

Constraints in Database Management System

In a database management system, certain constraints are applied while performing any operation on the database to ensure that the integrity and quality of the data are maintained throughout the...

4 minutes read.