MySQL RIGHT JOIN
Summary: In this chapter, we will learn about MySQL RIGHT JOIN clause and how to apply it to query data from two or more tables.
MySQL RIGHT JOIN Introduction
The MySQL RIGHT JOIN clause allows you to query data from two or more tables. The RIGHT JOIN keyword returns all rows from the right table, with the matching rows in the left table. The result is NULL in the left side when there is no match.
The following statement illustrates the syntax of RIGHT JOIN clause:
1
2
3
4
| SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name; |
In other words, the RIGHT JOIN clause allows to select rows from the both right and left tables that are matched, plus all rows from the right table even there is no match found for them in the left table.
The following Venn diagram helps you visualize how the RIGHT JOIN clause works.
MySQL RIGHT JOIN Example
Let’s take a look at the employees and branches tables in the sample database.
1
2
3
4
5
6
| SELECT e.firstname, e.lastname, e.jobtitle, b.city, b.country FROM employees AS e RIGHT JOIN branches AS b ON e.branchid=b.branchid; |
Above query returns all the rows from the right table (branches), even if there are no matches in the right table (employees).
In this chapter, we have explained the MySQL RIGHT JOIN clause and shown you how to apply it to query data from multiple tables.
MySQL LEFT JOIN MySQL SELF JOIN
MySQL LEFT JOIN MySQL SELF JOIN