You are currently viewing SQL 1.26 SQL RIGHT JOIN


Absolutely, let’s delve into SQL RIGHT JOIN. SQL, or Structured Query Language, is a powerful tool for managing and manipulating data in relational databases. The RIGHT JOIN is one of the different types of JOIN operations used to combine rows from two or more tables based on a related column between them. Here’s a detailed tutorial on SQL RIGHT JOIN:

1. Understanding SQL RIGHT JOIN

  • SQL RIGHT JOIN returns all rows from the right table (table2), and the matched rows from the left table (table1). The result is NULL from the left side, when there is no match.

2. Syntax of SQL RIGHT JOIN

   SELECT column_name(s)
   FROM table1
   RIGHT JOIN table2
   ON table1.column_name = table2.column_name;

3. Example Scenario:

  • Let’s say we have two tables: employees and departments.
    • employees table has columns: emp_id, emp_name, and dept_id.
    • departments table has columns: dept_id and dept_name.

4. Sample SQL RIGHT JOIN Query:

   SELECT employees.emp_id, employees.emp_name, departments.dept_name
   FROM employees
   RIGHT JOIN departments
   ON employees.dept_id = departments.dept_id;

5. Step-by-Step Explanation:

  • SELECT employees.emp_id, employees.emp_name, departments.dept_name:
    • We specify the columns we want to retrieve from both tables (employees and departments).
  • FROM employees RIGHT JOIN departments:
    • We specify the tables we want to join, with employees being the left table and departments being the right table.
  • ON employees.dept_id = departments.dept_id:
    • We define the condition for the join, which is the equality between the dept_id column in employees table and the dept_id column in departments table.

6. Output Explanation:

  • The output of this query will contain all rows from the departments table, and the matching rows from the employees table. If there is no matching row in the employees table for a department, the columns from the employees table will contain NULL values.

7. Sample Output:

   | emp_id | emp_name   | dept_name   |
   |   101  | John Doe   | HR          |
   |   102  | Jane Smith | IT          |
   |   NULL | NULL       | Finance     |
  • In this output, you can see that there are two employees from HR and IT departments, and one department (Finance) without any employees. Hence, the values for emp_id and emp_name for Finance department are NULL.


  • SQL RIGHT JOIN is useful when you want to retrieve all rows from the right table along with matching rows from the left table. It’s particularly handy when you want to ensure that you get all rows from a certain table, even if there are no matches in the other table.

By following this tutorial, you should have a good understanding of how SQL RIGHT JOIN works and how to use it in your database queries. Feel free to experiment with different tables and conditions to further solidify your understanding.

Leave a Reply