MySQL Joins Overview

MySQL Joins are used to retrieve data from two or more tables based on a related column. They are essential for combining records and understanding relationships between datasets.

Key Topics

Tables Setup for Joins

Let's create two tables, tamil_kings and kingdoms, which we will use in our join examples.

1. Creating the tamil_kings Table

CREATE TABLE tamil_kings (
    king_id INT PRIMARY KEY,
    king_name VARCHAR(100),
    reign_start YEAR,
    reign_end YEAR,
    kingdom_id INT
);

Inserting Data into tamil_kings

INSERT INTO tamil_kings (king_id, king_name, reign_start, reign_end, kingdom_id) VALUES
(1, 'Raja Raja Chola', 985, 1014, 1),
(2, 'Rajendra Chola', 1014, 1044, 1),
(3, 'Vijayalaya Chola', 850, 871, 2),
(4, 'Parantaka Chola I', 907, 955, 2),
(5, 'Kulothunga Chola I', 1070, 1122, 3);

2. Creating the kingdoms Table

CREATE TABLE kingdoms (
    kingdom_id INT PRIMARY KEY,
    kingdom_name VARCHAR(100),
    region VARCHAR(100)
);

Inserting Data into kingdoms

INSERT INTO kingdoms (kingdom_id, kingdom_name, region) VALUES
(1, 'Chola Dynasty', 'South India'),
(2, 'Pandya Dynasty', 'South India'),
(3, 'Chera Dynasty', 'South India'),
(4, 'Pallava Dynasty', 'South India');

Key Takeaways

  • These tables will be used in our join examples to demonstrate how data can be combined based on relationships.
  • Make sure to set up these tables before running the join queries.