Joins in SQL Server

SQL Server Joins are used to combine records from two or more tables based on common fields between them.

1. SQL Server INNER JOIN statement

  • SQL server INNER JOIN statement selects all possible rows from both tables, if there is a match between the columns in both tables.
  • The data is not displayed, if there is no match between columns in both tables.

Syntax

SELECT column_name
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name


Example: INNER JOIN in SQL server.

Consider the following two tables, 'Employee' and 'Salary'.

Table 1: Employee

Emp_IDEmp_NameEmp_Phone
1Marten9022222222
2Paul9033333333
3Albert9044444444
4Steve9055555555

Table 2: Salary

Emp_IDTransaction_IDAmount
110110000
210220000
310325000
410430000
510530000
610650000

The column 'Emp_ID' is common to both tables. So the query for INNER JOIN should be written as:

SELECT Employee.Emp_Name, Salary.Transaction_ID, Salary.Amount
FROM Employee
INNER JOIN Salary
ON Salary.Emp_ID = Employee.EMP_ID


The result is shown in the following table.

Emp_NameTransaction_IDAmount
Marten10110000
Paul10220000
Albert10325000
Steve10530000

2. SQL Server LEFT JOIN

  • SQL Server LEFT JOIN or LEFT OUTER JOIN keyword returns all rows from the left table (table1) with the matching rows in the right table (table2).
  • If there is no match found, the result displayed is NULL.

Syntax

SELECT column_name
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name


Example: LEFT JOIN statement in SQL server.

Consider the following two tables 'Employee' and 'Salary'.

Table 1: Employee

Emp_IDEmp_NameEmp_Phone
1Marten9022222222
2Paul9033333333
3Albert9044444444
4Steve9055555555

Table2: Salary

Emp_IDTransaction_IDAmount
110110000
210220000
310325000
410430000
510530000
610650000

To perform LEFT JOIN on table (Salary), query should be written as:

SELECT Employee.Emp_Name, Employee.Emp_Phone, Salary.Amount
FROM Employee
LEFT JOIN Salary
ON Salary.Emp_ID = Employee.Emp_ID


The output will be:

Emp_NameEmp_PhoneAmount
Marten902222222210000
Paul903333333320000
Albert904444444425000
Steve905555555530000

3. SQL Server RIGHT JOIN

  • The SQL server RIGHT JOIN or RIGHT OUTER JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1).
  • If there is no match, the result in the left table is null.

Syntax

SELECT Column_name
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2_column_name


Example: RIGHT JOIN Statement in SQL server.

Consider the following two tables titled  'Employee', 'Salary'.

Table: Employee

Emp_IDEmp_NameEmp_Phone
1Marten9022222222
2Paul9033333333
3Albert9044444444
4Steve9055555555
5Smith9066666666
6Jhon9011111111

Table: 'Salary'

Emp_IDTransaction_IDAmount
110110000
210220000
310325000
410430000
510530000

To perform RIGHT JOIN ON table (Employee), query should be written as:

SELECT  Employee.Emp_Name, Salary.Amount
FROM Salary
RIGHT JOIN Employee
ON Salary.Emp_ID = Employee.Emp_ID


The result is shown in the following table.

Emp_NameAmount
Martin10000
Paul20000
Albert25000
Steve30000
Smith30000
Jhon

4. SQL Server FULL JOIN

The SQL server FULL JOIN or FULL JOIN statement returns all rows from the left table (table1) and the right table (table2).

Syntax

SELECT column_name
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name


Example: FULL JOIN Statement in SQL server.

Consider the following tables

Table 1: Empoyee

Emp_IDEmp_NameEmp_Phone
1Marten9022222222
2Paul9033333333
3Albert9044444444
4Steve9055555555
5Smith9066666666
6Jhon9011111111

Table 2: Salary

Emp_IDTransaction_IDAmount
110110000
210220000
310325000
410430000

To perform RIGHT JOIN, query should be written as:

SELECT Employee.Emp_ID, Employee.Emp_Name, Salary.Transaction_ID, Salary.Amount
FROM Employee
FULL JOIN Salary
ON Salary.Emp_ID = Employee.Emp_ID


The result is shown in the following table.

Emp_IDEmp_NameTransaction_IDAmount
1Marten10110000
2Paul10220000
3Albert10325000
4Steve10430000
5 Smith
6Jhon

5. SQL Server CROSS JOIN

  • The SQL server CROSS JOIN returns the cartesian product of rows from joined table.
  • A cartesian join is a join of all rows of table1 to all rows of table2.
Syntax:
SELECT * FROM table1 CROSS JOIN table2;

Example: Write a query to perform CROSS JOIN on table1 and table2.

Table1 : Students

Stud_IDStud_NameSubjectDept_ID
1RaviC01
2SapnaCPP02
3PrajaktaJAVA03

Table2 : Department

Dept_IDDept_Name
01Comp_Science
02IT
03MCA

SELECT Std_Id, Stud_Name, Subject, Dept_Name
FROM students CROSS JOIN Department;


Stud_IDStud_NameSubjectDept_Name
1RaviCComp_Science
2SapnaCPPComp_Science
3PrajaktaJAVAComp_Science
1RaviCIT
2SapnaCPPIT
3PrajaktaJAVAIT
1RaviCMCA
2SapnaCPPMCA
3PrajaktaJAVAMCA