Paramclasses Academy - Blog

Tutorials Details

←
Post 17 of 40
→

SQL Joins Explained in SQL Server: INNER, LEFT, RIGHT and FULL OUTER JOIN with Examples

SQL Joins Explained in SQL Server: INNER, LEFT, RIGHT and FULL OUTER JOIN with Examples

November 17, 2026

SQL Joins Explained in SQL Server: INNER, LEFT, RIGHT and FULL OUTER JOIN with Examples

In Microsoft SQL Server, data is often stored in multiple related tables instead of keeping everything in one large table. For example, a Student Management System may store student information in one table and course information in another table. To retrieve related information from both tables, SQL Server provides JOIN operations.

A SQL JOIN is used to combine rows from two or more tables based on a related column between them. JOINs help developers retrieve meaningful information from relational databases without duplicating data unnecessarily.

In this tutorial by Param Classes, you will learn the four commonly used SQL JOIN types: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. We will understand their differences through table examples, SQL queries, expected results, and practical database scenarios.

1. What Is a JOIN in SQL Server?

A JOIN combines related records from two or more tables using a matching column or join condition.

For example, consider two tables:

Students Table

StudentID StudentName CourseID
1 Rahul 101
2 Priya 102
3 Amit 103
4 Neha 104

Courses Table

CourseID CourseName
101 SQL Server
102 Python
103 Web Development
105 Data Science

Both tables contain a common column named CourseID.

We can use this column to connect student records with their corresponding course information.

Basic JOIN Syntax

SELECT
    table1.column_name,
    table2.column_name
FROM table1
JOIN table2
ON table1.common_column = table2.common_column;

Explanation:

  • SELECT specifies the columns to retrieve.

  • FROM identifies the first table.

  • JOIN specifies the second table.

  • ON defines the condition used to match records.

2. Create Sample Tables for JOIN Examples

Before learning the different JOIN types, let's create two sample tables in SQL Server.

Step 1: Create the Students Table

CREATE TABLE Students
(
    StudentID INT PRIMARY KEY,
    StudentName VARCHAR(50),
    CourseID INT
);

Step 2: Insert Student Records

INSERT INTO Students
(StudentID, StudentName, CourseID)
VALUES
(1, 'Rahul', 101),
(2, 'Priya', 102),
(3, 'Amit', 103),
(4, 'Neha', 104);

Step 3: Create the Courses Table

CREATE TABLE Courses
(
    CourseID INT PRIMARY KEY,
    CourseName VARCHAR(100)
);

Step 4: Insert Course Records

INSERT INTO Courses
(CourseID, CourseName)
VALUES
(101, 'SQL Server'),
(102, 'Python'),
(103, 'Web Development'),
(105, 'Data Science');

Step 5: View Both Tables

SELECT * FROM Students;

SELECT * FROM Courses;

Notice these important details:

  • Student records contain CourseID values 101, 102, 103, and 104.

  • Course records contain CourseID values 101, 102, 103, and 105.

  • CourseID 104 exists only in Students.

  • CourseID 105 exists only in Courses.

These unmatched records will help us understand how different JOIN operations work.

3. INNER JOIN in SQL Server

INNER JOIN returns only those records that have matching values in both tables according to the join condition.

If a record does not have a matching value in the other table, it will not appear in the result.

Syntax

SELECT
    table1.column_name,
    table2.column_name
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;

Example: Get Students with Their Course Names

SELECT
    Students.StudentID,
    Students.StudentName,
    Courses.CourseName
FROM Students
INNER JOIN Courses
ON Students.CourseID = Courses.CourseID;

Expected Output

StudentID StudentName CourseName
1 Rahul SQL Server
2 Priya Python
3 Amit Web Development

Explanation:

SQL Server compares CourseID in both tables and returns only matching records.

  • Rahul matches CourseID 101.

  • Priya matches CourseID 102.

  • Amit matches CourseID 103.

  • Neha is excluded because CourseID 104 is not present in Courses.

Important: INNER JOIN returns only matching records from both tables.

Real-World Use Cases of INNER JOIN

  • Display students with their enrolled courses.

  • Show orders with valid customer information.

  • Retrieve employees with matching department records.

  • Display products with their corresponding category details.

4. LEFT JOIN in SQL Server

LEFT JOIN, also known as LEFT OUTER JOIN, returns all records from the left table and matching records from the right table.

If there is no matching record in the right table, SQL Server displays NULL for the right table's columns.

Syntax

SELECT
    table1.column_name,
    table2.column_name
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;

Example: Get All Students with Their Course Names

SELECT
    Students.StudentID,
    Students.StudentName,
    Students.CourseID,
    Courses.CourseName
FROM Students
LEFT JOIN Courses
ON Students.CourseID = Courses.CourseID;

Expected Output

