Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

SQL INNER JOIN

by suresh

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:

SQL INNER JOIN Example

Data present in the SQL Server Department Table is:

SQL INNER JOIN

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]
SQL INNER JOIN 8

Let me avoid the INNER Keyword

-- SQL Server INNER JOIN Example
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 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]
SQL INNER JOIN 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 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.

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 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]
SQL INNER JOIN 10

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'
SQL INNER JOIN 11

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

Placed Under: SQL

  • Install SQL Server
  • Install SQL Management Studio
  • Uninstall Management Studio
  • Install AdventureWorks Database
  • SQL Management Studio Intro
  • Connect SQL with sqlcmd utility
  • SQL Attach Database
  • SQL Detach Database
  • SQL Restore Database
  • Restore Database using BAK
  • SQL Rename Database with Files
  • Get SQL Database Names
  • SQL Create Table
  • SQL Rename Table
  • SQL Alter Table
  • SQL Add Column
  • SQL Rename Column
  • Get SQL Table Names in a DB
  • Find SQL Table Dependencies
  • Rename SQL Table & Column
  • SQL Global & Local Temp Table
  • SQL Table Variable
  • SQL Derived Table
  • SQL DATALENGTH
  • SQL Data Types
  • DML, DDL, DCL & TCL Cmds
  • SQL Query Builder
  • SQL ALIAS
  • SQL SELECT Statement
  • SQL SELECT DISTINCT
  • SQL SELECT INTO Statement
  • SQL INSERT Statement
  • SQL INSERT INTO SELECT
  • SQL BULK INSERT or BCP
  • SQL UPDATE Statement
  • SQL UPDATE from SELECT
  • SQL DELETE Statement
  • SQL TRUNCATE Table
  • SQL CASE Statement
  • SQL MERGE Statement
  • SQL Subquery
  • SQL CTE
  • SQL PIVOT
  • SQL UNPIVOT
  • SQL Clauses Examples
  • SQL TOP Clause
  • SQL WHERE Clause
  • SQL ORDER BY Clause
  • SQL GROUP BY Clause
  • SQL HAVING Clause
  • SQL Primary Key
  • SQL Foreign Key
  • SQL Referential Integrity
  • SQL Check Constraint
  • SQL Unique Constraint
  • SQL Default Constraint
  • SQL Clustered Index
  • SQL Non Clustered Index
  • SQL Filtered Indexes
  • SQL COALESCE Function
  • SQL IS NOT NULL
  • SQL IS NULL Function
  • SQL ISNULL
  • SQL JOINS
  • SQL CROSS JOIN
  • SQL FULL JOIN
  • SQL SELF JOIN
  • SQL Outer Joins
  • SQL Cross Join Vs Inner Join
  • SQL LEFT JOIN
  • SQL RIGHT JOIN
  • SQL AND & OR Operators
  • SQL Arithmetic Operators
  • SQL BETWEEN Operator
  • SQL Comparison Operators
  • SQL LIKE
  • SQL EXCEPT
  • SQL EXISTS Operator
  • SQL NOT EXISTS Operator
  • SQL INTERSECT
  • SQL IN Operator
  • SQL NOT IN Operator
  • SQL UNION
  • SQL UNION ALL
  • SQL IF ELSE
  • SQL ELSE IF
  • SQL WHILE LOOP
  • SQL Nested While Loop
  • SQL BREAK Statement
  • SQL CONTINUE Statement
  • SQL GOTO Statement
  • SQL IIF Function
  • SQL CHOOSE Function
  • SQL Change Data Capture
  • SQL Table Partitioning
  • SQL Table Partition using SSMS
  • SQL TRY CATCH
  • SQL VIEWS
  • SQL User Defined Functions
  • SQL Alter User Defined Functions
  • SQL Stored Procedure Intro
  • Useful System Stored Procedures
  • SQL SELECT Stored Procedure
  • SQL INSERT Stored Procedure
  • SQL UPDATE Stored Procedure
  • Stored Procedure Return Values
  • Stored Procedure Output Params
  • Stored Procedure Input Params
  • Insert SP result into Temp Table
  • SQL Triggers Introduction
  • SQL AFTER INSERT Triggers
  • SQL AFTER UPDATE Triggers
  • SQL AFTER DELETE Triggers
  • SQL INSTEAD OF INSERT
  • SQL INSTEAD OF UPDATE
  • SQL INSTEAD OF DELETE
  • SQL STATIC CURSOR
  • SQL DYNAMIC CURSOR
  • SQL FORWARD_ONLY Cursor
  • SQL FAST_FORWARD CURSOR
  • SQL KEYSET CURSOR
  • SQL TRANSACTIONS
  • SQL Nested Transactions
  • SQL ACID Properties
  • Create SQL Windows Login
  • Create SQL Server Login
  • SQL Server Login Error
  • Create SQL Server Roles
  • SQL Maintenance Plan
  • Backup SQL Database
  • SQL Ranking Functions Intro
  • SQL RANK Function
  • SQL PERCENT_RANK Function
  • SQL DENSE_RANK Function
  • SQL NTILE Function
  • SQL ROW_NUMBER
  • SQL Aggregate Functions
  • SQL Date Functions
  • SQL Mathematical Functions
  • SQL String Functions
  • SQL CAST Function
  • SQL TRY CAST
  • SQL CONVERT
  • SQL TRY CONVERT
  • SQL PARSE Function
  • SQL TRY_PARSE Function
  • SQL Calculate Running Total
  • SQL Find Nth Highest Salary
  • SQL Reverse String
  • SQL FOR XML PATH
  • SQL FOR XML AUTO
  • SQL FOR XML RAW

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy