MySQL Self Join

A Self Join is a regular join, but the table is joined with itself. It is useful for comparing rows within the same table.

Example: Using Self Join

SELECT A.king_name AS King1, B.king_name AS King2
FROM tamil_kings A, tamil_kings B
WHERE A.reign_end < B.reign_start;

Code Explanation: This query compares kings within the tamil_kings table, retrieving pairs where one king's reign ended before the other king's reign started.

Key Takeaways

  • Use Self Join for comparing rows within the same table.
  • Aliases are important to distinguish between the same table referenced multiple times.