MySQL
SQL
database
table join
SQL tutorial

Joining three tables using MySQL

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

MySQL is a powerful relational database management system widely used for managing and manipulating data in databases. One common data manipulation task is joining tables to combine data from multiple sources. This article explores how to join three tables using MySQL, providing technical explanations, examples, and important considerations.

Understanding SQL Joins

SQL joins are used to combine rows from two or more tables based on a related column. The main types of joins in MySQL are:

  1. INNER JOIN: Returns records with matching values in both tables.
  2. LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table and matched records from the right table. Returns `NULL` where there is no match.
  3. RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table and matched records from the left table.
  4. FULL JOIN (or FULL OUTER JOIN): Returns all records when there is a match in either left or right table records. MySQL does not support this natively but can be simulated.

For joining three tables, the most common and straightforward type is the INNER JOIN.

Example Databases

Consider the following three tables representing information about students, courses they take, and their grades:

  1. `students` table:
student_idstudent_name
1Alice
2Bob
3Charlie2. `enrollments` table:enrollment_idstudent_idcourse_id
------------------
1011201
1021202
10322033. `courses` table:course_idcourse_name
------------------
201Mathematics
202Science
203Literature4. `grades` table:grade_idenrollment_idgrade
------------------
301101A
302102B
303103C

Joining Three Tables

To retrieve a list of students, the courses they are enrolled in, and their grades, you can join these tables using the following SQL query:

Step 1: Start by listing the columns you want in the `SELECT` statement. • Step 2: Use `INNER JOIN` to combine the `students` table with the `enrollments` table based on the `student_id`. • Step 3: Follow this by joining the `courses` table via the `course_id`. • Step 4: Lastly, join the `grades` table using the `enrollment_id`. • Join Order Matters: The sequence of joins can impact both performance and results significantly. • Indexing: Proper indexing on the foreign key columns (`student_id`, `course_id`, and `enrollment_id`) can greatly enhance the performance. • Data Integrity: Ensure that all foreign keys are valid and the relationships between tables are correctly defined. • Nullable Values: Consider how null values in joined tables affect the result, particularly when using OUTER JOINS. • Using Aliases: Using table aliases (like `s` for `students`) in queries can make the SQL more concise and readable. • Subqueries and Common Table Expressions (CTEs): For complex queries, consider using subqueries or CTEs to simplify the main query logic. • Testing and Validation: Always validate the results of the join queries against the raw data to ensure accuracy.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.