Skip to main content

Joins in DBMS

DBMS Joins: Inner, THETA, Outer, Equi Types of Join Operations

Join in DBMS is a binary operation that allows you to combine join product and selection in one single statement. The goal of creating a join condition is that it helps you to combine the data from two or more DBMS tables. The tables in DBMS are associated using the primary key and foreign keys.

Types of Join

There are mainly two types of joins in DBMS:
  1. Inner Joins: Theta, Natural, EQUI
  2. Outer Join: Left, Right, Full

Inner Join

Inner Join is used to return rows from both tables which satisfy the given condition. It is the most widely used join operation and can be considered as a default join-type

An Inner join or equijoin is a comparator-based join which uses equality comparisons in the join-predicate. However, if you use other comparison operators like “>” it can’t be called equijoin.

Inner Join further divided into three subtypes:
  • Theta join
  • Natural join
  • EQUI join

Theta Join

Theta Join allows you to merge two tables based on the condition represented by theta. Theta joins work for all comparison operators. It is denoted by symbol θ. The general case of JOIN operation is called a Theta join.

Syntax:
A ⋈θ B

Theta join can use any conditions in the selection criteria.

Consider the following tables.

Table A

 

Table B

column 1

column 2

 

column 1

column 2

1

1

 

1

1

1

2

 

1

3


For example:

A  A.column 2 >  B.column 2 (B)

A  A.column 2 > B.column 2 (B)

column 1

column 2

1

2



EQUI Join

EQUI Join is done when a Theta join uses only the equivalence condition. EQUI join is the most difficult operation to implement efficiently in an RDBMS, and one reason why RDBMS have essential performance problems.

For example:

A  A.column 2 =  B.column 2 (B)

A  A.column 2 = B.column 2 (B)

column 1

column 2

1

1



Natural Join (⋈)

Natural Join does not utilize any of the comparison operators. In this type of join, the attributes should have the same name and domain. In Natural Join, there should be at least one common attribute between two relations.

It performs selection forming equality on those attributes which appear in both relations and eliminates the duplicate attributes.

Example:
Consider the following two tables

C

Num

Square

2

4

3

9

 

D

Num

Cube

2

8

3

18

C  D

C  D

Num

Square

Cube

2

4

8

3

9

18



Outer Join

An Outer Join doesn’t require each record in the two join tables to have a matching record. In this type of join, the table retains each record even if no other matching record exists.

Three types of Outer Joins are:
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join

Left Outer Join (A B)

Left Outer Join returns all the rows from the table on the left even if no matching rows have been found in the table on the right. When no matching record is found in the table on the right, NULL is returned.



Consider the following 2 Tables

A

Num

Square

2

4

3

9

4

16

 

B

Num

Cube

2

8

3

18

5

75

A  B

A  B

Num

Square

Cube

2

4

8

3

9

18

4

16



Right Outer Join ( A B )

Right Outer Join returns all the columns from the table on the right even if no matching rows have been found in the table on the left. Where no matches have been found in the table on the left, NULL is returned. RIGHT outer JOIN is the opposite of LEFT JOIN

In our example, let’s assume that you need to get the names of members and movies rented by them. Now we have a new member who has not rented any movies yet.




A  B

A  B

Num

Cube

Square

2

8

4

3

18

9

5

75


Full Outer Join ( A B)

In a Full Outer Join, all tuples from both relations are included in the result, irrespective of the matching condition.

Example:

A  B

A  B

Num

Square

Cube

2

4

8

3

9

18

4

16

5

75


Comments

Popular posts from this blog

Languages in DBMS

Languages in DBMS Structured Query Language(SQL) as we all know is the database language by the use of which we can perform certain operations on the existing database and also we can use this language to create a database. SQL uses certain commands like Create, Drop, Insert, etc. to carry out the required tasks. These SQL commands are mainly categorized into five categories as: DDL – Data Definition Language DQL – Data Query Language DML – Data Manipulation Language DCL – Data Control Language TCL – Transaction Control Language Now, we will see all of these in detail. DDL (Data Definition Language): DDL or Data Definition Language actually consists of the SQL commands that can be used to define the database schema. It simply deals with descriptions of the database schema and is used to create and modify the structure of database objects in the database. DDL is a set of SQL commands used to create, modify, and delete database structures but not data. These commands are normally ...

Indexing in DBMS

Indexing in DBMS Indexing is used to optimize the performance of a database by minimizing the number of disk accesses required when a query is processed.  The index is a type of data structure. It is used to locate and access the data in a database table quickly. Index structure: Indexes can be created using some database columns. The first column of the database is the search key that contains a copy of the primary key or candidate key of the table. The values of the primary key are stored in sorted order so that the corresponding data can be accessed easily.  The second column of the database is the data reference. It contains a set of pointers holding the address of the disk block where the value of the particular key can be found. Indexing Methods Ordered indices The indices are usually sorted to make searching faster. The indices which are sorted are known as ordered indices. Example: Suppose we have an employee table with thousands of record and each of which is 10 byte...

SQL Injection

SQL Injection The SQL Injection is a code penetration technique that might cause loss to our database. It is one of the most practiced web hacking techniques to place malicious code in SQL statements, via webpage input. SQL injection can be used to manipulate the application's web server by malicious users. SQL injection generally occurs when we ask a user to input their username/userID. Instead of a name or ID, the user gives us an SQL statement that we will unknowingly run on our database. For Example - we create a SELECT statement by adding a variable "demoUserID" to select a string. The variable will be fetched from user input (getRequestString). demoUserI = getrequestString("UserId"); demoSQL = "SELECT * FROM users WHERE UserId =" +demoUserId; Types of SQL injection attacks SQL injections can do more harm other than passing the login algorithms. Some of the SQL injection attacks include: Updating, deleting, and inserting the data: An attack can mo...