StudentID StudentName CourseID CourseName
1 Rahul 101 SQL Server
2 Priya 102 Python
3 Amit 103 Web Development
4 Neha 104 NULL

Explanation:

LEFT JOIN keeps every record from the Students table.

  • Rahul, Priya, and Amit have matching courses.

  • Neha also appears because Students is the left table.

  • Since CourseID 104 has no matching course, CourseName is NULL.

Important: LEFT JOIN returns all rows from the left table, even when there is no match in the right table.

Real-World Use Cases of LEFT JOIN

  • Display all students, including those without an assigned course.

  • Show all customers, including customers who have not placed orders.

  • List all employees, including those without department information.

  • Identify products that do not have sales records.

5. RIGHT JOIN in SQL Server

RIGHT JOIN, also known as RIGHT OUTER JOIN, returns all records from the right table and matching records from the left table.

When a record in the right table has no matching record in the left table, the left table's columns contain NULL.

Syntax

SELECT
    table1.column_name,
    table2.column_name
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;

Example: Get All Courses with Their Student Details

SELECT
    Students.StudentID,
    Students.StudentName,
    Courses.CourseID,
    Courses.CourseName
FROM Students
RIGHT JOIN Courses
ON Students.CourseID = Courses.CourseID;

Expected Output

StudentID StudentName CourseID CourseName
1 Rahul 101 SQL Server
2 Priya 102 Python
3 Amit 103 Web Development
NULL NULL 105 Data Science

Explanation:

RIGHT JOIN keeps every record from the Courses table.

  • SQL Server, Python, and Web Development have matching students.

  • Data Science also appears because Courses is the right table.

  • No student has CourseID 105, so the student columns contain NULL.

Important: RIGHT JOIN returns all rows from the right table, whether or not a matching row exists in the left table.

Real-World Use Cases of RIGHT JOIN

  • Display all available courses, including courses without enrolled students.

  • List all departments, including departments without employees.

  • Retrieve all categories, including categories without products.

6. FULL OUTER JOIN in SQL Server

FULL OUTER JOIN returns all matching and non-matching records from both tables.

When a record has no matching row in the other table, SQL Server fills the missing columns with NULL.

Syntax

SELECT
    table1.column_name,
    table2.column_name
FROM table1
FULL OUTER JOIN table2
ON table1.common_column = table2.common_column;

Example: Get All Students and All Courses

SELECT
    Students.StudentID,
    Students.StudentName,
    Students.CourseID AS StudentCourseID,
    Courses.CourseID AS AvailableCourseID,
    Courses.CourseName
FROM Students
FULL OUTER JOIN Courses
ON Students.CourseID = Courses.CourseID;

Expected Output

StudentID StudentName StudentCourseID AvailableCourseID CourseName
1 Rahul 101 101 SQL Server
2 Priya 102 102 Python
3 Amit 103 103 Web Development
4 Neha 104 NULL NULL
NULL NULL NULL 105 Data Science

Explanation:

FULL OUTER JOIN includes:

  • All matching records from both tables.

  • Neha's record, even though CourseID 104 has no matching course.

  • Data Science, even though CourseID 105 has no matching student.

Important: FULL OUTER JOIN returns all records from both tables, including unmatched records.

Real-World Use Cases of FULL OUTER JOIN

  • Compare two datasets and identify missing records.

  • Find customers or orders that do not have matching records.

  • Reconcile data between two systems.

  • Identify unmatched employee and department records.

7. Difference Between INNER, LEFT, RIGHT and FULL OUTER JOIN

Understanding the differences between JOIN types is essential for writing correct SQL queries.

JOIN Type Records Returned
INNER JOIN Only matching records from both tables
LEFT JOIN All left table records and matching right table records
RIGHT JOIN All right table records and matching left table records
FULL OUTER JOIN All records from both tables, matched and unmatched

Quick Memory Trick

  • INNER JOIN: Only matching records.

  • LEFT JOIN: Everything from the left table.

  • RIGHT JOIN: Everything from the right table.

  • FULL OUTER JOIN: Everything from both tables.

8. How to Find Unmatched Records Using LEFT JOIN

One common database task is identifying records that do not have matching information in another table.

For example, to find students who do not have a matching course:

SELECT
    Students.StudentID,
    Students.StudentName,
    Students.CourseID
FROM Students
LEFT JOIN Courses
ON Students.CourseID = Courses.CourseID
WHERE Courses.CourseID IS NULL;

Expected Output:

StudentID StudentName CourseID
4 Neha 104

This query uses LEFT JOIN to preserve all student records and then filters for rows where no matching course was found.

Similarly, to find courses without students:

SELECT
    Courses.CourseID,
    Courses.CourseName
FROM Courses
LEFT JOIN Students
ON Courses.CourseID = Students.CourseID
WHERE Students.StudentID IS NULL;

