SQL Inner Join - Fetch Data From Two Tables in Single Query With Example

25-02-2018

SQL Inner Join - Fetch Data From Two Tables in Single Query With Example

SQL INNER JOIN joins two tables based on common column values


SQL inner join Syntax


SELECT column_name1,column_name2,...
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name = table_name2.column_name;	
	

To Understand the INNER JOIN practically, we will create two tables and we will apply INNER JOIN on them.
Let's create a table users that store the user information like: UserID, Name, Email, and MobileNumber.

UserID Name Email MobileNumber
1 Taohid taohidphp@gmail.com 9131628365
2 Sajid sajidfreefire@gmail.com 9589186366

Now, we will create another table orders that will store the user's orders.

OrderID UserID OrderDate ItemName Status
10 1 2027-12-01 Dell Laptop Delivered
11 2 2027-12-10 HP Laptop Delivered

In the both table, we can see there is a common column UserID. So, we can apply here Inner Join.

Normaly, if we select the order details from orders Table, we can only select OrderID, UserID, OrderDate, ItemName and Status. To view the User Details who made the order, we need to fire another query from users Table. But, using Inner Join We can select Order Details along with the user details in single query. Let's see the INNER JOIN query below to select order details and also uder details.


SELECT orders.OrderID,orders.OrderDate,orders.ItemName,orders.Status,users.Name,users.Email,users.MobileNumber FROM orders INNER JOIN users ON orders.UserID = users.UserID;	
	

The above query will give output as:
OrderID OrderDate ItemName Status Name Email MobileNumber
10 2027-12-01 Dell Laptop Delivered Taohid taohidphp@gmail.com 9131628365

Latest Published Blog


PurgeCSS - Remove unused CSS from Web Pages

PurgeCSS - Remove unused CSS from Web Pages

03-04-2023

It is generally a good practice to remove unused CSS from web pages. As mentioned earlier, removing unused CSS can provide several benefits, including faster page loading times, improved website performance, reduced page weight, and better code maintainability.


Installing Asterisk16 on CentOs7

Installing Asterisk16 on CentOs7

14-04-2022

Here is Step-by-Step Guide to Installing Asterisk16 on CentOs7.


Make CentOS7 Full Screen Like Primary Operating System in VirtualBox

Make CentOS7 Full Screen Like Primary Operating System in VirtualBox

01-03-2022

Learn How to Make CentOS7 Full Screen Like Primary Operating System in VirtualBox. After Installing CentOS in VirtualBox, CentOs Screen does not Occupy the Full Window Screen. Here in this Tutorial, We Will See how to do it.


Setting  Key to a Column For Quick Fetch Operation From Larze Records

Setting Key to a Column For Quick Fetch Operation From Larze Records

14-02-2019

To set a key to a column in SQL, you need to create an index on that column. An index is a data structure that allows the database to quickly look up records based on the values in the indexed column. By creating an index on a column, you can improve the performance of queries that filter, sort, or group by that column.