×

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

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.

Trigger in DBMS

Trigger in DBMS: A trigger is a procedure of SQL statements, which is automatically fired when the DML statements are executed on the table of the database. Triggers are the...

3 minutes read.

Foreign key in DBMS

Foreign key in DBMS: The Foreign key is a field or the set of fields in the relational database table, which points to the existing field in another table. It...

2 minutes read.

DBMS Schema

Schema It is a physical representation of data that appears in the database management system. In simple words, a schema is the structure of any database.It defines how the data is stored...

2 minutes read.

Defference between Database and Data Warehouse

Database A database is a collection of related information is called a database. It has some elements that can be mapped to real-world objects. It is designed in such a manner...

4 minutes read.

Fragmentation in Distributed DBMS

Fragmentation is a course of isolating the entire or full information base into different sub tables or sub relations with the goal that information can be put away in various...

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.

Fundamentals Of Distributed DBMS

Fundamentals of DDBMS Here we are going to talk about the Fundamentals of DDBMS. But first, we need to know about Distributed databases to understand the Distributed Database Management System. Distributed Database: It is...

4 minutes read.

DBMS Generalization, Specialization, and Aggregation

DBMS Generalization Generalization is a bottom-up approach in which the common attributes of two or more lower-level entities combines to form a new higher-level entity. In generalization, the generalized entity of higher level can also...

3 minutes read.

Relational Integrity Constraints

Relational Integrity Constraints are the set of rules that can be used to maintain the data integrity during an insert, delete and update operations into a table. These constraints are checked in the...

2 minutes read.

Three Schema Architecture of DBMS

The three schema architecture describes how the data is represented or viewed by the user in the database. This architecture is also known as three-level architecture and is sometimes called...

3 minutes read.

DBMS Data Independence: Logical and Physical

Data Independence in DBMS: Data independence is a concept of DBMS which alters the schema of the database at one level of the database system without altering the schema definition...

2 minutes read.

Applications of DBMS

There are various fields where a database management system is used. Following are some applications which make use of the database management system: 1. Railway Reservation System: In the railway reservation...

7 minutes read.

Relational Algebra in DBMS

Relational Algebra is a widely used procedural query language, which takes instances of one or more relation as an input and generates a new relation as an output. It uses a different...

6 minutes read.

Candidate Key in DBMS

Candidate key in DBMS: The candidate key is a single column or the set of columns that uniquely identifies the rows of data in the database table. It is a...

2 minutes read.

Why we need Distributed Database Management System

Assuming there is a very strong single machine having properties like: Heaps of memory.Gigantic measure of dependable stockpiling with very quick I/O.Incredible handling rate and processing power (10s, 100s and might...

4 minutes read.

Multivalued Dependency

Multivalued Dependency exists in a relation when two attributes depend on the third attribute but independent to each other. It is a full constraint between two sets of attributes in a relation. It...

2 minutes read.

DBMS View: Read, Update, Create and Drop

View in DBMS: The View is a logical or virtual table that allows users to view or manipulate parts of the table. View is also a table which consists of...

2 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.

ER Diagram for Student Management System in DBMS

Entity Relationship diagrams 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...

6 minutes read.