SQL
MYSQL - ALTER, DROP, RENAME, MODIFY
WHAT IS THE ALTER COMMAND? As the saying goes Change is the only constant With time business...
Join in DBMS is a binary operation which 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.
In this DBMS tutorial, you will learn:
There are mainly two types of joins in DBMS:
Let's see them in detail:
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 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 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 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 |
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 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 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 |
AB
A ⋈ B | ||
Num | Square | Cube |
2 | 4 | 8 |
3 | 9 | 18 |
4 | 16 | - |
RIGHT 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 movie yet.
AB
A ⋈ B | ||
Num | Cube | Square |
2 | 8 | 4 |
3 | 18 | 9 |
5 | 75 | - |
In a FULL OUTER JOIN , all tuples from both relations are included in the result, irrespective of the matching condition.
Example:
AB
A ⋈ B | ||
Num | Square | Cube |
2 | 4 | 8 |
3 | 9 | 18 |
4 | 16 | - |
5 | - | 75 |
WHAT IS THE ALTER COMMAND? As the saying goes Change is the only constant With time business...
What is PL/SQL Datatypes? A data type is associated with the specific storage format and range...
SQL stands for Structured Query language, pronounced as "S-Q-L" or sometimes as "See-Quel." SQL is...
In SQL Null is both a value as well as a keyword. Let's look into NULL value first - Null as a...
What is BULK COLLECT? BULK COLLECT reduces context switches between SQL and PL/SQL engine and...
What is For Loop? "FOR LOOP" statement is best suitable when you want to execute a code for a...