A SQL Join statement joins data or rows from two or more tables together based on a common field.
The following are examples of different types of joins:
🚀 INNER JOIN
🚀 LEFT JOIN
🚀 RIGHT JOIN
🚀 FULL JOIN
The INNER JOIN keyword selects records from both tables that have matching values.
SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
Let's understand this by taking one example,
SELECT Student.Name , Student.Age, Course.Course_Id FROM Student INNER JOIN Course ON Student.Roll_no = Course.Roll_no;
LEFT JOIN returns all rows from the table on the left side of the join, as well as matching rows from the table on the right. The result-set will include null for the rows for which there is no matching row on the right side. LEFT OUTER JOIN is another name for LEFT JOIN.
SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
Let's understand this by taking one example,
SELECT Student.Name , Student.Age, Course.Course_Id FROM Student INNER JOIN Course ON Student.Roll_no = Course.Roll_no;
The RIGHT JOIN function is analogous to the LEFT JOIN function. This join retrieves all rows from the table on the right side of the join, as well as matching rows from the table on the left. The result-set will include null for the rows for which there is no matching row on the left side. RIGHT OUTER JOIN is another name for RIGHT JOIN.
SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
Let's understand this by taking one example,
SELECT Student.Name , Student.Age, Course.Course_Id FROM Student RIGHT JOIN Course ON Student.Roll_no = Course.Roll_no;
The result-set of FULL JOIN is created by combining the results of both LEFT JOIN and RIGHT JOIN. All of the rows from both tables will be included in the result-set. The result-set will contain NULL values for the rows for which there is no match.
SELECT column_name(s) FROM table1 FULL JOIN table2 ON table1.column_name = table2.column_name;
Let's understand this by taking one example,
SELECT Student.Name , Student.Age, Course.Course_Id FROM Student FULL JOIN Course ON Student.Roll_no = Course.Roll_no;