Premium lesson

Retrieving Records From Multiple Tables With Joins in MySQL

MySQL·3 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;
icon light bulb key

Unlock the Build RESTful APIs in Node.js module

Learn how to build real database-backed and framework-powered RESTful APIs with MySQL, Sequelize, and Express.

You get immediate access to:

  • 37 focused lessons across MySQL, Sequelize, backend, and Express
  • 4 real-world projects with commented solutions
  • Ongoing updates to this bundle
  • Lifetime access to this bundle
Unlock this module