Joins In SQL Server 2017

Introduction

In this article, I am going to explain about joins and types of joins with examples. Here we will be using SQL Server 2017 or you can use SQL Server 2008 or above.

Definition

Joins are used to fetch/retrieve data from two or more related tables from a database. In general, tables are related to each other using foreign key constraints.

Types of Joins

There are four types of joins in SQL Server.

  • Inner Join
  • Outer Join
  • Cross Join
  • Self Join

Again, Outer Joins are divided into three types.

  • Left Join or Left Outer Join
  • Right Join or Right Outer Join
  • Full Join or Full Outer Join

Now, I am going to explain different types of joins with examples and the differences between them.

Prerequisites

SQL Server 2017 or you can use SQL Server 2008 or above version.

Now, first, we will create a Database and two tables to apply the joins for understanding.

Creating a Database and two tables

Step 1. Create a Database

Open your SQL Server and use the following script to create the “chittadb” Database.

CREATE DATABASE chittadb;

Now, select the script query then press F5 or click on Execute button to execute the above script.

You should see a message, “Command(s) completed successfully.” This means your new database has been created.

Step 2. Create the first table

Open your SQL Server and use the following script to create table “tbl_Department”.

CREATE TABLE tbl_Department  
(  
   DeptId INT PRIMARY KEY NOT NULL IDENTITY(1,1),  
   DeptName NVARCHAR(50),  
   DeptHead NVARCHAR(50),  
   Location NVARCHAR(100)  
)  

Execute the above query to create “tbl_Department “.

You should see a message, “Command(s) completed successfully.”

Now, data is inserted into the table.

INSERT INTO tbl_Department VALUES ('IT', 'Chitta', 'Chennai');
INSERT INTO tbl_Department VALUES ('Payroll', 'Akhil', 'Odisha');
INSERT INTO tbl_Department VALUES ('HR', 'Ram', 'Pune');
INSERT INTO tbl_Department VALUES ('Timesheet', 'Kannan', 'Chennai');

Execute the above query, you should see a message, “Command(s) completed successfully.”

Now retrieve all data from the “tbl_Department” table.

SELECT * FROM tbl_Department;

output

Joins In SQL Server

Step 3. Create the second table

Open your SQL Server and use the following script to create table “tbl_Employee”.

CREATE TABLE tbl_Employee (
    EmpID INT PRIMARY KEY NOT NULL IDENTITY(1,1),
    Name NVARCHAR(50),
    Gender NVARCHAR(50),
    Country NVARCHAR(20),
    Salary INT,
    DepartmentId INT FOREIGN KEY REFERENCES tbl_Department(DeptId)
)

Execute the above query to create “tbl_Employee “.

You should see a message, “Command(s) completed successfully.”

Now, data is inserted into the table.

INSERT INTO tbl_Employee VALUES ('Jitu', 'Male', 'India', 4000, 1);
INSERT INTO tbl_Employee VALUES ('Rani', 'Female', 'India', 5000, 3);
INSERT INTO tbl_Employee VALUES ('Rohit', 'Male', 'India', 5500, 1);
INSERT INTO tbl_Employee VALUES ('Dibas', 'Male', 'India', 6500, 2);
INSERT INTO tbl_Employee VALUES ('Gajendra', 'Male', 'India', 3800, 2);
INSERT INTO tbl_Employee VALUES ('Raja', 'Male', 'India', 9000, 1);
INSERT INTO tbl_Employee VALUES ('Jeni', 'Female', 'India', 5800, 3);
INSERT INTO tbl_Employee VALUES ('Chandin', 'Female', 'India', 8500, 1);
INSERT INTO tbl_Employee VALUES ('pintu', 'Male', 'India', 9500, NULL);
INSERT INTO tbl_Employee VALUES ('Subrat', 'Male', 'India', 9800, NULL);

Execute the above query, you should see a message, “Command(s) completed successfully.”

Now retrieve all data from the “tbl_Employee” table.

SELECT * FROM tbl_Employee;

output

EmpID

General Formula for Joins

SELECT ColumnList (whatever column you want to display)
FROM LeftTableName
JOIN JOIN_TYPE RightTableName
ON JoinCondition

INNER JOIN

Inner join returns only the matching rows between both tables; non-matching rows are eliminated.

Example

Write a query to retrieve Name, Gender, Country, Salary, and DeptName from tbl_Employee and tbl_Department table.

INNER JOIN Query

SELECT Name, Gender, country, Salary, DeptName
FROM tbl_Employee
INNER JOIN tbl_Department
ON tbl_Employee.DepartmentId = tbl_Department.DeptId

OR

SELECT Name, Gender, country, Salary, DeptName
FROM tbl_Employee
JOIN tbl_Department
ON tbl_Employee.DepartmentId = tbl_Department.DeptId

Note. JOIN or INNER JOIN are the same. It's always better to use INNER JOIN.

OR

SELECT emp.Name, emp.Gender, emp.Country, emp.Salary, dept.DeptName
FROM tbl_Employee emp
INNER JOIN tbl_Department dept
ON emp.DepartmentId = dept.DeptId

Output

DeptName

LEFT JOIN or LEFT OUTER JOIN

Left Join or Left Outer Join returns only the matching rows between both tables, plus non-matching rows from the left table.

Example

Write a query to retrieve Name, Gender, Country, Salary and DeptName from tbl_Employee and tbl_Department table.

LEFT JOIN or LEFT OUTER JOINQuery

SELECT Name, Gender, country, Salary, DeptName
FROM tbl_Employee
LEFT OUTER JOIN tbl_Department
ON tbl_Employee.DepartmentId = tbl_Department.DeptId

OR

SELECT Name, Gender, country, Salary, DeptName
FROM tbl_Employee
LEFT JOIN tbl_Department
ON tbl_Employee.DepartmentId = tbl_Department.DeptId

Note. You can use, LEFT JOIN or LEFT OUTER JOIN. OUTER keyword is optional.

OR

SELECT emp.Name, emp.Gender, emp.Country, emp.Salary, dept.DeptName
FROM tbl_Employee emp
LEFT JOIN tbl_Department dept
ON emp.DepartmentId = dept.DeptId

OutPut

Country

RIGHT JOIN or RIGHT OUTER JOIN

Right Join or Right Outer Join returns only the matching rows between both the tables, plus non-matching rows from the right table.

Example

Write a query to retrieve Name, Gender, Country, Salary and DeptName from tbl_Employee and tbl_Department table.

RIGHT JOIN or RIGHT OUTER JOIN Query

SELECT Name, Gender, country, Salary, DeptName
FROM tbl_Employee
RIGHT JOIN tbl_Department
ON tbl_Employee.DepartmentId = tbl_Department.DeptId

OR

SELECT 
    Name,
    Gender,
    Country,
    Salary,
    DeptName
FROM 
    tbl_Employee
RIGHT OUTER JOIN 
    tbl_Department
ON 
    tbl_Employee.DepartmentId = tbl_Department.DeptId

Note. You can use, RIGHT JOIN or RIGHT OUTER JOIN. The OUTER keyword is optional.

OR

SELECT emp.Name, emp.Gender, emp.country, emp.Salary, dept.DeptName
FROM tbl_Employee emp
RIGHT JOIN tbl_Department dept
ON emp.DepartmentId = dept.DeptId

OutPut

Salary

FULL JOIN or FULL OUTER JOIN

Full Join or Full Outer Join returns all rows from both tables (left & right tables), including non-matching rows from both the tables.

Example

Write a query to retrieve Name, Gender, Country, Salary and DeptName from tbl_Employee and tbl_Department table.

FULL JOIN or FULL OUTER JOIN Query

SELECT 
    Name, 
    Gender,
    country, 
    Salary, 
    DeptName
FROM 
    tbl_Employee
FULL OUTER JOIN 
    tbl_Department
ON 
    tbl_Employee.DepartmentId = tbl_Department.DeptId

OR

SELECT 
    Name, 
    Gender, 
    Country, 
    Salary, 
    DeptName
FROM 
    tbl_Employee
FULL JOIN 
    tbl_Department
ON 
    tbl_Employee.DepartmentId = tbl_Department.DeptId

Note. You can use, FULL JOIN or FULL OUTER JOIN. OUTER keyword is optional.

OR

SELECT emp.Name, emp.Gender, emp.country, emp.Salary, dept.DeptName
FROM tbl_Employee emp
FULL JOIN tbl_Department dept
ON emp.DepartmentId = dept.DeptId

OutPut

Output

CROSS JOIN

CROSS JOIN, produces the Cartesian product of the 2 tables.

For example, in the tbl_Employee table we have 10 rows and in the tbl_Department table we have 4 rows. So, a cross join between these 2 tables produces 40 rows. Cross Join shouldn't have ON clause.

Example

Write a query, to retrieve Name, Gender, Country, Salary and DeptName from tbl_Employee and tbl_Department table.

CROSS JOIN Query

SELECT Name, Gender, country, Salary, DeptName
FROM tbl_Employee
CROSS JOIN tbl_Department;

OR

SELECT emp.Name, emp.Gender, emp.country, emp.Salary, dept.DeptName
FROM tbl_Employee emp
CROSS JOIN tbl_Department dept

OutPut

SQL Server

Conclusion

In this article, I explained joins in SQL Server with examples. I hope this article has helped you to understand this topic. Post your valuable feedback in the comments section.


Similar Articles