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

RIGHT JOIN

RIGHT JOIN returns all records from the right table and the matching records from the left table.

If there is no matching record in the left table, the columns from the left table contain NULL values.

What is RIGHT JOIN?

A RIGHT JOIN returns every row from the right table, regardless of whether a matching row exists in the left table.

When a matching record is found, information from both tables is returned. When no match is found, the columns from the left table contain NULL.

RIGHT JOIN Syntax

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

Example Tables

Students Table — Left Table

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

Courses Table — Right Table

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

Basic RIGHT JOIN Example

Here, Students is the left table and Courses is the right table.

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

Result

Because Courses is the right table, all courses are returned, including courses that do not have a matching student.

StudentName CourseName
Rahul .NET Full Stack
Amit .NET Full Stack
Priya Java Full Stack
Neha Python Full Stack
NULL Software Testing
Notice: Software Testing has CourseId 4, but no student has CourseId 4. Because Courses is the right table, the course is still included and StudentName becomes NULL.

RIGHT JOIN with WHERE

RIGHT JOIN can be combined with a WHERE clause to filter the returned records.

SQL
SELECT
    S.StudentName,
    C.CourseName
FROM Students AS S
RIGHT JOIN Courses AS C
    ON S.CourseId = C.CourseId
WHERE C.CourseName LIKE '%Full Stack';

This returns courses whose names contain Full Stack.

Finding Records Without a Match

RIGHT JOIN can be used with IS NULL to find records in the right table that do not have a matching record in the left table.

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

This returns courses that do not have any matching student.

RIGHT JOIN using Aliases

Table aliases make RIGHT JOIN queries shorter and easier to read.

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

RIGHT JOIN vs LEFT JOIN

LEFT JOIN RIGHT JOIN
Returns all records from the left table. Returns all records from the right table.
Non-matching right-side values become NULL. Non-matching left-side values become NULL.
Left table is given priority. Right table is given priority.

RIGHT JOIN and LEFT JOIN

A RIGHT JOIN can often be rewritten as a LEFT JOIN by reversing the order of the tables.

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

The same result can generally be expressed by placing Courses first and using LEFT JOIN.

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

Important Points

  • RIGHT JOIN returns every record from the right table.
  • Matching records from the left table are included.
  • If there is no match, left-table columns contain NULL.
  • The ON clause defines the relationship between the tables.
  • RIGHT JOIN can be rewritten as a LEFT JOIN by reversing the table order.

Summary

RIGHT JOIN returns all records from the right table and matching records from the left table. When a matching record is not available in the left table, NULL values are returned for the left-table columns.