SQL INNER JOIN

The SQL 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 Inner is the default join, so it is optional to use the INNER Keyword. Let us see the visual representation of the this for better understanding.

From the above image, you can understand easily that, 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 Employees and Department tables

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

Let me avoid the INNER Keyword

SELECT *
FROM [Employee]
   JOIN
     [Department] ON
 [Employee].[DepartID] = [Department].[id]
SQL INNER JOIN 1

If you observe the above screenshot, Although we had 15 records in the Employee, and Inner join is displaying 11 records. It is because Department Id for the remaining four records (i., ID number 10, 11, 14, and 15) in 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 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]
SQL INNER JOIN 2

The above SQL 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 the 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 Inner Join example. As you can see, we are using the above query but we added id from department table as an additional column.

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

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

SQL INNER JOIN 9

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

We can write the above SQL Inner Join 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]
SQL INNER JOIN 10

Inner with Where Clause

It also allows us to use Where Clause to limit the number of rows delivered by it. In this example, we will use that WHERE Clause along with the SQL 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 11

SQL 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 3