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.
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:
- INNER JOIN: Returns records with matching values in both tables.
- 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.
- RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table and matched records from the left table.
- 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:
- `students` table:
| student_id | student_name | ||||
| 1 | Alice | ||||
| 2 | Bob | ||||
| 3 | Charlie | 2. `enrollments` table: | enrollment_id | student_id | course_id |
| --- | --- | --- | --- | --- | --- |
| 101 | 1 | 201 | |||
| 102 | 1 | 202 | |||
| 103 | 2 | 203 | 3. `courses` table: | course_id | course_name |
| --- | --- | --- | --- | --- | --- |
| 201 | Mathematics | ||||
| 202 | Science | ||||
| 203 | Literature | 4. `grades` table: | grade_id | enrollment_id | grade |
| --- | --- | --- | --- | --- | --- |
| 301 | 101 | A | |||
| 302 | 102 | B | |||
| 303 | 103 | C |
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
- Joins are for lazy people?
- JPA and Hibernate - Criteria vs. JPQL or HQL
- JPA EntityManager Why use persist() over merge()?
- JPA OneToMany not deleting child
- JPA or JDBC, how are they different?
- JpaRepository Not supported for DML operations delete query
- jQuery UI Sortable, then write order into a database
- JSON encode MySQL results

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.