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 Server Inner Join returns the records (or rows) present in both tables as long as the Condition after the ON Keyword is TRUE.
SQL Inner Join Syntax
The syntax of the Inner Join in SQL Server is
-- SQL Server INNER JOIN Syntax 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 join is the default join, so it is optional to use the INNER Keyword. Let us see the visual representation of the Inner join for better understanding.
From the above image, you can understand easily that, Sql Server Inner join only displays the matching records from Table 1 and Table 2 (Like an Intersect in Math)
For this Sql Server Inner Join example, We are going to use two tables in our [SQL Server Tutorials] Database. Data present in the Employee Table is:
Data present in the SQL Server Department Table is:
SQL Inner Join Select All Columns
The following Inner Join Query will display all the columns present in Employees and Department tables
-- SQL Server INNER JOIN Example SELECT * FROM [Employee] INNER JOIN [Department] ON [Employee].[DepartID] = [Department].[id]
Let me avoid the INNER Keyword
-- SQL Server INNER JOIN Example SELECT * FROM [Employee] JOIN [Department] ON [Employee].[DepartID] = [Department].[id]
If you observe the above screenshot, Although we had 15 records in the Employee table, 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 columns. So, Please avoid SELECT * Statements in INNER JOINS
SQL Inner Join Select Few Columns
Please place the required columns after the SELECT Statement to avoid unwanted columns in Inner Join.
-- SQL Server INNER JOIN Example SELECT [FirstName] ,[LastName] ,[DepartmentName] FROM [Employee] INNER JOIN [Department] ON [Employee].[DepartID] = [Department].[id]
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 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.
-- SQL Server 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 column is present in both Employee and department table. And SQL Server doesn’t know which column you are asking it to retrieve.
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 column belonging to the department table.
We can write the above Inner Join query as:
-- SQL Server INNER JOIN Example 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]
Inner Join Where Clause
The SQL Server Inner Join also allows us to use Where Clause to limit the number of rows delivered by the Inner Join. In this example, we will use that WHERE Clause along with the Inner Join.
-- SQL Server Inner Join Example 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'
Inner Join Order By Clause
The SQL Server Inner Join allows us to use Order By Clause to rearrange the order of the records.
-- SQL Server INNER JOIN Example 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