Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

FULL OUTER JOIN

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

When a matching record exists, data from both tables is displayed. When there is no matching record, NULL values are returned for the missing side.

What is FULL OUTER JOIN?

A FULL OUTER JOIN combines the behavior of LEFT JOIN and RIGHT JOIN.

It returns:

  • All matching records from both tables.
  • All non-matching records from the left table.
  • All non-matching records from the right table.

For a missing match, the columns belonging to the table without a matching row contain NULL.

FULL OUTER JOIN Syntax

SQL
SELECT columns
FROM Table1
FULL OUTER JOIN Table2
    ON Table1.CommonColumn = Table2.CommonColumn;

Example Tables

Students Table

StudentId StudentName CourseId
101 Rahul 1
102 Priya 2
103 Amit 1
104 Neha 3
105 Rohan 5

Courses Table

CourseId CourseName
1 .NET Full Stack
2 Java Full Stack
3 Python Full Stack
4 Software Testing

Basic FULL OUTER JOIN Example

We can combine Students and Courses using CourseId.

SQL
SELECT
    Students.StudentName,
    Courses.CourseName
FROM Students
FULL OUTER JOIN Courses
    ON Students.CourseId = Courses.CourseId;

Result

FULL OUTER JOIN returns matching records as well as unmatched records from both tables.

StudentName CourseName
Rahul .NET Full Stack
Amit .NET Full Stack
Priya Java Full Stack
Neha Python Full Stack
Rohan NULL
NULL Software Testing
Notice: Rohan has no matching CourseId, so CourseName is NULL. Software Testing has no matching student, so StudentName is NULL.

Matching Records

When CourseId exists in both tables, the values from both tables are combined into one result row.

SQL
Students.CourseId = Courses.CourseId

For example, CourseId 1 exists in both tables, so Rahul and Amit can be matched with .NET Full Stack.

Non-Matching Records from Left Table

FULL OUTER JOIN also keeps records from the left table that do not have a matching record in the right table.

SQL
SELECT
    S.StudentName,
    C.CourseName
FROM Students AS S
FULL OUTER JOIN Courses AS C
    ON S.CourseId = C.CourseId
WHERE C.CourseId IS NULL;

This can be used to identify students whose CourseId does not exist in the Courses table.

Non-Matching Records from Right Table

FULL OUTER JOIN can also be used to identify records from the right table that have no matching record in the left table.

SQL
SELECT
    S.StudentName,
    C.CourseName
FROM Students AS S
FULL OUTER JOIN Courses AS C
    ON S.CourseId = C.CourseId
WHERE S.StudentId IS NULL;

This returns courses that do not have a matching student.

FULL OUTER JOIN using Aliases

SQL
SELECT
    S.StudentName,
    C.CourseName
FROM Students AS S
FULL OUTER JOIN Courses AS C
    ON S.CourseId = C.CourseId;

Aliases make the query shorter and easier to read.

FULL OUTER JOIN with WHERE

We can use WHERE conditions to filter the result of a FULL OUTER JOIN.

SQL
SELECT
    S.StudentName,
    C.CourseName
FROM Students AS S
FULL OUTER JOIN Courses AS C
    ON S.CourseId = C.CourseId
WHERE S.StudentName IS NOT NULL
   OR C.CourseName IS NOT NULL;

JOIN Comparison

JOIN Records Returned
INNER JOIN Matching records only.
LEFT JOIN All left records + matching right records.
RIGHT JOIN All right records + matching left records.
FULL OUTER JOIN All records from both tables.

Important Points

  • FULL OUTER JOIN returns all records from both tables.
  • Matching records are combined.
  • Non-matching records are also included.
  • NULL values appear for the side where no matching record exists.
  • The ON clause defines how the tables are related.

Summary

FULL OUTER JOIN returns all matching and non-matching records from both tables. It is useful when we need complete information from both sides of a relationship, including records that do not have a match.