×

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause.

What is INNER JOIN in SQL?

Inner Join is a type of join in the SQL. Inner Join in SQL is a widely used join. This join returns only those rows which are common in both tables. Inner join is used to join two tables.

Syntax of Inner Join in the SQL:

SELECT table1.columname1, table1.columnname2, table2.columnname1, table2.columnname2 FROM TABLE1 INNER JOIN TABLE2 ON table1.column = table2.column;

Table1.column = table2.column is the common column that maintains the parent child relationship between these two tables.

There are some steps that we have to use in the Inner Join in the SQL query:

1. Create a new database or use an existing database by selecting the database using the USE keyword followed by the database name.

2. Create a new table inside the selected database, or you can use an already created table.

3. If the table is newly created, insert the records in the newly created database using the INSERT query.

4. View the inserted data using the SELECT query without the Inner Join query.

5 Now, we are ready to use the Inner Join in the SQL queries.

Step 1: Create a new database or use an already created database.

I have already created a database. I will use my existing created database name, Company.

USE Company;

Company is the database name.

Those who didn’t have created a database follow the below query to create the database:

CREATE DATABASE database_name;

After creating the database, select the database using the USE keyword followed by the database name.

Step 2: Create a new table or use already existing table:

I have already created a table. I will use the existing table named Employees and Manager as we are executing the Inner Join query.

To create the new table, follow the below CREATE TABLE syntax:

CREATE TABLE table_name(

columnname1 datatype(column size),

columnname2 datatype(column size),

columnname3 datatype(column size)

);

Step 3: Insert the records in the newly created table using the INSERT query

Use the below syntax to insert new records in the table:

INSERT INTO table_name VALUES(value1, value2, value3);

Step 4: View the records using the SELECT query.

View the records from the table using the following syntax:

SELECT * FROM table_name;

The following query will display the records of Employees.

SELECT * FROM Employees;

The output of the above SELECT query is:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAC#5
1003NIKHILVANI50500JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAIC#5
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003RUCHIKAAGARWAL60000DELHIORACLE1
5001ARCHITSHARMA55500DELHITESTING4
5002SANKETCHAUHAN70000HYDERABADJAVA3
5003ROSHANNEHTE48500CHANDIGARHC#5
6001RAHULNIKAM54500BANGALORETESTING4
6002ATISHJADHAV60500BANGALOREC#5
6003NIKITAINGALE65000HYDERABADORACLE1

The following query will display the records of Manager.

SELECT * FROM Manager;

The output of the above SELECT query is:

Manageridmanager_namemanager_department
1Snehdeep KaurORACLE
2Kirti KirtaneFMW
3Abhishek ManishJAVA
4Anupam MishraTESTING
5Akash KadamC#

The following query will display the records of Laptop.

SELECT * FROM Laptop;

The output of the above SELECT query is:

LAPTOPIDNAME                    EMPLOYEEID
101DELLNULL
102HP1002
103LENOVONULL
104HP3003
105DELL4002
106LENOVO4003
107DELL5001
108HPNULL
109DELLNULL
110HPNULL
111LENOVO2002
112LENOVO6003
113HP1003

Step 5: We are ready to use the INNER JOIN in the queries

Let's understand the Inner Join with the help of examples.

Example 1: Write a query to display employee id, first name, last name, salary, city from the employee's table, and manager id and manager name from the manager table using the Inner join.

SELECT E.EMPLOYEEID, E.FIRST_NAME, E.LAST_NAME, E.SALARY, E.CITY, M.MANAGERID, M.MANAGER_NAME FROM EMPLOYEES E INNER JOIN MANAGER M ON E.MANAGERID =M.MANAGERID;

In the above query, we have fetched employee id, first name, last name, salary, city from the employee's table and manager id, manager name from manager table where employees’ table manager id column equals to manager table manager id column. The query will return all the matches records from both tables. Manager id is a common column between both tables. E is an alias name for the employee's table, while M is for the manager table. The manager id column works as a foreign key in the employee's table. The manager id works as the primary key in the manager table, which creates a parent-child relationship between the two tables.

