Joins in SQL Server
As the growing software/IT industry applications are developed in various programming languages like PHP, Java, Asp.net, Asp.net core, Asp.Net MVC, Javascript(KendoUI), Angular.Js, Node.js, Android, IOS etc., the database plays a major role in interacting with the User Interface (UI) of the application.
As these applications are developed in various programming languages, the UI of the application interacts with the database and fetches the records from the tables and displays them to the user when the user requests it.
Each programming language like PHP, Asp.net, or Java interacts with their own databases:
- Applications developed using PHP interact with MySql database.
- Applications developed using Java interact with Oracle database.
- Application developed using Asp.net interacts with Microsoft's Sql Server database. etc.
As these databases are RDBMS (Relational Database Management Systems) ,the data will be maintained in multiple tables and we can maintain/establish the connection/relationship between the tables by taking the primary key in the first table and the foreign key in the second table and so on.
To perform the connection/relationship between both the tables, each table must have a common column. Let's say we have a first table (Employee table) with the columns EmployeeId, EmployeeName and Employee Designation; and we have a second tabl (SalaryIncrement table) with the columns EmployeeId and IncrementSal. In these two tables Employee table and Salary Increment table we have a common column name, EmployeeId.
For EmployeeId in Employee table we make it the primary key and for EmployeeId in SalaryIncrement table we make it the foreign key.
From the above tables (Employee table and SalaryIncrement table), to know the employee name whose salary increased by 25 percent, we can retrieve the record by joining both the tables, so we need to use the joins concept.
JOINS
Joins are used for joining both the tables with the relationship as the primary key in the first table and the foreign key in the second table and retrieving the data from more than one table. We can perform joins on more than one table.
A common column must exist between both the tables. From the above example EmployeeId is the common column from the two tables. With a single Select query we can retrieve the records from more than one table.
Types of JOINS
Different types of Joins in Sql Server are
- Inner join
- Outer Join
- Left outer join
- Right outer join
- Full outer join.
- Cross join.
- Self join
- Equi join
- Non equi join.
Inner join
Inner join retrieves all the rows / Records from both the tables which have matching records in both tables. [OR] Inner join is used to retrieve the matching rows/records from more than one table by joining both the tables by using "Join clause" & "on clause".

From the above figure the highlighted orange color is the matching one from both the tables.
Query for creating tables and Inserting rows.
- create table Employee(EmployeeNo int, EmployeeName varchar(10), EmployeeSalary money)
- Insert into Employee(EmployeeNo, EmployeeName, EmployeeSalary)
- Values(1201, 'khaja', 10, 000), (1202, 'moiz', 20, 000), (1203, 'ajay', 15, 000), (1204, 'waseem', 25, 000)
- create table Department(DepartmentNo int, DepartmentName varchar(15), EmployeeNo int)
- Insert into Department(DepartmentNo, DepartmentName, EmployeeNo)
- values(100, 'IT', 1201), (200, 'Mathematics', 1202), (300, 'Physics', 1203), (400, 'Chemistry', 1205)
| EmployeeNo | EmployeeName | EmployeeSalary |
| 1201 | khaja | 10,000 |
| 1202 | moiz | 20,000 |
| 1203 | ajay | 15,000 |
| 1204 | waseem | 25,000 |
Department Table
| DepartmentNo | DepartmentName | EmployeeNo |
| 100 | IT | 1201 |
| 200 | Mathematics | 1202 |
| 300 | Physics | 1203 |
| 400 | Chemistry | 1205 |
Syntax for Inner Join
Select table1.column1,table1.column2, table1. column3.....,table2, column1,table2.column2,table3.column3,...............from table 1 inner join table 2 on table1.CommonColumn=table2.commonColumn.
Different ways of retrieving records from Employee Table and Department table.
Select Employee.EmployeeNo,Employee.EmployeeName,Employee.EmployeeSalary,Department.DepartmentNo,
Department.DepartmentName from Employee inner join Department on Employee.EmployeeNo=Department.EmployeeNo
(OR)
Select Employee.*,Department.DepartmentNo,Department.DepartmentName from Employee inner join Department on Employee.EmployeeNo=Department.EmployeeNo
Instead of retrieving the data with the help of table names we can use alias names like for employee table as (e) and department table as (d), we can retrieve the records by using their alias names as e and d as shown below.
Select e.EmployeeNo,e.EmployeeName,e.EmployeeSalary,d.DepartmentNo,d.DepartmentName from Employee e inner join Department d on e.EmployeeNo=d.EmployeeNo
(OR)
Select e.*,d.DepartmentNo,d.DepartmentName from Employee e inner join Department d on e.EmployeeNo=d.EmployeeNo
In the above Select statement , instead of inner join we can directly use join clause as joins are by default inner join.
Example
Select Employee.EmployeeNo,Employee.EmployeeName,Employee.EmployeeSalary,Department.DepartmentNo,
Department.DepartmentName from Employee join Department on Employee.EmployeeNo=Department.EmployeeNo
(OR)
Select Employee.*,Department.DepartmentNo,Department.DepartmentName from Employee join Department on Employee.EmployeeNo=Department.EmployeeNo
(OR)
Select e.EmployeeNo,e.EmployeeName,e.EmployeeSalary,d.DepartmentNo,d.DepartmentName from Employee e join Department d one.EmployeeNo=d.EmployeeNo
(OR)
Select e.*,d.DepartmentNo,d.DepartmentName from Employee e join Department d on e.EmployeeNo=d.EmployeeNo
The result of the above query is
| 1201 | khaja | 10,000 | IT |
| 1202 | moiz | 20,000 | Mathematics |
| 1203 | ajay | 15,000 | Physics |
The above result shows the records which are common in both the tables.
OUTER JOIN




Khaja MoizuddinPosted Feb 7, 2019, 10:27 PM
Thanks Farhan hope it helped you.
Farhan AhmedPosted Jul 24, 2018, 9:48 AM
I find it useful...
Khaja MoizuddinPosted May 3, 2017, 11:47 PM
Thanks Subash foryour valuable feedback......
SubashPosted May 3, 2017, 10:01 PM
Very simple explanation which makes easy to understand by one
Khaja MoizuddinPosted Jan 8, 2017, 2:49 AM
Thanks Humayun............... :)
Humayun Kabir MamunPosted Jan 8, 2017, 2:33 AM
Nice article about SQL Joins...
Khaja MoizuddinPosted Jan 4, 2017, 12:52 PM
Thanks Anu .....................
Anu VPosted Jan 1, 2017, 11:24 PM
Nice article.. Thanks for sharing..
Khaja MoizuddinPosted Dec 20, 2016, 11:24 AM
Thanks Manav & Subhashkumar.
Subhashkumar YadavPosted Dec 20, 2016, 6:09 AM
Nicely written. Very easy to understand.
Manav PandyaPosted Dec 20, 2016, 4:45 AM
Thanks for sharing this one Khaja Moizuddin