How to use INNER JOIN in SQL Server?

The most frequently used and important of the joins is the INNER JOIN. They are also referred to as an EQUIJOIN.

The INNER JOIN creates a new result table by combining column values of two tables (table1 and table2) based upon the join-predicate. The query compares each row of table1 with each row of table2 to find all pairs of rows which satisfy the join-predicate. When the join-predicate is satisfied, column values for each matched pair of rows of A and B are combined into a result row.

Syntax: The basic syntax of INNER JOIN is as follows:

SELECT table1.column1, table2.column2…
FROM table1
INNER JOIN table2
ON table1.common_field = table2.common_field;

Image Representation:

The INNER JOIN in SQL joins two tables according to the matching of a certain criteria using a comparison operator.

Sample Two Table Data:

FOODS:

 ITEM_ID ITEM_NAME ITEM_UNIT COMPANY_ID
1 Bread PCS 25
2 Sliced Bread PCS 16
3 Roasted Bread PCS 32
4 Garlic Bread PCS 48
5 Cheese Bread PCS 18
6 Fruit Bread PCS 22
7 Milk Bread PCS 28

Company:

COMPANY_ID COMPANY_NAME COMPANY_CITY
25 SUBWAY IRLAND
16 BURGER KING UNITED STATES
32 KFC UK
18 ROLLZONE AUSTRALIA
28 DOMINOS INDIA

Example

Here is an example of inner join in SQL between two tables.

To join item name, item unit columns from foods table and company name, company city columns from company table, with the following condition –

1. company_id of foods and company_id company table must be same, the following SQL statement can be used :

SELECT foods.item_name,foods.item_unit,
company.company_name,company.company_city
FROM foods
INNER JOIN company
ON foods.company_id =company.company_id;

Example of SQL INNER JOIN using JOIN keyword

To get item name, item unit columns from foods table and company name, company city columns from company table, after joining these mentioned tables, with the following condition –

1. company id of foods and company id of company table must be same,

the following SQL statement can be used :
SELECT foods.item_name,foods.item_unit,
company.company_name,company.company_city
FROM foods
JOIN company
ON foods.company_id =company.company_id;

About sqlschool

SQL School (Regd: SequelGate Innovative Technologies Pvt. Ltd.) is one of fastest growing organization delivering Realtime Training and Realtime Projects exclusively on Microsoft SQL Server. Our Training services include SQL DBA Online Training, MSBI Online Training, SQL DBA Classroom Training, MSBI Classroom Training and Realtime Projects. SQL School, established in February, 2008 is now, one of the best institute offering Trainings on SQL Server, SQL Database Administration (DBA) and Business Intelligence (BI) technologies. We have been working with numerous consultancies in India, US, UK and Australia. We undertake Online Trainings and Corporate Trainings on SQL Server 2008, SQL Server 2008 R2 and SQL Server 2012 (DENALI) versions. We make sure that all our sessions are very much interactive and well structured. We encourage every participant to come up with his / her own queries during & after the training sessions. We prefer practical approach rather than theoretical information to master the technical depth of the technology. We provide excellent Lab Handouts for practice, Realtime Case Studies and Projects on SQL Server Administration (SQL DBA) and Microsoft Business Intelligence (MS BI) Training courses. Free LIVE DEMOs would be provided prior to training registration. Specific DAY to DAY Course Plan will also be shared prior to training registration to ensure transparency of our Training services.