What"s the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for What"s the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?

The Dance of Joins: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN 💃🕺

So you've encountered these fancy terms called INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN while working with MySQL, and you're wondering what they actually mean. Fear not! In this blog post, we'll unravel the mysteries of these SQL joins and make them dance to your tune. 💃🎶

The Basics: What are Joins? 🤔

In the world of databases, joining tables is like bringing different pieces of a puzzle together. Joins allow you to combine data from two or more tables based on a related column between them. This opens up a whole new world of possibilities for querying and analyzing data. 🌍🧩

INNER JOIN: The Matchmaker 👥

Imagine you're playing matchmaker and trying to find the perfect matches from two sets of data. This is where INNER JOIN comes in. It selects the matching records between two tables, leaving out any records that don't have a match.

Here's an example:

SELECT *
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

This query will give you a result set containing only the rows where the values in the specified columns match in both tables. It's like finding your soulmate, but for data! 💑💘

LEFT JOIN: The Inclusive Friend 🤝

Let's say you have two tables, and you want to keep all the records from the "left" table (the one specified first in the query) while also including matching records from the "right" table. That's where LEFT JOIN comes into play.

Example time:

SELECT *
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;

In this query, you'll get all the records from table1, and if there's a match in table2, it will be included as well. If there's no match, the columns from table2 will be filled with NULL values. It's like being the inclusive friend who invites everyone to the party! 🎉🤩

RIGHT JOIN: The Settler 🧱🏠

On the other hand, if you want to keep all the records from the "right" table and include matching records from the "left" table, you'll want to use RIGHT JOIN.

Here's an example:

SELECT *
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;

With this query, you'll get all the records from table2, and if there's a match in table1, it will be included as well. Again, if there's no match, the columns from table1 will be filled with NULL values. It's like settling down with all your possessions in a new house! 🧳🏡

FULL JOIN: The Ultimate Reunion 🎉🎉

Sometimes, you just want to bring everyone together, no matter if they have a match or not. That's where FULL JOIN shines. It combines all the records from both tables, including both the matching and non-matching ones.

Take a look at this example:

SELECT *
FROM table1
FULL JOIN table2
ON table1.column = table2.column;

Now you'll get all the rows from both table1 and table2, and matches will be displayed with their corresponding values. Any non-matching rows will have NULL values in their respective columns. It's like throwing the ultimate reunion party where everyone is invited! 🎊🎈

Wrapping Up 🎁

By now, you should be familiar with the dance of INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN. They all have their unique styles and purposes, so make sure to choose the right one for your data puzzle.

Remember, INNER JOIN finds matches, LEFT JOIN keeps the left side and adds matching records, RIGHT JOIN keeps the right side and adds matching records, and FULL JOIN combines everyone for the ultimate reunion.

So go ahead, try them out, and make your data dance! 💃🕺

Have you encountered any interesting scenarios where these joins saved the day? Share your thoughts and experiences in the comments below! Let's get the conversation started! 👇😄

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my