MySQL Join 3 Tables

Joining three tables in a single SQL query can be very tricky if you are not good with the concept of SQL Join.

SQL Joins have always been tricky not only for new programmers but for many others,  who are in programming and SQL for more than 2 to 3 years.

There are enough to confuse someone on SQL JOIN ranging from various types of SQL JOIN like INNER JOIN and OUTER JOIN, LEFT JOIN and RIGHT outer JOIN, CROSS JOIN, etc.

Between all of these fundamentals, What is most important about Join is, combining multiple tables. If you need data from multiple tables in one SELECT query you need to use either subquery or JOIN.

Important Points To Remember

To join 3 tables, You should know the following points –

  • All the 3 tables should be in a relationship with a foreign key(FK).
  • Each table must have a common column.
  • The common column must contain the matching values.
  • A common column of the first tables must have a primary key and another common column of the second tables must have a foreign key.
  • datatype & name of the common column may be the same or different.
  • All 3 tables contain the same or the different numbers of rows or columns with some values.

Three table JOIN syntax in SQL

SELECT t1.col, t3.col
FROM table1
JOIN table2 ON table1.primarykey = table2.foreignkey
JOIN table3 ON table2.primarykey = table3.foreignkey

Example

To understand SQL join Query for 3 tables. You have to see an example. So, Let’s discuss it with the three tables like studentscontactsuniversities. Each table is related to each other with the Foreign keys.

Now, Lets see the structure of the following tables closely.

The students table will be joined to universities table based on a common column  university_id.

The contacts table will be joined to students table based on a common column  student_id .

INNER JOIN 3 Tables

If you use the INNER JOIN keyword with the select statement, It will return a new result table by joining the above three tables with the matching records of each table.

Syntax:

You can join 3 tables using the following INNER JOIN syntax –

SELECT table1.column1_name, table1.column2_name,..., 
       table2.column1_name, table2.column2_name,..., 
       table3.column1_name, table3.column2_name,..., 
FROM table1
INNER JOIN table2 ON table1.table1_id = table2.table1_id 
INNER JOIN table3 ON table2.table2_id  = table3.table2_id;

Query for the above tables–

SELECT students.full_name, students.gender, contacts.mobile_number, contacts.email_address, universities.university_name
FROM universities
INNER JOIN students ON universities.university_id = students.university_id
INNER JOIN contacts ON students.student_id = contacts.student_id;

Output –

full_namegendermobile_numberemail_addressuniversity_name
Noor KhanMale2222222222aaaa222@gmail.comSRM University
Avneesh MishraMale5555555555bbbb555@gmail.comAmity University
Monika SinghFemale7777777777cccc777@gmail.comAnna University
Aaliya KhanFemale8888888888dddd888@gmail.comHindustan University
Aaliya KhanFemale1010101010eeee101@gmail.comHindustan University
Aaliya KhanFemale1212121212ffff121@gmail.comHindustan University
Avneesh MishraMale1313131313gggg131@gmail.comAmity University
Avneesh MishraMale1414141414iiii141@gmail.comAmity University

LEFT JOIN 3 Tables

If you use the LEFT JOIN keyword with the select statement, It will return a new result table by joining the above three tables with all the records of left the table and matching values of the right tables.

Syntax

You can join 3 tables using the following INNER JOIN syntax –

SELECT table1.column1_name, table1.column2_name,..., 
       table2.column1_name, table2.column2_name,..., 
       table3.column1_name, table3.column2_name,..., 
FROM table1
LEFT JOIN table2 ON table1.table1_id = table2.table1_id 
LEFT JOIN table3 ON table2.table2_id  = table3.table2_id;

Example Query 

SELECT students.full_name, students.gender, contacts.mobile_number, contacts.email_address, universities.university_name
FROM universities
LEFT JOIN students ON universities.university_id = students.university_id
LEFT JOIN contacts ON students.student_id = contacts.student_id;

Output 

full_namegendermobile_numberemail_addressuniversity_name
Noor KhanMale2222222222aaaa222@gmail.comSRM University
Avneesh MishraMale5555555555bbbb555@gmail.comAmity University
Monika SinghFemale7777777777cccc777@gmail.comAnna University
Aaliya KhanFemale8888888888dddd888@gmail.comHindustan University
Aaliya KhanFemale1010101010eeee101@gmail.comHindustan University
Aaliya KhanFemale1212121212ffff121@gmail.comHindustan University
Avneesh MishraMale1313131313gggg131@gmail.comAmity University
Avneesh MishraMale1414141414iii141@gmail.comAmity University
Sunil KumarMaleNULLNULLHindustan University
Mamta GuptaFemaleNULLNULLHindustan University
Rapson JaniMaleNULLNULLSRM University
Kundan BhartiMaleNULLNULLSRM University

