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 rows 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:
Table 1 - Student
Table 2 - StudentCourseBoth 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 has two types.
Inner Join
- Conditional join
- Equi Join
- Natural Join
(a) Conditional Join
Conditional join or Theta join is a type of inner join in which tables are combined based on the specified condition.
In conditional join, the join condition can include <, >, <=, >=, ≠ operators in addition to the '=' operator.
Example: Suppose two tables A and B
Table A
Table B
A ⨝ S<T B
Output
SQL Query
SELECT R, S, T, U FROM Table A JOIN Table B ON S < T;
Explanation: This query joins the table A, B and projects attributes R, S, T, U were the condition S < T is satisfied.
(b) Equi Join
Equi Join is a type of inner join where the join condition uses the equality operator ('=') between columns.
Example: Suppose there are two tables Table A and Table C
Table A
Table C
A ⨝ A.Column B = C.Column B (C)
Output
SQL Query
SELECT *
FROM Table A
INNER JOIN Table C
ON A.ColumnB = C.ColumnB;
Explanation: The data value "a" is available in both tables Hence we write that "a" is the table in the given output.
(c) 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
Table B
A ⨝ B
Output
Number | Square | Cube |
---|
2 | 4 | 8 |
3 | 9 | 27 |
SQL Query
SELECT * FROM Table A NATURAL JOIN Table B;
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. There are 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
Table B
A ⟕ B
Output
Number | Square | Cube |
---|
2 | 4 | 8 |
3 | 9 | 27 |
4 | 16 | NULL |
SQL Query
SELECT * FROM Table A LEFT OUTER JOIN Table B 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 JoinExample: 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 Table A FULL OUTER JOIN Table B 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.
Similar Reads
Merge Join in DBMS Merge be part of is a hard and fast-based be part of operation used in database control systems (DBMS) to mix rows from or extra tables based on an associated column among them. It is mainly efficient whilst the tables involved are large and while they are each sorted on the be a part of the key, wh
7 min read
Nested Loop Join in DBMS The joining of tables in relational databases is a common operation aimed at merging data from many different sources. In this article, we will look into nested-loop join which is one of the basic types of joins that underlies several other join algorithms. We are going to dive deeply into the mecha
7 min read
SQL Inner Join SQL INNER JOIN is a powerful and frequently used operation in relational databases. It allows us to combine two or more tables based on a related column, returning only the records that satisfy the join conditionThis article will explore the fundamentals of INNER JOIN, its syntax, practical examples
4 min read
What is a Query in DBMS? In the field of Database Management Systems (DBMS), a query serves as a fundamental tool for retrieving, manipulating, and managing data stored within a database. Queries act as the bridge between users and databases, enabling them to communicate with the system to extract specific information or pe
5 min read
Join algorithms in Database A well-designed database aims to reduce unnecessary repetition of data. This is achieved through normalization which breaks data into smaller, more manageable tables. However, when we need to combine information from different tables, we use joins.One common type of join is the inner equijoin. This
7 min read
Conditional Join DBMS or Database Management Systems consist of data collected from various sources. Database administrators and analysts use this data to analyze the collected data. Database administrators execute the query through which some output is generated, the conditions are passed through the query. This qu
5 min read