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.
-
Add a new column named
manager_idto theemployeestable:mysql> ALTER TABLE employees ADD COLUMN manager_id INT NULL; -
Create a relationship between the
idcolumn and themanager_idcolumn:mysql> ALTER TABLE employees ADD CONSTRAINT fk_manager FOREIGN KEY (manager_id) REFERENCES employees(id); -
Update the records of the
employeestable:mysql> UPDATE employees SET manager_id = 9 WHERE id = 2;