All You Need To Know About Joins In MySQL

When To Use a Join?

There comes a time when answering a question with data that not all the information needed is within a singular table. That is where knowing how to join tables becomes invaluable.

We will go through an understanding of Joins and how to apply them in MySQL.

What are Joins?

Joins help you connect multiple tables that have a column in common. The tables below have a relationship, highlighted in red, that we can use to connect the tables.

Employees

IDfirst_namelast_nameoffice_codeboss_ID
100DonatelloBianchi001null
101UgoCavallo001100
102GionataLemmi001100
103MadelineFournier002100
104LuukRoosa002100
105NiallMcCabe003101
106RosalvaRojasnull101
107KostisAntonisnull101

Offices

office_IDcityphone_number
001Seattle+1202-555-0183
002Dublin+353 01 918 3457
003Sydney+61 2 5550 9137
004Busan+82 2 407 5914

Types of Joins

Left Join

The returned table shows all rows from the left table (employees) and rows that have a matching value with the right table (offices).

SELECT * 
FROM employees
LEFT JOIN offices
ON employee.office_code = offices.office_ID
IDfirst_namelast_nameoffice_codeboss_IDoffice_IDcityphone_number
100DonatelloBianchi001null1Seattle+1202-555-0183
101UgoCavallo0011001Seattle+1202-555-0183
102GionataLemmi0011001Seattle+1202-555-0183
103MadelineFournier0021002Dublin+353 01 918 3457
104LuukRoosa0021002Dublin+353 01 918 3457
105NiallMcCabe0031013Sydney+61 2 5550 9137
106RosalvaRojasnull101nullnullnull
107KostisAntonisnull101nullnullnull

Right Join

The returned table shows all rows from the right table (offices) and rows that have a matching value with the left table (employees).

SELECT * 
FROM employees
RIGHT JOIN offices
ON employees.office_code = offices.office_ID
IDfirst_namelast_nameoffice_codeboss_IDoffice_IDcityphone_number
nullnullnullnullnull4Busan+82 2 407 5914
100DonatelloBianchi001null1Seattle+1202-555-0183
101UgoCavallo0011001Seattle+1202-555-0183
102GionataLemmi0011001Seattle+1202-555-0183
103MadelineFournier0021002Dublin+353 01 918 3457
104LuukRoosa0021002Dublin+353 01 918 3457
105NiallMcCabe0031013Sydney+61 2 5550 9137

As you can see above, the second table (offices) returned the Busan office details, although there are no recorded employees at this location.

Inner Join

The table will only return rows with the same value in common from both tables.

SELECT * 
FROM employees
INNER JOIN offices
ON employees.office_code = offices.office_ID
IDfirst_namelast_nameoffice_codeboss_IDoffice_IDcityphone_number
100DonatelloBianchi001null1Seattle+1202-555-0183
101UgoCavallo0011001Seattle+1202-555-0183
102GionataLemmi0011001Seattle+1202-555-0183
103MadelineFournier0021002Dublin+353 01 918 3457
104LuukRoosa0021002Dublin+353 01 918 3457
105NiallMcCabe0031013Sydney+61 2 5550 9137

Self-Join

Duplicates a singular table, treating each version as unique and joining them into one. But why would you need a table to do this? In the case of hierarchical data.

Each row in the Employees’ table references their boss’s ID number. We may want the information of the higher-up to show on the same row as the subordinate.

-- I'm narrowing down the columns viewed after the join
SELECT emp.ID, emp.first_name, emp.last_name, emp.office_code, 
       emp.boss_ID, boss.first_name AS boss_fname, 
       boss.last_name as boss_lname, boss.office_code AS boss_office_code
FROM employees AS emp
INNER JOIN employees AS boss
ON emp.boss_ID= office_table.ID
IDfirst_namelast_nameoffice_codeboss_IDboss_fnameboss_lnameoffice_code
101UgoCavallo001100DonatelloBianchi001
102GionataLemmi001100DonatelloBianchi001
103MadelineFournier002100DonatelloBianchi001
104LuukRoosa002100DonatelloBianchi001
105NiallMcCabe003101UgoCavallo001
106RosalvaRojasnull101UgoCavallo001
107KostisAntonisnull101UgoCavallo001

James Glassey