![]() |
VOOZH | about |
SQL Joins are used to combine data from two or more tables based on a related column. They help in:
SQL joins are categorized into different types based on how rows from two tables are matched and combined.
INNER JOIN is used to retrieve rows where matching values exist in both tables. It helps in:
Syntax:
SELECT table1.column1,table1.column2,table2.column1,.... FROM table1 INNER JOIN
table2 ON table1.matching_column = table2.matching_column; Note: We can also write JOIN instead of INNER JOIN. JOIN is same as INNER JOIN.
Example of INNER JOIN:
Consider the two tables, Student and StudentCourse, which share a common column ROLL_NO. Using SQL JOINS, we can combine data from these tables based on their relationship, allowing us to retrieve meaningful information like student details along with their enrolled courses.
Student Table:
👁 Screenshot-2026-02-18-112538StudentCourse Table:
👁 courseThe following example demonstrates the working of the INNER JOIN clause. This query displays the names and ages of students enrolled in different courses.
Query:
SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE
FROM Student
INNER JOIN StudentCourse
ON Student.ROLL_NO = StudentCourse.ROLL_NO;Output:
👁 Screenshot-2026-06-12-111015LEFT JOIN is used to retrieve all rows from the left table and matching rows from the right table. It helps in:
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;Note: We can also use LEFT OUTER JOIN instead of LEFT JOIN, both are the same.
Example: In this example, the LEFT JOIN retrieves all rows from the Student table and the matching rows from the StudentCourse table based on the ROLL_NO column.
Query:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;Output:
👁 NullRIGHT JOIN is used to retrieve all rows from the right table and the matching rows from the left table. It helps in:
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;Note: We can also use RIGHT OUTER JOIN instead of RIGHT JOIN, both are the same
Example: In this example, the RIGHT JOIN retrieves all rows from the StudentCourse table and the matching rows from the Student table based on the ROLL_NO column.
Query:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;Output:
👁 Output-12FULL JOIN is used to combine the results of both LEFT JOIN and RIGHT JOIN. It helps in:
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column; Example: This example uses a FULL JOIN to return all rows from both tables. Matching records appear together, while non-matching records still show up with NULL values for the missing fields.
Query:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
FULL JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;Output :
👁 output-44Note: MySQL does not support FULL OUTER JOIN directly. It cab be simulated using UNION of LEFT JOIN and RIGHT JOIN.
A Natural Join is a type of INNER JOIN that automatically joins two tables based on columns with the same name and data type. It returns only the rows where the values in the common columns match.
Example:
Employee Table
👁 EmployeeDepartment Table
👁 Depart_manExample: Find all Employees and their respective departments.
SELECT
Emp_name,
Dept_name
FROM Employee
NATURAL JOIN Department;Output:
👁 Dept_name