SQL INNER JOIN

The SQL Server Inner Join returns the records (or rows) present in both tables If there is at least one match between columns.

Or we can say SQL Inner Join returns the records (or rows) present in both tables as long as the Condition after the ON Keyword is TRUE and the syntax is

SELECT Table1.Column(s), Table2.Column(s),
FROM Table1
 INNER JOIN
     Table2 ON
   Table1.Common_Column = Table2.Common_Column

--OR We can Simply Write it as
SELECT Table1. Column(s), Table2. Column(s),f
FROM Table1
 JOIN
     Table2 ON
   Table1.Common_Column = Table2.Common_Column

The SQL Server Inner is the default join, so it is optional to use the INNER Keyword. Let us see the visual representation of this for better understanding.

Ven Diagram

From the above image, you can easily understand that the SQL Inner join only displays the matching records from Table1 and Table2 (Like an Intersect in Math). For this example, We are going to use two tables in our Database. Data present in the Employee is:

Left Table Data

Data present in the SQL Server Department is:

Department Table records

SQL Inner Join Select All Columns

The following query will display all the columns present in the Employees and Department tables.

SELECT *
FROM [Employee]
   INNER JOIN
     [Department] ON
 [Employee].[DepartID] = [Department].[id]
SQL INNER JOIN Select All Columns 8

Let me avoid the INNER Keyword.

SELECT *
FROM [Employee]
   JOIN
     [Department] ON
 [Employee].[DepartID] = [Department].[id]
Without Keyword 1

If you observe the above screenshot, Although we had 15 records in the Employee, SQL Server Inner join is displaying 11 records. It is because Department Id for the remaining four records (i., ID numbers 10, 11, 14, and 15) in the Employee table are NULLS.

NOTE: The [Department ID] column is repeated twice, which is annoying to the user. By selecting individual column names, we can avoid unwanted cols. So, Please avoid the SELECT * Statement.

SQL Inner Join Select Few Columns

Please place the required ones after the SELECT Statement to avoid unwanted columns in Inner Join.

SELECT [FirstName]
      ,[LastName]
      ,[DepartmentName]
FROM [Employee]
  INNER JOIN
     [Department] ON
  [Employee].[DepartID] = [Department].[id]
Select Few Columns 2

The above Inner Join query will perfectly work as long as the column names from both the tables are different like above. What happens if we have the same Column titles in both tables? Well, you will end up in a mess. Let us see how to resolve the issue.

Before we get into the solution, let me show you one practical example. As you can see, we are using the above query, but we added id from the department table as an additional column.

SELECT [FirstName]
      ,[LastName]
      ,id
      ,[DepartmentName]
FROM [Employee]
INNER JOIN
     [Department] ON
           [Employee].[DepartID] = [Department].[id]

You can see from the below screenshot, the SQL Inner Join is throwing an error Ambiguous column name id. It is because the id is present in both employees and departments. And the Server doesn’t know which one you are asking it to retrieve.

INNER Ambiguous Column Name Error 9

To resolve these kinds of problems, always have to use the table name before the column name. The following query uses the ALIAS table name before the column names. By this approach, we can inform the server that we are looking for an id belonging to the department table.

We can write the above SQL Server Inner Join Ambiguous column name error query as the following.

SELECT Emp.[FirstName] AS [First Name]
      ,Emp.[LastName] AS [Last Name]
	  ,Dept.id 
      ,Dept.[DepartmentName] AS [Department Name]
FROM [Employee] AS Emp
INNER JOIN
     [Department] AS Dept ON
    Emp.[DepartID] = Dept.[id]
Solve Error Msg 209 10

Inner Where Clause

It also allows us to use the Where Clause to limit the number of rows delivered by it. In this example, we will use that WHERE Clause along with the Inner Join.

SELECT Emp.[FirstName] AS [First Name]
      ,Emp.[LastName] AS [Last Name]
      ,Dept.[DepartmentName] AS [Department Name]
FROM [Employee] AS Emp
INNER JOIN
     [Department] AS Dept ON
			Emp.[DepartID] = Dept.[id]
WHERE Dept.[DepartmentName] = 'Software Developer' OR
		Dept.[DepartmentName] = 'Sr. Software Developer'
SQL INNER JOIN Where Clause 11

SQL Server Inner Join Order By Clause

It allows us to use Order By Clause to rearrange the order of the records.

SELECT Emp.[FirstName] AS [First Name]
      ,Emp.[LastName] AS [Last Name]
      ,Dept.[DepartmentName] AS [Department Name]
FROM [Employee] AS Emp
INNER JOIN
     [Department] AS Dept ON
    Emp.[DepartID] = Dept.[id]
ORDER BY [FirstName] ASC
SQL INNER JOIN Order By 3
Categories SQL