➡️ RIGHT JOIN – Show All from the Right, Matches from the Left

(because sometimes you need everything from the other side, even if there’s no match)

🔧 Quick Syntax

SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;

This says:
“Give me all rows from the right table, and match anything from the left where possible.”

👀 Imagine You Have These Two Tables

🧑 employees table:

emp_idname
1Olivia
2Mason
3Zoe

🛠️ projects table:

project_idemp_idproject_name
2011Website
2022App
2034Database

In this case, not all employees are assigned to projects, and one project (id 203) has no matching employee from the employees table.

🔧 RIGHT JOIN Syntax

SELECT employees.name, projects.project_name
FROM employees
RIGHT JOIN projects
ON employees.emp_id = projects.emp_id;

What’s happening here?
You’re telling SQL:
“Show me all projects (from the right table),
and if there’s an employee assigned, show their name.”

✅ Result:

nameproject_name
OliviaWebsite
MasonApp
NULLDatabase

Notice:
Zoe didn’t have any projects, so she’s not shown here.
– The Database project doesn’t have a matching employee, so it’s shown with NULL under the name.

🔁 LEFT JOIN vs RIGHT JOIN

TypeWho’s Shown
LEFT JOINAll rows from the left, matches from the right
RIGHT JOINAll rows from the right, matches from the left

🧃 Recap

  • RIGHT JOIN gives you all rows from the right, even if there’s no match on the left
  • If there’s no matching data from the left, SQL gives you NULL
  • Use RIGHT JOIN when the right table holds the focus, and you want all that data regardless of matches

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top