Join Multiple Tables Using Inner Join
Last Updated :
27 May, 2021
To retrieve data from the single table we use SELECT and PROJECTION operations but to retrieve data from multiple tables we use JOINS in SQL. There are different types of JOINS in SQL. In this article, only operations on inner joins in MSSQL are discussed.
Inner Join is the method of retrieval of data from multiple tables based on a required condition and necessary conditions are that there must be common columns or matched columns between the two tables of the database and the data types of columns must be the same.
Let us see how to join two tables and three tables using Inner Join in MSSQL step-by-step.
Creating a database :
Creating a database GeeksForGeeks by using the following SQL query as follows.
CREATE DATABASE GeeksForGeeks;

Using the database :
Using the database student using the following SQL query as follows.
USE GeeksForGeeks;

Creating three tables student, course, and lecturer with SQL query as follows:
CREATE TABLE student
( stu_id varchar(10),
stu_name varchar(20),
course_id varchar(10),
branch varchar(20),
lecturer_id varchar(10)
);

CREATE TABLE course
(
course_id varchar(10),
course_name varchar(20)
);

CREATE TABLE lecturer
(
lecturer_id varchar(10),
lecturer_name varchar(20)
);

Verifying the database :
To view the description of the three tables in the database GeeksForGeeks using the following SQL query as follows.
EXEC sp_columns student;
EXEC sp_columns course;
EXEC sp_columns lecturer;

Inserting data into the three tables:
Inserting rows into student table using the following SQL query as follows:
INSERT INTO student VALUES
('1901401','DEVA','CS1003','C.S', 'P4002'),
('1901402','HARSH','CS1001','C.S', 'P4001'),
('1901403','ABHISHEK','CS1001','C.S', 'P4001'),
('1901404','GARVIT','CS1002','C.S', 'P4003'),
('1901405','SAMPATH','CS1003','C.S', 'P4002'),
('1901406','SATWIK','CS1002','C.S', 'P4003'),
('1901407','GUPTA','CS1001','C.S', 'P4001'),
('1901408','DAS','CS1003','C.S', 'P4002');

Inserting rows into the course table using the following SQL query as follows:
INSERT INTO course VALUES
('CS1001', 'DBMS'),
('CS1002', 'O.S'),
('CS1003', 'C.N'),
('CS1004', 'M.L'),
('CS1005', 'A.I');

Inserting rows into lecturer table using the following SQL query as follows:
INSERT INTO lecturer VALUES
('P4001', 'RAMESH'),
('P4002', 'RAVINDER'),
('P4003', 'RAHUL SHARMA'),
('P4004', 'PRADEEP KUMAR'),
('P4005', 'SRINIVASA RAO');

Verifying the inserted data :
Viewing the three tables after inserting rows by using the following SQL query as follows.
SELECT* FROM student;
SELECT* FROM course;
SELECT* FROM lecturer;

Applying inner joins:
The syntax for multiple joins:
SELECT column_name1,column_name2,..
FROM table_name1
INNER JOIN
table_name2
ON condition_1
INNER JOIN
table_name3
ON condition_2
INNER JOIN
table_name4
ON condition_3
.
.
.
Note: While selecting only particular columns use table_name. column_name when there are the same column names in the two tables otherwise you will get an ambiguous error.
Queries:
Inner Join on two tables student and course:
SELECT *
FROM student
INNER JOIN
course
ON
student.course_id = course.course_id;

All the columns of 2 tables appear that satisfy the equality condition
Inner Join on three tables student, course, and lecturer:
SELECT *
FROM student
INNER JOIN
course
ON
student.course_id = course.course_id
INNER JOIN
lecturer
ON
student.lecturer_id = lecturer.lecturer_id;

All the columns of 3 tables appear that satisfy the equality condition
Inner join on three tables student, course, lecturer but by selecting particular columns of a particular table.
SELECT stu_id, stu_name,course.course_id,course.course_name,
lecturer.lecturer_name
FROM student
INNER JOIN
course
ON
student.course_id = course.course_id
INNER JOIN
lecturer
ON
student.lecturer_id = lecturer.lecturer_id;

Similar Reads
Left join using data.table in R
The data. table package in R is one of the best data manipulation tools that enable users to manage big data with so much ease and flexibility. One of its essential operations is the join, particularly the left join. This article will explore how to perform a left join using data.table, its advantag
6 min read
How to Left Join Multiple Tables in SQL
Left Join is one of the Keywords used while writing queries in SQL. In SQL we normally use Join for the purpose of forming a new table by taking out common data like rows or records or tuples from both the tables which are having matching records in general. Here when it comes to Left Join in SQL it
3 min read
Multiple Joins in SQL
Structured Query Language or SQL is a standard database language that is used to create, maintain and retrieve the data from relational databases like MySQL, Oracle, etc. In this article, we will be using the Microsoft SQL Server. Multiple Joins :Here we are going to implement the concept of multipl
3 min read
Connecting Multiple Data Tables in Power BI
When you get data with different and multiple tables, you will look forward to knowing how these data relate to each other and have any relationship. Power BI helps us to understand with easy steps. Transform the data if you have to make changes and then load it.Power BI creates the relationship bet
4 min read
Joining 4 Tables in SQL
The purpose of this article is to make a simple program to Join two tables using Join and Where clause in SQL. Below is the implementation for the same using MySQL. The prerequisites of this topic are MySQL and the installment of Apache Server on your computer. Introduction :In SQL, a query is a req
3 min read
SQL Left Outer Join vs Left Join
In SQL, LEFT JOIN and LEFT OUTER JOIN are among the most commonly used join operations to combine data from multiple tables. These terms are interchangeable, as both retrieve all rows from the left table and the matching rows from the right table, with unmatched rows in the right table appearing as
5 min read
SQL Joins (Inner, Left, Right and Full Join)
SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Joining Three or More Tables in SQL
SQL joins are an essential part of relational database management, allowing users to combine data from multiple tables efficiently. When the required data is spread across different tables, joining these tables efficiently is necessary. In this article, weâll cover everything we need to know about j
5 min read
SQL | Query to select NAME from table using different options
Let us consider the following table named "Geeks" : G_ID FIRSTNAME LASTNAME DEPARTMENT 1 Mohan Arora DBA 2 Nisha Verma Admin 3 Vishal Gupta DBA 4 Amita Singh Writer 5 Vishal Diwan Admin 6 Vinod Diwan Review 7 Sheetal Kumar Review 8 Geeta Chauhan Admin 9 Mona Mishra Writer SQL query to write âFIRSTNA
2 min read
SQL Query to select Data from Tables Using Join and Where
In SQL, the JOIN clause combines data from multiple tables based on a common column, while the WHERE clause filters the results based on specific conditions. Together, they allow us to retrieve relevant data efficiently from related tables. This article will guide us in using SQL JOIN and WHERE clau
5 min read