Retrieving Records From Multiple Tables With Joins in MySQL

MySQL·2 min read·Jan 1, 2025

A relational database consists of multiple related tables linked together by common attributes called relationships.

These relationships can be used to combine the data from multiple tables simultaneously in a single query through a mechanism called a join.

Setting up the database

Before diving into this lesson, you will have to perform some modifications on your database.

  1. Add a new column named manager_id to the employees table:

    mysql> ALTER TABLE employees ADD COLUMN manager_id INT NULL;
  2. Create a relationship between the id column and the manager_id column:

    mysql> ALTER TABLE employees ADD CONSTRAINT fk_manager FOREIGN KEY (manager_id) REFERENCES employees(id);
  3. Update the records of the employees table:

    mysql> UPDATE employees SET manager_id = 9 WHERE id = 2;