SQL JOINs: INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL OUTER JOIN

2018-12-21  Programming

My next post on SQL takes a look at JOINs, which allow you to compare and merge two tables in various ways and determine the relationship between two sets of data. Here’s what INNER, LEFT, RIGHT, and FULL OUTER JOINs are and how to use them.

Having gotten to grips with the construction of basic SQL queries, the next challenge that arose in my work was to merge two SQL tables based on records and values that match. In SQL, this is done with the very useful JOIN command, which has a few variants – INNER, LEFT, RIGHT, and OUTER – that return different combinations of records from the first, second, and both tables, and merge them differently.

To illustrate this, I’ve set up two tables. The first, Positions, contains a list of team IDs and their 2017/18 Premier League finishing positions. The second, Teams, contains a list of team IDs and the teams’ names. To mix things up, there are some positions in Positions that do not have corresponding teams in Teams – seventh, eighth, and ninth – and some non-Premier League teams in Teams that do not have positions in Positions – Portsmouth, Millwall, and Norwich.

Teams
team_id team_name
12 Manchester City
4 Chelsea
1 Arsenal
13 Manchester United
17 Tottenham
11 Liverpool
53 Portsmouth
29 Millwall
33 Norwich
Positions
team_id position
12 1
4 5
1 6
13 2
17 3
11 4
15 7
19 9
20 8

INNER JOIN

A SQL INNER JOIN returns all the records in the two tables where the values in a column match. In this case, I’m matching on the team_id column, which is the only column the two tables have in common.

SELECT Teams.team_id, team_name, position FROM Teams
INNER JOIN Positions ON Teams.team_id = Positions.team_id
ORDER BY position ASC

In our example, this returns a list of the team IDs that match across the two tables with their corresponding names and positions. The positions that don’t have matching teams (seventh, eighth, ninth) and teams that do not have matching positions (Portsmouth, Millwall, Norwich) are excluded.

team_id team_name position
12 Manchester City 1
13 Manchester United 2
17 Tottenham 3
11 Liverpool 4
4 Chelsea 5
1 Arsenal 6

LEFT JOIN

A SQL LEFT JOIN returns all the records from your first table, and all the records that match from the second. In this example, this means we get the full list of teams, but only the positions that match.

SELECT Teams.team_id, team_name, position FROM Teams
LEFT JOIN Positions ON Teams.team_id = Positions.team_id
ORDER BY position ASC

This returns the full list of team_ids from Teams with corresponding data from Positions added where appropriate. This means Portsmouth, Millwall, and Norwich are included without positions, but the additional positions (seventh, eighth, and ninth) are left out.

team_id team_name position
53 Portsmouth null
29 Millwall null
33 Norwich null
12 Manchester City 1
13 Manchester United 2
17 Tottenham 3
11 Liverpool 4
4 Chelsea 5
1 Arsenal 6

RIGHT JOIN

A SQL RIGHT JOIN does just the opposite, returning all the records from your second table, and all the records that match from the first. This means the full list of positions will be returned.

SELECT Teams.team_id, team_name, position FROM Teams
RIGHT JOIN Positions ON Teams.team_id = Positions.team_id
ORDER BY position ASC</pre>

This gives us the full list of positions from Positions, including seventh, eighth, and ninth, which have no teams. Meanwhile, Portsmouth, Millwall, and Norwich – which have no positions – are excluded.

team_id team_name position
12 Manchester City 1
13 Manchester United 2
17 Tottenham 3
11 Liverpool 4
4 Chelsea 5
1 Arsenal 6
null null 7
null null 8
null null 9

FULL OUTER JOIN

A SQL FULL OUTER JOIN will return all records in both of the tables, merging them were appropriate.

SELECT Teams.team_id, team_name, position FROM Teams
FULL OUTER JOIN Positions ON Teams.team_id = Positions.team_id
ORDER BY position ASC

Here we get the full list of teams – even those without positions – and the full list of positions – even those without corresponding teams. Every record in both tables is represented in the output.

team_id team_name position
53 Portsmouth null
29 Millwall null
33 Norwich null
12 Manchester City 1
13 Manchester United 2
17 Tottenham 3
11 Liverpool 4
4 Chelsea 5
1 Arsenal 6
null null 7
null null 8
null null 9

SQL JOINs can be very useful when establishing the relationships between two or more large data sets – particularly when merging two tables of related data from two different sources. Learning what each JOIN does and how to use it effectively can be a valuable tool to have in your SQL arsenal.

Photo from rawpixel.com on Pexels

Looking for the comments? My website doesn't have a comments section because it would take a fair amount of effort to maintain and wouldn't usually present much value to readers. However, if you have thoughts to share I'd love to hear from you - feel free to send me a tweet or an email.