The Wayback Machine - https://web.archive.org/web/20260128102946/https://www.geeksforgeeks.org/sql/sql-join-set-1-inner-left-right-and-full-joins/

SQL Joins (Inner, Left, Right and Full Join)

Last Updated : 21 Jan, 2026

SQL Joins are used to combine rows from two or more tables based on a related column between them. They help in retrieving connected data stored across multiple tables.

Types of SQL Joins

SQL joins are categorized into different types based on how rows from two tables are matched and combined.

1. SQL INNER JOIN

The INNER JOIN in SQL returns only the rows where there is a matching value in both tables. It combines records based on a related column and excludes non-matching rows.

Syntax:

SELECT table1.column1,table1.column2,table2.column1,.... FROM table1  INNER JOIN
table2 ON table1.matching_column = table2.matching_column;
SQL-Join
Inner Join

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. 

students
Students Table
course
StudentCourse Table

Let's look at the example of INNER JOIN clause, and understand it's working. This query will show the names and age 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:

Output_11

2. SQL LEFT JOIN

A LEFT JOIN returns all rows from the left table, along with matching rows from the right table. If there is no match, NULL values are returned for columns from the right table. LEFT JOIN is also known as LEFT OUTER JOIN.

Syntax:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Left_Join
Left JOIN

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:

Null

3. SQL RIGHT JOIN

RIGHT JOIN returns all the rows of the table on the right side of the join and matching rows for the table on the left side of the join. It is very similar to LEFT JOIN for the rows for which there is no matching row on the left side, the result-set will contain null. RIGHT JOIN is also known as RIGHT OUTER JOIN. 

Syntax

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Right_join
Right JOIN

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-12

4. SQL FULL JOIN

FULL JOIN creates the result-set by combining 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.

Full_Join

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 (simulated in SQLite) 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-44

5. SQL Natural 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.

  • It joins tables using common columns with the same name.
  • It returns only rows where values in those columns match.
  • The common column appears only once in the result.

Example: Look at the two tables below:

Employee Table:

Employee

Department Table:

Depart_man

Example: Find all Employees and their respective departments.

SELECT 
Emp_name,
Dept_name
FROM Employee
NATURAL JOIN Department;

Output:

Dept_name
Suggested Quiz

0 Questions

Quiz Completed Successfully

Your Score : 0/0

Accuracy : 0%

Comment