A Step-By-Step Walkthrough of SQL Inner Join
A Step-By-Step Walkthrough of SQL Inner Join
A Step-By-Step Walkthrough of SQL Inner Join
Organizations are generating and analyzing unmatched volumes of data with each passing minute. In this article, we
will demonstrate how we can employ SQL Inner Join to query and access data from multiple tables that store this
incessantly growing data in the SQL databases.
Before we get started with SQL Inner Join, I would like to call out SQL Join here, Join is the widely-used clause in the
SQL Server essentially to combine and retrieve data from two or more tables. In a real-world relational database, data is
structured in a large number of tables and which is why, there is a constant need to join these multiple tables based on
logical relationships between them. There are four basic types of Joins in SQL Server – Inner, Outer (left, right, full), Self
and Cross join. To get a quick overview of all these joins, I would recommend going through this link, SQL Join types
overview and tutorial.
This article targets all about the Inner Join in SQL Server, so let’s head over to it.
Inner Join clause in SQL Server creates a new table (not physical) by combining rows that have matching values in two
or more tables. This join is based on a logical relationship (or a common field) between the tables and is used to
retrieve data that appears in both tables.
Assume, we have two tables, Table A and Table B, that we would like to join using SQL Inner Join. The result of this join
will be a new result set that returns matching rows in both these tables. The intersection part in black below shows the
data retrieved using Inner Join in SQL Server.
SQL Server Inner Join Syntax
SELECT Column_list
FROM TABLE1
INNER JOIN TABLE2
ON Table1.ColName = Table2.ColName
Inner Join syntax basically compares rows of Table1 with Table2 to check if anything matches based on the condition
provided in the ON clause. When the Join condition is met, it returns matched rows in both tables with the selected
columns in the SELECT clause.
SQL Inner Join clause is the same as Join clause and works the same way if we don’t specify the type (INNER) while
using the Join clause. In short, Inner Join is the default keyword for Join and both can be used interchangeably.
Note – We will use the keyword ‘Inner’ Join in this article for the sake of more clarity. You can omit it while writing your
queries and can use only ‘Join’ as well.
Let’s create and populate Foods table now. CompanyID in this table is the foreign key that is referencing to the Primary
key of the PizzaCompany table created above.
The following table shows data in the Foods table. This table stores information like units sold per food item and also
the pizza outlet (CompanyId) that delivers it.
Now, if we would like to see the items and also the units sold by each pizza company, we can combine these two tables
with the help of an inner join clause being used on the field CompanyId (in our case this shares a foreign key
relationship).
Below is the result set of the above SQL Inner Join query. For each row in the table PizzaCompany, Inner Join compares
and finds the matching rows in the table Foods and returns all the matching rows as shown below. And if you notice,
CompanyId = 5 is excluded from the query result, as it does not make a match in the Foods table.
With the help of the above result set, we can make out the items and also the count of items delivered by pizza outlets
in various cities. For instance, Dominos made a delivery of 7 Breadsticks and 11 Medium Pizza in Los Angeles.
Let’s explore more into this join and suppose three waterparks (looks like summer) get opened in the state and these
waterparks outsource food from the pizza outlets mentioned in the table PizzaCompany.
I am going to quickly create a table WaterPark and load some arbitrary data into it as shown below.
As the saying goes, the picture is worth a thousand words. Let’s quickly see the database diagram of these three tables
with their relationships to understand them better.
Now we are going to include this third table in the SQL Inner Join clause to see how it is going to impact the result set.
Per data in the WaterPark table, the three waterparks have been outsourcing food from all the Pizza Companies but
Pizzeria (Id=6) and chuck e cheese (Id = 7). Execute the code below to see all the food distribution across the
waterparks by the Pizza outlets.
Based on CompanyId, SQL Inner Join matches rows in both tables, PizzaCompany (Table 1) and Foods (Table 2) and
subsequently looks for a match in the WaterPark (Table 3) to return rows. As shown below, with the addition of inner
join on WaterPark, CompanyId (6,7 (apart from 5)) are also excluded from the final result set as the condition
w.CompanyId = pz.CompanyId is not satisfied for Ids (6,7). This is how SQL Inner join helps to return specific rows of
data from multiple tables.
Let’s dig into SQL Inner Join more with a few more T-SQL clauses.
We can filter records based on a specified condition when SQL Inner Join is used with a WHERE clause. Assume that we
would like to get the rows where units sold were more than 6.
In the following query, the WHERE clause is added to extract results with value more than 6 for units sold.
Execute above code in SSMS to see the below result. Four such records are returned by this query.
Here, we intend to obtain total items sold by each Pizza company present in the City. As you can see below,
aggregated result in ‘totalquantitysold’ column as 18 (7+11) and 9 (6+3) for Los Angeles and San Diego respectively is
computed.
Before we conclude this article, let’s quickly go over terms, a SQL developer may hear sporadically – Equi and Theta
Join.
Equi Join
As the name suggests, equi join contains an equality operator ‘=’ either in the Join clause or in the WHERE condition.
SQL Inner, Left, Right are all equi joins when ‘=’ operator is being used as a comparison operator. Usually, when there is
a mention of SQL Inner Join, it is considered as an Inner equi Join, in an unusual situation only, equality operator is not
used.
To make things easier, I am going to refer to AdventureWorksDW2017 sample database and fire a query against
existing tables to demonstrate how equi join looks like.
Non-equi join is basically opposite of equi-join and is used when we join on a condition other than ‘=’ operator. This
type is rarely used in practice. Below is an example that makes use of theta join with an inequality operator (<) to
evaluate profit by estimating cost and selling prices in two tables.
Conclusion
I hope this article on ‘SQL Inner Join’ provides a comprehensible approach to one of the important and frequently used
clauses – ‘Inner join’ in the SQL Server to combine multiple tables. In case you have any questions, please feel free to
ask in the comments section below.
To continue your learning on SQL Joins, you can refer to below posts: