The Wayback Machine - https://web.archive.org/web/20240926182907/https://www.geeksforgeeks.org/join-multiple-tables-using-inner-join/
Open In App

Join Multiple Tables Using Inner Join

Last Updated : 27 May, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

Difference between Natural join and Inner Join in SQL
The join operation merges the two tables based on the same attribute name and their datatypes are known as Natural join Unlike INNER JOIN, which requires you to specify the columns and conditions for the join explicitly. In this article, we will also see the differences between them. Let's start with Natural Join. Example: If you have two tables "S
3 min read
Full join and Inner join in MS SQL Server
Full Join Full join selects all the rows from the left and right tables along with the matching rows as well. If there are no matching rows, it will be displayed as NULL. Syntax: select select_list from table1 full join table2 on join _predicate (OR) select table1.*, table2.* from table1 full join table2 on join _predicate (OR) select * from table1
2 min read
Difference between Inner Join and Outer Join in SQL
JOINS in SQL are fundamental operations used to combine data from multiple tables based on related columns. They are essential for querying data that is distributed across different tables, allowing you to retrieve and present it as a single or similar result set. In this article, We will learn about Inner Join vs Outer Join in SQL in detail. What
5 min read
Difference Between “INNER JOIN” and “OUTER JOIN”
Joins in SQL are essential tools that allow us to combine rows from multiple tables based on specific conditions, often involving a relation between columns in those tables. These joins help in pulling related data together in a meaningful way. Among the most commonly used joins are INNER JOIN and OUTER JOIN. In this article, We will learn about th
5 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 only returns all the records or tuples or rows fr
3 min read
SQL | Join (Cartesian Join & Self Join)
SQL| JOIN(Inner, Left, Right and Full Joins) In this article, we will discuss about the remaining two JOINS: CARTESIAN JOIN SELF JOIN Consider the two tables below: StudentCourse CARTESIAN JOIN: The CARTESIAN JOIN is also known as CROSS JOIN. In a CARTESIAN JOIN there is a join for each row of one table to every row of another table. This usually h
2 min read
How Inner Join works in LINQ to SQL
LINQ (Language Integrated Query) in C# provides a powerful way to query data from various data sources, including databases. In LINQ to SQL, which is specifically designed for working with relational databases, an inner join is a common operation used to combine records from two tables based on a specified condition. This article explores the conce
3 min read
SQL Inner Join
SQL INNER JOIN combines two or more tables based on specified columns and retrieves records with matching values in the common columns. INNER JOIN IN SQLThe INNER JOIN clause in SQL is used to combine multiple tables and fetch records that have the same values in the common columns. The difference in the use of INNER JOIN vs OUTER JOIN is that INNE
2 min read
SQL Joins (Inner, Left, Right and Full Join)
SQL Join operation combines data or rows from two or more tables based on a common field between them. In this article, we will learn about Joins in SQL, covering JOIN types, syntax, and examples. SQL JOINSQL JOIN clause is used to query and access data from multiple tables by establishing logical relationships between them. It can access data from
5 min read
Insert Multiple Values Into Multiple Tables in SQL Server
To insert multiple values into multiple tables in SQL Server, use the OUTPUT clause. The SQL OUTPUT clause allows to insert multiple values into multiple tables in a single query. Output clause clause in SQL is used to capture data affected by Data Manipulation Language (DML) statements like INSERT, UPDATE, or DELETE. SyntaxSyntax to insert multipl
2 min read
SQL Query to select Data from Tables Using Join and Where
The aim of this article is to make a simple program to Join two tables using Join and Where clause using MySQL. Below is the method to do the same using MySQL. The prerequisites of this article are MySQL and Apache Server on your computer are installed. What is a Query in SQL? A SQL query is a request passed for data/information from a table in a d
3 min read
SQL Full Outer Join Using Left and Right Outer Join and Union Clause
An SQL join statement is used to combine rows or information from two or more than two tables on the basis of a common attribute or field. There are basically four types of JOINS in SQL. In this article, we will discuss FULL OUTER JOIN using LEFT OUTER Join, RIGHT OUTER JOIN, and UNION clause. Consider the two tables below: Sample Input Table 1: Pu
3 min read
SELECT data from Multiple Tables in SQL
The statement is used to retrieve the fields from multiple tables and with the help of JOIN operations we easily fetch the records from multiple tables, Generally JOINS are used when there are common records between two tables. In this article, we will look into various types of JOIN that are used in SQL. SQL JoinsSQL joins combine two or more tabl
3 min read
Querying Multiple Tables in SQL
Here, we are going to see how to query multiple tables in SQL. For example, here, we will first create a database named “geeks” then we will create 2 tables “department” and "employee" in that database. After, that we will execute our query on the tables. Creating a Database : Use the below SQL statement to create a database called geeks: CREATE DA
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
Drop Multiple Tables in MySQL
DROP statement in a relational database management system (RDBMS) is used to delete a database object such as a table, index, or view. It can be used to delete the complete database also. The general syntax of the DROP command is as follows: Syntax: DROP object object_name; Example: DROP DATABASE db; In everyday life scenarios and live projects, we
3 min read
How to Retrieve Data from Multiple Tables in SQL?
In SQL, Retrieving data from multiple tables is a common requirement in database operations. In this article, we will explore multiple approaches to retrieving data from multiple tables in SQL. We will provide an introduction to the topic, explain two distinct approaches with their respective syntax, present detailed examples for each approach with
4 min read
Left join and Right join in MS SQL Server
Prerequisite – Introduction of MS SQL Server 1. Left Join : A join combines the set of two tables only. A left join is used when a user wants to extract the left table's data only. Left join not only combines the left table's rows but also the rows that match alongside the right table. Syntax - select select_list from table1 left join table2 on joi
2 min read
Self Join and Cross Join in MS SQL Server
Prerequisite - Introduction of MS SQL Server 1. Self Join : Self-join allows us to join a table itself. It is useful when a user wants to compare the data (rows) within the same table. Syntax - select select_list from T t1 [Inner|Left] Join on T t2 on join_predicate. Here T refers to the table we use for comparison and it is referred twice. To avoi
2 min read
SQL | EQUI Join and NON EQUI JOIN
Types of SQL Joins are explained in left, right, and full join and SQL | Join (Cartesian Join & Self Join). And Remaining EQUI Join and NON-EQUI will discuss in this article. Let's discuss one by one. SQL JOINS : EQUI JoinNON-EQUI Join Example - Let's Consider the two tables given below. Table name — Student In this table, you have I'd, name, c
2 min read
Implicit Join vs Explicit Join in SQL
JOIN clause is used to combine rows from two or more tables, based on a relation between them. There are two different syntax forms to perform JOIN operation: Explicit joinImplicit join Step 1: Creating the Database Use the below SQL statement to create a database called geeks: CREATE DATABASE geeks; Step 2: Using the Database Use the below SQL sta
3 min read
SQL Left Outer Join vs Left Join
In SQL, both "LEFT JOIN" and "LEFT OUTER JOIN" are used to combine data from two or more tables based on a related column, but they are essentially the same operation, and there is no practical difference between them. The keyword "OUTER" is optional in most database systems, including popular ones like MySQL, PostgreSQL, and SQL Server. Left JoinA
4 min read
Difference Between Left Join and Left Outer Join
In SQL language, different joins are used to assemble rows from two or more tables from the related column. The terms "Left Join" and "Left Outer Join" are used interchangeably in SQL but they refer to the same concept. A Left Join retrieves all records from the left table (the first table in the query) and matches them with records from the right
5 min read
Difference Between Left Join and Right Join
In DBMS(Database Management System) Join is an operation that combines the row of two or more tables based on related columns between them. The main purpose of Join is to retrieve the data from multiple tables in other words Join is used to perform multi-table queries. So for that purpose, joins come into the picture. So in this article, we will go
5 min read
Difference Between Right Join and Right Outer Join
Joins in a Database (SQL) are mostly used for combining data or the rows of two or more table records that are based on the same or common attribute. There are various types of Joins like Right Join, Left Join, Full Join, etc. Each join has its own syntax and data-returning capability. In this article, we will see the information about Right Join a
5 min read
How to Perform SQL Join on Multiple Columns in Same Table?
To perform SQL JOIN on multiple columns in the same table use the Self Join. Using Self Join on a table, a connection between columns of the same table can be created. We can implement Self Join on a table using “inner”, “left”, “right”, “full”, or “cartesian” join. Let us see how to perform a Self Join to connect multiple columns within the same t
3 min read
Copy tables between databases in SQL Server using Import-and-Export Wizard
Introduction to Import-and-Export Wizard : The wizard allows easy steps through a process, and to execute the data copy process with writing very little or no code. However, for importing and exporting data from one source into another, the below steps could be followed - Open the Object Explorer, select the database, right-click on the database na
2 min read
Joining three or more tables in SQL
There may occur some situations sometimes where data needs to be fetched from three or more tables. This article deals with two approaches to achieve it. Example: Creating three tables: student marks details Note: Click on image if not clear to view in bigger size. Table 1: student create table student(s_id int primary key, s_name varchar(20)); ins
3 min read
Compare and Find Differences Between Two Tables 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. Here we are going to see how to Compare and Find Differences Between Two Tables in SQL Here, we will first create a database named “geeks” then we will create two tables “departme
3 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
Article Tags :