×

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 KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 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.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

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

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 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 Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 minutes read.