Expected Output:

CourseID CourseName
105 Data Science

9. Using Table Aliases with JOINs

When working with multiple tables, writing full table names repeatedly can make SQL queries lengthy.

SQL Server allows you to use table aliases to simplify queries.

Example Without Aliases

SELECT
    Students.StudentName,
    Courses.CourseName
FROM Students
INNER JOIN Courses
ON Students.CourseID = Courses.CourseID;

Example With Aliases

SELECT
    S.StudentName,
    C.CourseName
FROM Students AS S
INNER JOIN Courses AS C
ON S.CourseID = C.CourseID;

Explanation:

  • S represents the Students table.

  • C represents the Courses table.

  • Aliases make queries shorter and easier to read.

Tip: Use meaningful aliases when working with multiple tables, especially in large database applications.

10. Joining More Than Two Tables

SQL Server allows you to join multiple tables in a single query.

Suppose you have three tables:

  • Students

  • Courses

  • Instructors

You can retrieve student, course, and instructor information together.

Example

SELECT
    S.StudentName,
    C.CourseName,
    I.InstructorName
FROM Students AS S
INNER JOIN Courses AS C
ON S.CourseID = C.CourseID
INNER JOIN Instructors AS I
ON C.InstructorID = I.InstructorID;

Explanation:

  1. The Students table is joined with Courses.

  2. The Courses table is joined with Instructors.

  3. SQL Server returns the matching student, course, and instructor information.

Note: This example assumes the Courses table contains InstructorID and the Instructors table contains a matching InstructorID.

11. Common Mistakes to Avoid When Using JOINs

  1. Missing ON condition: A missing or incorrect join condition can produce unintended results.

  2. Joining unrelated columns: Always identify the correct relationship between tables.

  3. Using INNER JOIN instead of LEFT JOIN: INNER JOIN removes unmatched left-side records.

  4. Ignoring duplicate results: A one-to-many relationship can return multiple rows for one record in the first table.

  5. Using ambiguous column names: Specify the table name or alias when multiple tables contain the same column name.

  6. Filtering the wrong table: Conditions in WHERE and ON can affect outer join results differently.

  7. Assuming every JOIN returns the same number of rows: The result depends on matching records and table relationships.

12. Practice Questions

Try these SQL queries yourself using the Students and Courses tables.

  1. Write an INNER JOIN query to display StudentName and CourseName.

  2. Write a LEFT JOIN query to display all students, including students without matching courses.

  3. Write a RIGHT JOIN query to display all courses, including courses without enrolled students.

  4. Write a FULL OUTER JOIN query to display all students and all courses.

  5. Find students who do not have a matching course using LEFT JOIN.

  6. Find courses that do not have any matching student.

  7. Rewrite an INNER JOIN query using table aliases.

  8. Explain the difference between LEFT JOIN and RIGHT JOIN with an example.

13. Frequently Asked Questions (FAQs)

Q1. What is a JOIN in SQL Server?

A JOIN combines records from two or more tables based on a related column or join condition.

Q2. What is the difference between INNER JOIN and LEFT JOIN?

INNER JOIN returns only matching records from both tables, whereas LEFT JOIN returns every record from the left table and matching records from the right table.

Q3. What is the difference between LEFT JOIN and RIGHT JOIN?

LEFT JOIN preserves all records from the left table, while RIGHT JOIN preserves all records from the right table.

Q4. What does FULL OUTER JOIN do?

FULL OUTER JOIN returns matching and unmatched records from both tables. Missing matching values appear as NULL.

Q5. Can we join more than two tables in SQL Server?

Yes. You can use multiple JOIN clauses to combine information from several related tables.

Q6. What is the purpose of the ON clause?

The ON clause specifies the condition SQL Server uses to match records between tables.

Q7. Can JOINs return duplicate records?

Yes. When multiple rows match a row in another table, the result may contain multiple rows. This is normal for one-to-many relationships.

Q8. Which JOIN should I use to find unmatched records?

LEFT JOIN with an IS NULL condition is commonly used to find unmatched records from the left table. You can reverse the tables to find unmatched records from the other side.

14. Conclusion

In this tutorial, we learned how SQL JOINs work in Microsoft SQL Server and how they help combine related information stored in multiple database tables.

We explored four important JOIN types: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. We also created sample Students and Courses tables, inserted records, executed practical JOIN queries, identified unmatched records, and used table aliases to write cleaner SQL statements.

Understanding JOINs is an essential skill for database developers because real-world applications often store information across multiple related tables.

At Param Classes, we focus on helping beginners learn SQL Server through structured lessons, practical database examples, and easy-to-understand explanations. Practise these JOIN queries in SQL Server Management Studio (SSMS) to strengthen your database querying skills.

Keep learning SQL Server step by step with Param Classes!

←
Post 17 of 40
→