MySQL allows joining multiple tables using the JOIN keyword. This enables combining related data from several tables into a single result set using common columns.
// Syntax for joining multiple tables
SELECT column1, column2
FROM table1
JOIN table2 ON table1.column = table2.column
JOIN table3 ON table2.column = table3.column;
// Sample data from students table
id | name
---------
1 | Alice
2 | Bob
// Sample data from courses table
id | course
-----------
1 | Math
2 | Science
// Mapping students to courses
student_id | course_id
----------------------
1 | 1
2 | 2
// Joining three tables using INNER JOIN
SELECT students.name, courses.course
FROM students
JOIN enrollments ON students.id = enrollments.student_id
JOIN courses ON enrollments.course_id = courses.id;
// Result after joining all tables
name | course
--------------
Alice | Math
Bob | Science
This query starts from students, matches records in enrollments, and finally links to courses, ensuring data consistency across all tables.
ON conditionsteachers and departments