sajad torkamani

Example database

The rest of this post will assume an example blog database with two tables: posts and users

posts table

idtitlecontentuser_id
1Michael post 1Lorem ipsum1
2Michael post 2Lorem ipsum1
3Jim post 1Lorem ipsum2
4Jim post 2Lorem ipsum2
5Anonymous post 1Lorem ipsumNULL
6Anonymous post 2Lorem ipsumNULL

users table

idname
1michael@example.com
2jim@example.com
4pam@example.com

Inner joins

An inner join returns all records from the left table that have a matching record in the right table.

SQL inner join illustration

Example query

Assuming the blog database mentioned at the start of this post, the following query will return all the posts records that have a corresponding record in users:

SELECT p.title post,
       u.name  author
FROM posts p
         INNER JOIN users u ON p.user_id = u.id;

Example result

postauthor
Michael post 1michael@example.com
Michael post 2michael@example.com
Jim post 1jim@example.com
Jim post 2jim@example.com

Note how the anonymous posts (i.,e posts with the IDs 5 and 6) that don’t have a user_id aren’t included in the result.

Inner joins are very useful when you want to select only records that have a corresponding record from another table. For example, you may want to query all students that have already been assigned to a supervisor.

Left joins

A left join returns all records from the left table along with any matching records from the right table. Unlike inner joins though, left joins will include all results regardless of whether there is a matching record from the right table. Where there are no matching records from the right table, their column values will be set to NULL.

SQL left join illustration

Example query

The following query will return all posts records, even if they don’t have a corresponding record in users.

SELECT p.title post,
       u.name  author
FROM posts p
         LEFT JOIN users u ON p.user_id = u.id;

Example result

postauthor
Michael post 1michael@example.com
Michael post 2michael@example.com
Jim post 1jim@example.com
Jim post 2jim@example.com
Anonymous post 1NULL
Anonymous post 2NULL

Notice how unlike with the inner join query, the left join returns all six posts (including the anonymous posts) and displays NULL for the columns from the users table for posts that don’t have a corresponding user record.

Right joins

A right join returns all records from the right table along with any matching records from the left table. Where there are no matching records from the left table, their column values will be set to NULL.

SQL right join illustration

Example query

The following query will return all users records, even if they don’t have a corresponding record in posts.

SELECT u.name  author,
       p.title post
FROM posts p
         RIGHT JOIN users u ON p.user_id = u.id;

Example result

authorpost
michael@example.comMichael post 2
michael@example.comMichael post 1
jim@example.comJim post 2
jim@example.comJim post 1
pam@example.comNULL

Notice how we get the user pam@example.com even though she doesn’t have any associated posts.

Right joins seem rarer from my experience because they are typically written as left joins to improve readability. For example, the previous right join query can be re-written as:

SELECT u.name  author,
       p.title post
FROM users u
         LEFT JOIN posts p ON p.user_id = u.id;

This will return the same result:

authorpost
michael@example.comMichael post 2
michael@example.comMichael post 1
jim@example.comJim post 2
jim@example.comJim post 1
pam@example.comNULL

Wrapping up

Here are some links that I found useful for explaining SQL joins.

You might also find it useful to practice your SQL skills using a platform like Codewars.

Tagged: MySQL

Leave a comment

Your email address will not be published. Required fields are marked *