Se one in SQL | Types of joins in SQL

Contents

Introduction

SQL Joins can be a tricky concept for beginners to master. If you haven't studied programming before, you may have a hard time understanding what combinations are in SQL and the different types of unions.

But as an aspiring or data science professional, you should have a solid understanding of what SQL joins are and how they work. Créame, you will use it a lot if the data science industry quickly retrieves and manipulates the data present in different tables.

join-sql-4406020

In this article, i will show that sql joins are really easy to learn. We will first understand what SQL joins are and then we will look at the four different types of joins that you will need to master..

Do you want to learn the basics of what SQL is and how it can be applied in data science? Look at the popular course SQL for data science.

What are SQL Joins?

Let's first answer the million dollar question before looking at the different types of joins in SQL.

I'll take an intuitive example to explain what SQL joins are. Consider these two collections:

1-5-300x153-2674159

Let's say the blue circle represents the set of all children (KIDS) and the gray represents the set of people who love to see Messi play (MESSI). How would you proceed if we wanted the set of all the guys who love to see Messi play??

There is a very procedural way to approach this problem:

  • First, select all the different identifiers from Messi's table that represent the inner query below
  • Take each ID from the Boys chart and compare it to this set.
  • If the id matches any of them, then generate that row from Boys table

This is quite similar to the concept of a 'for loop’ and it is called sub-selection in SQL.

SELECT * FROM BOYS
WHERE id IS IN (SELECT DISTINCT id FROM MESSI);

But in SQL, there is another way to approach this problem.

To begin to understand combinations, we must first have a different perspective on what we really want. In established terminology: we want the intersection by BOYS and MESSI. In graphic terms, this is expressed as:

img1-300x193-8693146

We are interested in the celestial part, truth? This part, o la indoor part (track), They are all the boys who love to see Messi. All we have to do now is express this in SQL:

SELECT * FROM BOYS
INNER JOIN MESSI
ON BOYS.id = MESSI.id;

Look what the (indoor) join ago? It couldn't be easier! This is the intuitive approach on how to understand the combinations.

Note: Venn diagrams are not applied directly to SQL because elements in collections (The tables) they are not identical. But because they refer to each other, we can use Venn diagrams to better understand the concept.

Different types of joins in SQL

Now. We will extend this to the big picture and learn about the different types of SQL joins. Consider the following sample tables:

selection_162-2674429

1. INNER JOIN en SQL

This is what we covered in the previous section. Inner Join devuelve registros que tienen valores coincidentes en ambas tablas:

inner-300x237-1785551

Let's see what the output is using the above example:

SELECT * FROM BOYS INNER JOIN MESSI
ON BOYS.id = MESSI.id;

PRODUCTION:

selection_163-4866670

As I mentioned before, inner join gives the intersection of two tables, namely, rows that are common in both tables.

2. RIGHT JOINT (external) and SQL

Suppose we want the identification and name of all the people who love to see Messi play. Obviously, there are many ways to write this query, but we will understand it with the help of unions.
right-300x221-6808244

Let's see what the output is:

SELECT  *  FROM BOYS RIGHT JOIN MESSI 
ON BOYS.id = MESSI.id;

PRODUCTION:

selection_164-6575760

Can you find out what happened here? The right outer join gives us the rows that are common in both tables, as well as additional rows from Messi's table that are not present at the intersection. In other words, a right join returns all records from the right table and matching records from the left table.

3. LEFT (Outer) JOIN en SQL

Let's say we want the list of all the guys who love to watch Messi play and don't love to see Messi play wearing joins..

left-300x212-7384950

I want you to guess the final result before reading on.

SELECT  *  FROM BOYS LEFT JOIN MESSI
ON BOYS.id = MESSI.id;

PRODUCTION:

selection_165-1571438

The left outer join gives us the rows that are common in both tables, as well as additional rows from the Boys table that are not present in the intersection. In other words, a left join returns all records from the left table and matching records from the right table.

4. Complete union (EXTERIOR) and SQL

Finally, let's say we want the list of all people, including kids who love to watch Messi play.

full-300x202-3724756

I'm sure you already know the answer by now!!

SELECT  *  FROM BOYS FULL OUTER JOIN MESSI
ON BOYS.id = MESSI.id;

PRODUCTION:

selection_166-8467922

Perfect! A full outer join gives us the rows that are common in both tables, as well as additional rows from both tables that are not present at the intersection. We get all the records when there is a match in the left or right table.

Final notes

Want to learn how SQL can be used in data science? I highly recommend taking a look at this amazing course: Structured query language (SQL) for data science.

If you have any questions or comments about this article, let me know in the comment section below and I'll be happy to connect with you.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.

Datapeaker