MySQL LEFT JOIN

The LEFT JOIN keyword returns all records from the left table (tamil_kings), and the matched records from the right table (kingdoms). If there is no match, NULL values are returned for columns from the right table.

Example: Using LEFT JOIN

SELECT tamil_kings.king_name, kingdoms.kingdom_name
FROM tamil_kings
LEFT JOIN kingdoms ON tamil_kings.kingdom_id = kingdoms.kingdom_id;

Code Explanation: This query retrieves all kings from the tamil_kings table and their corresponding kingdoms. If a king does not belong to any kingdom, the kingdom_name will be NULL.

Key Takeaways

  • Use LEFT JOIN to include all records from the left table, even if there are no matches in the right table.
  • Useful for cases where you want to include all records from one table and only matching records from the other.