![]() |
VOOZH | about |
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 ⨝.
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.
Temp <- ⨝(student) S.roll=E.roll(Exam)
where S and E are aliases of the student and exam respectively.
Consider the two tables below as follows:
Both these tables are connected by one common key (column) i.e. ROLL_NO.
We can perform a JOIN operation using the given relational algebra:
Student ⨝ Student_course
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 |
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:
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.
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
R | S |
|---|---|
10 | 5 |
7 | 20 |
Table B
T | U |
|---|---|
10 | 12 |
17 | 6 |
A ⨝ S<T B
Output
| R | S | T | U |
|---|---|---|---|
| 10 | 5 | 10 | 12 |
| 10 | 5 | 17 | 6 |
Explanation: This query joins the table A, B and projects attributes R, S, T, U were the condition S < T is satisfied.
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
Column A | Column B |
|---|---|
a | a |
a | b |
Table C
Column A | Column B |
|---|---|
a | a |
a | c |
A ⨝ A.Column B = C.Column B (C)
Output
Column A | Column B |
a | a |
Explanation: The data value "a" is available in both tables Hence we write that "a" is the table in the given output.
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 |
Explanation - Column Number is available in both tables Hence we write the "Number column once " after combining both tables.
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
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 |
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.
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.
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 |
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.
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 |
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.