Dark Mode
Image

SQL Tutorial

SQL Database

SQL Injection

PL/SQL Tutorial

Sql Interview Question

SQl Quiz

SQL Inner Join

INNER JOIN in SQL is the most common and important type of join which allows users to access matching data from two or more database tables.

When the join condition is met between the tables, then it returns all the common rows from them.

The Venn diagram of INNER JOIN is shown in the following picture. The shaded region of the Venn diagram shows the intersection values of two tables:

SQL Inner Join

Syntax of INNER JOIN in SQL

SELECT Column_Name1, Column_Name2, ….., Column_NameN  
FROM Table_Name1 INNER JOIN Table_Name2  
ON Table_Name1.Column_Name = Table_Name2.Column_Name;  

Example of Inner Join in SQL

Let's take two tables named Employee_Details and Department to understand the concept of INNER JOIN. The Employee_Details table contains Emp_ID, Emp_Name, Dept_ID, and Emp_Salary columns. The Department table contains Dept_Id and Dept_Name columns.

We can check the data of Employee_Details and Department table using the following two different queries:

SELECT * FROM Department;  

Output:

Dept_Id Dept_Name
1001 Finance
1002 Marketing
1003 Sales
1004 Coding
1005 Administration

 

SELECT * FROM Employee_Details;  

Output:

Emp_Id Emp_Name Dept_Id Emp_Salary
1 Akshay 1001 23000
2 Ram 1002 24000
3 Balram 1004 25000
4 Yatin NULL NULL
5 Manoj 1004 23000
6 Sheetal 1003 24000
8 Yogesh NULL NULL
9 Naveen NULL NULL
10 Tarun 1004 23000

The following query joins these above two tables using INNER JOIN in structured Query Language:

SELECT   
Employee_Details.Emp_Id,   
Employee_Details.Emp_Name, Department.Dept_Name, Employee_Details.Emp_Salary,   
  
FROM Department INNER JOIN Employee_Details   
  
ON   
Department.Dept_ID = Employee_Details.Emp_Id;   

 

Explanation of above INNER JOIN query:

This query joins the Employee_Details and Department table and accesses the records from both the tables where Department.Dept_Id = Employee_Details.DeptId.

It only fetches the details of those employees from both the tables whose Dept_Id in the Employee table matches with the Dept_Id of the Department table.

If the Dept_Id is NULL or not matching, then that row would not show in the output.

Output:

Emp_Id Emp_Name Dept_Name Emp_Salary
1 Akshay Finance 23000
2 Ram Marketing 24000
3 Balram Coding 25000
5 Manoj Coding 23000
6 Sheetal Sales 24000
10 Tarun Coding 23000

Comment / Reply From