(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_id | name |
---|---|
1 | Olivia |
2 | Mason |
3 | Zoe |
🛠️ projects
table:
project_id | emp_id | project_name |
---|---|---|
201 | 1 | Website |
202 | 2 | App |
203 | 4 | Database |
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:
name | project_name |
---|---|
Olivia | Website |
Mason | App |
NULL | Database |
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
Type | Who’s Shown |
---|---|
LEFT JOIN | All rows from the left, matches from the right |
RIGHT JOIN | All 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