MySQL RIGHT JOIN
The RIGHT JOIN
keyword returns all records from the right table (kingdoms), and the matched records from the left table (tamil_kings). If there is no match, NULL values are returned for columns from the left table.
Example: Using RIGHT JOIN
SELECT tamil_kings.king_name, kingdoms.kingdom_name
FROM tamil_kings
RIGHT JOIN kingdoms ON tamil_kings.kingdom_id = kingdoms.kingdom_id;
Code Explanation: This query retrieves all kingdoms from the kingdoms
table and the corresponding kings. If a kingdom does not have any associated king, the king_name
will be NULL.
Key Takeaways
- Use
RIGHT JOIN
to include all records from the right table, even if there are no matches in the left table. - Useful for cases where you want to include all records from one table and only matching records from the other.