×

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query.

Inner join query retrieves the records from one or more tables with similar data or records. The joins condition returns only common rows between the tables.

Inner Join is one of the types of joins in the Structured Query Language Join.

Suppose we have two tables, Table A and Table B. Table A have some columns, and Table B also has some columns. How joining will happen between these two-columns? In this case, there is one common column between these two tables. So joining will be met through this common column.

Syntax of Inner Join in SQL:

SELECT COLUMNNAME1, COLUMNNAME2, COLUMNNAME3, COLUMNNAME4 FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.COLUMN NAME = TABLE2.COLUMN NAME;

Example of Inner Join in Structured Query Language:

Consider these tables to understand the Inner Join concept.

Table 1: Employees:

EMPLOYEE_IDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
1002VAIBHAVSHARMA60000BANGALOREC#4
1003NIKHILVANI50500HYDERABADFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAIC#4
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500HYDERABADFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING5
4002ASHWINIBAGHAT54500BANGALOREJAVA3
4003RUCHIKAAGARWAL60000DELHIORACLE1
5001ARCHITSHARMA55500DELHITESTING5
5002RAKESHKUMAR70000CHANDIGARHC#4
5003MANISHSHARMA62500BANGALORETESTING5

Table 2: Manager.

Manager_IdManager_NameManager_Department
1Kirti ChaudharyORACLE
2Manish BafnaFMW
3Snehdeep KaurJAVA
4Satish KumarC#
5Anupam MishraTESTING

Example 1: Execute an inner join query on the employee and manager table name.

SELECT Employee_Id, First_Name, Last_Name, Salary, City, Department, M.Manager_Id, Manager_Name FROM Employees E INNER JOIN Manager M ON E.Manager_Id = M.Manager_Id;

The above Inner join query joins the employees' table and manager table and retrieves the data from the tables where E.Manager_Id = M.Manager_Id.

The output of the above query is as follows:

EMPLOYEE_IDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGER_IDMANAGER_NAME
1001VAIBHAVIMISHRA65500PUNEORACLE1Kirti Chaudhary
1002VAIBHAVSHARMA60000BANGALOREC#4Satish Kumar
1003NIKHILVANI50500HYDERABADFMW2Manish Bafna
2001PRACHISHARMA55500CHANDIGARHORACLE1Kirti Chaudhary
2002BHAVESHJAIN65500PUNEFMW2Manish Bafna
2003RUCHIKAJAIN50000MUMBAIC#4Satish Kumar
3001PRANOTISHENDE55500PUNEJAVA3Snehdeep Kaur
3002ANUJAWANRE50500HYDERABADFMW2Manish Bafna
3003DEEPAMJAUHARI58500MUMBAIJAVA3Snehdeep Kaur
4001RAJESHGOUD60500MUMBAITESTING5Anupam Mishra
4002ASHWINIBAGHAT54500BANGALOREJAVA3Snehdeep Kaur
4003RUCHIKAAGARWAL60000DELHIORACLE1Kirti Chaudhary
5001ARCHITSHARMA55500DELHITESTING5Anupam Mishra
5002RAKESHKUMAR70000CHANDIGARHC#4Satish Kumar
5003MANISHSHARMA62500BANGALORETESTING5Anupam Mishra
SQL Inner Join

Example 2: Execute an inner join query on table name Employees and Manager using order by clause.

SELECT Employee_Id, First_Name, Last_Name, Salary, City, Department, M.Manager_Id, Manager_Name FROM Employees E INNER JOIN Manager M ON E.Manager_Id = M.Manager_Id ORDER BY Manager_Id;

The above inner join query joins the two tables named Employees Table and Manager Table and retrieves the data from both the tables where E.MANAGER_ID = M.MANAGER_ID. The employees' records will be displayed in the ascending order by Manager_Id as we have used the ORDER BY clause in the query. The query will display only similar records between the two tables.

The output of the above query is as follows:

EMPLOYEE_IDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGER_IDMANAGER_NAME
1001VAIBHAVIMISHRA65500PUNEORACLE1Kirti Chaudhary
2001PRACHISHARMA55500CHANDIGARHORACLE1Kirti Chaudhary
4003RUCHIKAAGARWAL60000DELHIORACLE1Kirti Chaudhary
2002BHAVESHJAIN65500PUNEFMW2Manish Bafna
3002ANUJAWANRE50500HYDERABADFMW2Manish Bafna
1003NIKHILVANI50500HYDERABADFMW2Manish Bafna
4002ASHWINIBAGHAT54500BANGALOREJAVA3Snehdeep Kaur
3003DEEPAMJAUHARI58500MUMBAIJAVA3Snehdeep Kaur
3001PRANOTISHENDE55500PUNEJAVA3Snehdeep Kaur
2003RUCHIKAJAIN50000MUMBAIC#4Satish Kumar
5002RAKESHKUMAR70000CHANDIGARHC#4Satish Kumar
1002VAIBHAVSHARMA60000BANGALOREC#4Satish Kumar
5001ARCHITSHARMA55500DELHITESTING5Anupam Mishra
5003MANISHSHARMA62500BANGALORETESTING5Anupam Mishra
4001RAJESHGOUD60500MUMBAITESTING5Anupam Mishra
SQL Inner Join

Example 3: Execute an inner join query on table name Employees and Manager using group by clause.

SELECT Employee_Id, First_Name, Last_Name, Salary, City, Department, M.Manager_Id, Manager_Name FROM Employees E INNER JOIN Manager M ON E.Manager_Id = M.Manager_Id GROUP By Salary;

The above inner join query joins the two Employees Table and Manager Table and retrieves the data from the tables where E.MANAGER_ID = M.MANAGER_ID. The data is displayed in the GROUP BY employee salary. Employees with the same salary will be in a group with one salary. Employees who don't have the same salary number as another employee's salary, will have separate salary group.

The output of the above query is as follows:

EMPLOYEE_IDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGER_IDMANAGER_NAME
2003RUCHIKAJAIN50000MUMBAIC#4Satish Kumar
1003NIKHILVANI50500HYDERABADFMW2Manish Bafna
4002ASHWINIBAGHAT54500BANGALOREJAVA3Snehdeep Kaur
2001PRACHISHARMA55500CHANDIGARHORACLE1Kirti Chaudhary
3003DEEPAMJAUHARI58500MUMBAIJAVA3Snehdeep Kaur
1002VAIBHAVSHARMA60000BANGALOREC#4Satish Kumar
4001RAJESHGOUD60500MUMBAITESTING5Anupam Mishra
5003MANISHSHARMA62500BANGALORETESTING5Anupam Mishra
1001VAIBHAVIMISHRA65500PUNEORACLE1Kirti Chaudhary
5002RAKESHKUMAR70000CHANDIGARHC#4Satish Kumar
SQL Inner Join

Example 4: Execute an inner join query on table name Employees and Manager using where clause.

SELECT Employee_Id, First_Name, Last_Name, Salary, City, Department, M.Manager_Id, Manager_Name FROM Employees E INNER JOIN Manager M ON E.Manager_Id = M.Manager_Id WHERE Salary > 52000;

The above inner join query joins the two Employees Table and Manager Table and retrieves the data from the tables where E.MANAGER_ID = M.MANAGER_ID. The query will display only employee's data where the employee salary is greater than 52000.

The output of the above query is as follows:

EMPLOYEE_IDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGER_IDMANAGER_NAME
1001VAIBHAVIMISHRA65500PUNEORACLE1Kirti Chaudhary
1002VAIBHAVSHARMA60000BANGALOREC#4Satish Kumar
2001PRACHISHARMA55500CHANDIGARHORACLE1Kirti Chaudhary
2002BHAVESHJAIN65500PUNEFMW2Manish Bafna
3001PRANOTISHENDE55500PUNEJAVA3Snehdeep Kaur
3003DEEPAMJAUHARI58500MUMBAIJAVA3Snehdeep Kaur
4001RAJESHGOUD60500MUMBAITESTING5Anupam Mishra
4002ASHWINIBAGHAT54500BANGALOREJAVA3Snehdeep Kaur
4003RUCHIKAAGARWAL60000DELHIORACLE1Kirti Chaudhary
5001ARCHITSHARMA55500DELHITESTING5Anupam Mishra
5002RAKESHKUMAR70000CHANDIGARHC#4Satish Kumar
5003MANISHSHARMA62500BANGALORETESTING5Anupam Mishra
SQL Inner Join

Related Topics

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 minutes read.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.

Types of SQL JOIN

The SQL JOIN combines one or more than one tables based on their relationship. The SQL JOIN involves a parent table and a child table relationship. There are different types of...

10 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.