DEV Community

Olufisayo Bamidele
Olufisayo Bamidele

Posted on

SQL JOINS; Identifying The Left And Right Table

If the blog post's title caught your attention, then you're probably like me; you remember the big things and always need to remind yourself of the small stuff. You spell length as "lenght" all the time, then go back to correct yourself; you need someone to remind you of the correct way to end a formal letter: yours faithfully, yours sincerely, yours violently, or yours sarcastically, and when joining tables in SQL, you argue with yourself about which table you're joining is the left or right table.

So Which One Is The Left Table?

I'll tell you now, for the one hundred and sixty-fifth times, the table that follows your FROM keyword is always the left table. The right table is the one that comes immediately after the JOIN keywords. You just need to remember one of those.

Let's Practice

SELECT post.* FROM users LEFT JOIN  posts ON   users.id = posts.user_id
Enter fullscreen mode Exit fullscreen mode

Now, which table is the left table? The left table in the query above is the users' table. Why? Because it comes immediately after the "FROM" keyword. That makes the posts table the right table.

Let's flip it around.

SELECT users.* FROM users RIGHT JOIN posts ON posts.user_id = users.id
Enter fullscreen mode Exit fullscreen mode

Now, which table is the right table? The right table is still the posts table. To identify the right table, first identify the left table. The left table is the table that comes immediately after theFROM keyword, which is the users table. This automatically makes the posts table the right table.

Now we get it but let's bookmark this blog post because I know we'll forget again 😉.

Top comments (0)