The output of the above query is:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
2001PRACHISHARMA55500CHANDIGARHORACLE1
4003RUCHIKAAGARWAL60000DELHIORACLE1
6003NIKITAINGALE65000HYDERABADORACLE1
1003NIKHILVANI50500JAIPURFMW2
2002BHAVESHJAIN65500PUNEFMW2
3002ANUJAWANRE50500JAIPURFMW2
3001PRANOTISHENDE55500PUNEJAVA3
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4002ASHWINIBAGHAT54500NOIDAJAVA3
5002SANKETCHAUHAN70000HYDERABADJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
5001ARCHITSHARMA55500DELHITESTING4
6001RAHULNIKAM54500BANGALORETESTING4
1002VAIBHAVSHARMA60000NOIDAC#5
2003RUCHIKAJAIN50000MUMBAIC#5
5003ROSHANNEHTE48500CHANDIGARHC#5
6002ATISHJADHAV60500BANGALOREC#5
How To Use INNER JOIN In SQL

The records are displayed in the ascending order by default manager id.

Example 2: Write a query to display employee id, first name, last name, city, and department from the employee's table and manager id and manager name from manager table using Inner Join where employee department is C#.

SELECT E.EMPLOYEEID, E.FIRST_NAME, E.LAST_NAME, E.CITY, E.DEPARTMENT, M.MANAGERID, M.MANAGER_NAME FROM EMPLOYEES E INNER JOIN MANAGER M ON E.MANAGERID =M.MANAGERID WHERE DEPARTMENT = ‘C#’;

In the above query, we have fetched the employee id, first name, last name, city, and department from the employee's table, manager id, and manager name from manager table where manager id from employees table equal to manager id from manager table only those employee records whose employee department is C#.

The output of the above query is:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1002VAIBHAVSHARMA60000NOIDAC#5
2003RUCHIKAJAIN50000MUMBAIC#5
5003ROSHANNEHTE48500CHANDIGARHC#5
6002ATISHJADHAV60500BANGALOREC#5
How To Use INNER JOIN In SQL

In the obove output, we can see only those records are retrieved from employees whose department is C#. We use the WHERE clause with the INNER join.

Example 3: Write a query to display the employee id, first name, salary, city, and department from employee’s table, and laptop id and laptop name from laptop table using Inner Join.

SELECT E.EMPLOYEEID, E.FIRST_NAME, E.SALARY, E.CITY, E.DEPARTMENT, L.LAPTOPID, L.NAME FROM EMPLOYEES E INNER JOIN LAPTOP L ON E.EMPLOYEEID = L.EMPLOYEEID;

In the above query, we have fetched the employee id, first name, salary, the city, and department from employees’ table, and laptop id, and name from laptop table where employee id from employee table equals to the employee id from laptop table. Employee id is a foreign key in the Laptop table, which creates a parent-child relationship between the Employee table and Laptop table.

The output of the above query is:

EMPLOYEEIDFIRST_NAMESALARYCITYDEPARTMENTLAPTOPIDNAME
1002VAIBHAV60000NOIDAC#102HP
3003DEEPAM58500MUMBAIJAVA104HP
4002ASHWINI54500NOIDAJAVA105DELL
4003RUCHIKA60000DELHIORACLE106LENOVO
5001ARCHIT55500DELHITESTING107DELL
2002BHAVESH65500PUNEFMW111LENOVO
6003NIKITA65000HYDERABADORACLE112LENOVO
1003NIKHIL50500JAIPURFMW113HP
How To Use INNER JOIN In SQL

Only those records are displayed in the given output to whom the Laptop is assigned.

Example 4: Write a query to display employee id, first name, salary, and department from employees table, and manager Id and manager name from manager table using inner join where salary > 57000 or department is Oracle.

SELECT EMPLOYEEID, FIRST_NAME, SALARY, DEPARTMENT, M.MANAGERID, M.MANAGER_NAME FROM EMPLOYEES E INNER JOIN MANAGER M ON E.MANAGERID = M.MANAGERID WHERE SALARY > 57000 OR DEPARTMENT = 'ORACLE';

In the above query, we have fetched the employee id, first name, salary, and department from the employee's table, and  manager id and manager name from manager table where manager id from employees table equals to the manager id from manager only those employees whose salary is greater than 57000 or Department is Oracle.

The output of the above query is:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
2001PRACHISHARMA55500CHANDIGARHORACLE1
4003RUCHIKAAGARWAL60000DELHIORACLE1
6003NIKITAINGALE65000HYDERABADORACLE1
2002BHAVESHJAIN65500PUNEFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
5002SANKETCHAUHAN70000HYDERABADJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
1002VAIBHAVSHARMA60000NOIDAC#5
6002ATISHJADHAV60500BANGALOREC#5
How To Use INNER JOIN In SQL

In the above output, only those employee records are displayed whose salary is greater than 57000 or employee department is 'Oracle'. The OR operator display records if one of the given condition in the query is true.


Related Topics

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

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 COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 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 NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 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 Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

3 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

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

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

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

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 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 JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.