The following topics will be covered in this tutorial:

What are Joins in SQL?

As the term suggests, SQL Joins refers to a clause in SQL which is used to combine rows of two or more tables by using common values. It takes into consideration the records from two or more tables in a database and combines them. SQL Join is typically used in situations where you want to access one or more tables through a select statement.

SQL Join Syntax

SQL Join ( also referred to as INNER JOIN ) is the most commonly used method of joining data from multiple tables. The syntax for SQL Join is given below:

Consider the example below, the syntax used to join data from these tables will be:

SELECT Name, City, Marks, Phone_no
FROM Student_detail, Personal_detail
WHERE Student_details.Name=Personal_detail.Name;

SQL Join Example:

Now let us understand SQL Join with the help of a detailed example: 

Consider this table showing student details:

Name Roll No. Marks
Akshay 1 57
Charu 2 68
Disha 3 52
Eva 4 68
Himanshu 5 75
Jitesh 6 88

Below given is another table showing the personal details:

Name City Phone No Email Id
Akshay Jaipur 9543846521 [email protected]
Disha Bombay 8532465892 [email protected]
Jitesh Banglore 9684365125 [email protected]

Prepare yourself for the industry by going through this Top SQL Interview Questions and Answers!

Now, by giving the following syntax, the two tables are joined together to form a new table that shows the name of the student, his/her city, marks obtained, and phone number.

SELECT Name, City, Marks, Phone_no
FROM Student_detail, Personal_detail
WHERE Student_details.Name=Personal_detail.Name;

The result of the above SQL Join syntax is given below:

Name City Marks Phone No
Akshay Jaipur 57 9543846521
Disha Bombay 52 8532465892
Jitesh Banglore 88 9684365125

Become a Database Architect

SQL Join Types

There are different types of joins available in SQL:

  • Inner Join in SQL
  • Left Join in SQL
  • Right Join in SQL
  • Full Join in SQL

Inner Joins in SQL:

Inner SQL Join Query returns a value when there is a match in both tables. Inner Join is the most commonly used method when it comes to joining tables. The terms ‘Inner Join’ and ‘SQL Join’ are sometimes used interchangeably. Below given is the syntax for Inner Join. It is also the same syntax used in the case of SQL Join.

Syntax:

SELECT Column_list
FROM TABLE1
INNER JOIN TABLE2
ON Table1.ColName = Table2.ColName
Watch this Inner Join in SQL video
SQL Joins - Types, Syntax, and Examples

Example: 

SELECT students_data.RollNo, students_data.Name, students_data.Address, students_mark.Marks, students_mark.Grade
FROM students_data
INNER JOIN students_mark ON students_data.RollNo = students_mark.RollNo;

Left Joins in SQL:

Left SQL Join Query returns all the values of the left table and matches the rows from the right table. Now, if there is no matching value on the right side, Left Join will return the value from the right side as NULL. In most of the cases, the result from Left Join is mostly the same as one from Right Join except that all the rows from the left table are also included.

Watch this Left Join in SQL video
SQL Joins - Types, Syntax, and Examples

Syntax:

SELECT column_name
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;

Example:

SELECT Student_details.Name, Address, Roll_no, Email_id
FROM Student_details
LEFT JOIN Personal_details
ON Student_details.Name= Personal_details.Name;

We have the perfect professional SQL Training Course for you!

Name Roll No. Address Email Id
Akshay 1 Jaipur [email protected]
Charu 2 NULL NULL
Disha 3 Bombay [email protected]
Eva 4 NULL NULL
Himanshu 5 NULL NULL
Jitesh 6 Banglore [email protected]

Right Joins in SQL:

Right SQL Join Query returns all the rows of the right table even if there are no matches in the left table. Basically, the Right Join will return all the values from the right table along with the matched values of the left table. Also, if there is no matched value found, the result will be NULL.

Watch this Right Join in SQL video
SQL Joins - Types, Syntax, and Examples

Syntax:

SELECT column_name
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;

Example:

SELECT Personal_details.Name, Address, Roll_no, Email_id
FROM Student_details
RIGHT JOIN Personal_details

Come to Intellipaat’s SQL Community if you have more queries on SQL!

ON Student_details.Name= Personal_details.Name;
Name Address Roll No. Email ID
Akshay Jaipur 1 [email protected]
Disha Bombay 3 [email protected]
Jitesh Banglore 6 [email protected]

FULL Joins in SQL:

Full SQL Join Query returns all the rows from the left table and the right table. The result set is created by combining all the rows of both tables. The results of LEFT JOIN and RIGHT JOIN are combined to arrive at the result of FULL JOIN.

Watch this Full Join in SQL video
SQL Joins - Types, Syntax, and Examples

Syntax:

SELECT column_name
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;

Example:

SELECT Student_details.Name, Roll_no, Address
FROM Student_details
FULL OUTER JOIN Personal_details
ON Student_details.Name= Personal_details.Name;
Name Roll No. Address
Akshay 1 Jaipur
Charu 2 NULL
Disha 3 Bombay
Eva 4 NULL
Himanshu 5 NULL
Jitesh 6 Banglore

Wish to get certified in SQL! Learn SQL from top SQL experts and excel in your career with Intellipaat’s SQL certification.

Course Schedule

Name Date
SQL Training 2022-10-01 2022-10-02
(Sat-Sun) Weekend batch
View Details
SQL Training 2022-10-08 2022-10-09
(Sat-Sun) Weekend batch
View Details
SQL Training 2022-10-15 2022-10-16
(Sat-Sun) Weekend batch
View Details

1 thought on “SQL Joins - Types, Syntax, and Examples”

Leave a Reply

Your email address will not be published. Required fields are marked *