RIGHT JOIN 3 Tables

If you use the RIGHT JOIN keyword with the select statement, It will return a new result table by joining the above three tables with all the records of right the table and matching values of the left tables.

Syntax 

You can join 3 tables using the following RIGHT JOIN syntax –

SELECT table1.column1_name, table1.column2_name,..., 
       table2.column1_name, table2.column2_name,..., 
       table3.column1_name, table3.column2_name,..., 
FROM table1
RIGHT JOIN table2 ON table1.table1_id = table2.table1_id 
RIGHT JOIN table3 ON table2.table2_id  = table3.table2_id;

Example Query 

SELECT students.full_name, students.gender, contacts.mobile_number, contacts.email_address, universities.university_name
FROM universities
RIGHT JOIN students ON universities.university_id = students.university_id
RIGHT JOIN contacts ON students.student_id = contacts.student_id;

Output 

full_namegendermobile_numberemail_addressuniversity_name
Noor KhaMale2222222222aaaa222@gmail.comSRM University
Avneesh MishraMale5555555555bbbb555@gmail.comAmity University
Monika SinghFemale7777777777cccc777@gmail.comAnna University
Aaliya KhanFemale8888888888dddd888@gmail.comHindustan University
Aaliya KhanFemale1010101010eeee101@gmail.comHindustan University
Aaliya KhanFemale1212121212ffff121@gmail.comHindustan University
Avneesh MishraMale1313131313gggg131@gmail.comAmity University
Avneesh MishraMale1414141414iiii141@gmail.comAmity University
NULLNULL1515151515jjjj151@gmail.comNULL

FULL JOIN 3 Tables

If you use the RIGHT JOIN keyword with the select statement, It will return a new result table by joining the above three tables with all the records of right the table and left tables.

Syntax 

You can join 3 tables using the following INNER JOIN syntax –

SELECT table1.column1_name, table1.column2_name,..., 
       table2.column1_name, table2.column2_name,..., 
       table3.column1_name, table3.column2_name,..., 
FROM table1
FULL JOIN table2 ON table1.table1_id = table2.table2_id 
FULL JOIN table3 ON table2.table2_id  = table3.table2_id;

Query 

SELECT students.full_name, students.gender, contacts.mobile_number, contacts.email_address, universities.university_name
FROM universities
FULL JOIN students ON universities.university_id = students.university_id
FULL JOIN contacts ON students.student_id = contacts.student_id;

Output 

full_namegendermobile_numberemail_addressuniversity_name
Noor KhanMale2222222222aaaa222@gmail.comSRM University
Avneesh MishraMale5555555555bbbb555@gmail.comAmity University
Monika SinghFemale7777777777cccc777@gmail.comAnna University
Aaliya KhanFemale8888888888dddd888@gmail.comHindustan University
Aaliya KhanFemale1010101010eeee101@gmail.comHindustan University
Aaliya KhanFemale1212121212ffff121@gmail.comHindustan University
Avneesh MishraMale1313131313gggg131@gmail.comAmity University
Avneesh MishraMale1414141414iiii141@gmail.comAmity University
Sunil KumaMaleNULLNULLHindustan University
Mamta GuptaFemaleNULLNULLHindustan University
Rapson JaniMaleNULLNULLSRM University
Kundan BhartiMaleNULLNULLSRM University
Manmohan SinghMaleNULLNULLNULL
Manisha ChaudhryMaleNULLNULLNULL
NULLNULL1515151515jjjj151@gmail.comNULL

MySQL Join 3 Tables with WHERE Clause Conditions

If you want to select those records that have a gender value is ‘male’ then you can join 3 tables with where clause.

SELECT students.full_name, students.gender, contacts.mobile_number, contacts.email_address, universities.university_name
FROM universities
LEFT JOIN students ON universities.university_id = students.university_id
LEFT JOIN contacts ON students.student_id = contacts.student_id
WHERE students.gender='male';

Join 3 Tables with ORDER BY Clause

If you want to select records based on full_name in alphabetical order then you can join 3 tables with ORDER BY clause.

SELECT students.full_name, students.gender, contacts.mobile_number, contacts.email_address, universities.university_name
FROM universities
LEFT JOIN students ON universities.university_id = students.university_id
LEFT JOIN contacts ON students.student_id = contacts.student_id
ORDER BY students.full_name;

Join 3 Tables with GROUP BY clause

If you want to count records by grouping gender then you can join 3 tables with the GROUP  BY clause

SELECT students.full_name, students.gender, contacts.mobile_number, contacts.email_address, universities.university_name
FROM universities
LEFT JOIN students ON universities.university_id = students.university_id
LEFT JOIN contacts ON students.student_id = contacts.student_id
GROUP BY students.gender;

REF