SQL JOINs. Bringing data together one join at a... | by Randy Macaraeg | Towards Data Science

Thanks to Jeremy Chow for the clever subtitle!
The code I used for this blog can be found on my GitHub.
There are a lot of things to learn in SQL, especially for me. One of the things that took the longest time for me to understand are JOIN statements. It’s intimidating at first, but after reading and lots of practice I got the hang of it and I hope by the end of this blog you’ll feel the same way!
There are four different types of Joins that we will be going over in this blog:
INNER JOIN (or JOIN)
LEFT JOIN (or LEFT OUTER JOIN)
RIGHT JOIN (or RIGHT OUTER JOIN)
FULL JOIN
Special Note: Not all SQL programs you encounter will have RIGHT JOINs or FULL JOINs, so I’ll add the workarounds to each in their respective sections.
For these examples, we’ll use the good ol’ Chinook database which is also used in the SQL Basics blog as well as a most other SQL related blogs I have.
Let’s set up the database file, libraries, and functions:
The database is loaded and now we’re ready to work with joins!
The INNER JOIN keyword selects all rows from both tables as long as the conditions are met. This keyword creates a new set that combines all rows from both of the tables where the values of the common fields are the same.
Let’s use an example and see how this works:
SELECT a.name, a.composer, b.nameFROM tracks AS aINNER JOIN genres AS bON a.genreid = b.genreidWHERE b.name LIKE 'b%'LIMIT 10;
After setting the tracks table to the variable name a and the genres table to the variable name b, these two tables are linked by a common key, which is the genreid key that exists in both tables. It then finds the song name and composer from the tracks table, then finds the corresponding genre name from the genres table.
It then limits the results to only the ones whose song name start with the letter “b” and further limits the results to the first 10. The end output will return 10 songs in the Blues genre with song name and composer name.
Now that we can explain what’s going on in the INNER JOIN keyword a little better, let’s move on to the LEFT JOIN keyword!
The LEFT JOIN keyword returns all the rows of the table on the left side of the join and matching rows for the table on the right side of the join. If there are left side rows with no matching rows on the right side, a null value will fill the space for the right side.
SELECT a.trackid, a.name, a.composer, b.invoicelineid, b.invoiceidFROM tracks AS aLEFT JOIN invoice_items AS bON a.trackid = b.trackid;
In the above example, the query sets the tracks table to the variable name a and the invoice_items table to the variable name b. It links them through the common key, trackid, and returns the trackid, name, and composer from the tracks table with the invoice line ID and invoice ID from the invoice_items table.
While looking at the data, you might think to yourself: “Where‘d the nulls go?”. And, if you didn’t, we’re going to show how to find some of them anyway!
SELECT a.trackid, a.name, a.composer, b.invoicelineid, b.invoiceidFROM tracks AS aLEFT JOIN invoice_items AS bON a.trackid = b.trackidWHERE b.invoiceid IS NULL;
The IS NULL clause is useful for finding any null values in a column. Adding this line to the original example, we find that many rows did not have a value in the original invoice_items table and are replaced with null values instead.
Just like you’d expect, the RIGHT JOIN is similar to the LEFT JOIN. This join returns all of the rows of the table on the right side of the join and matching rows for the table on the left side of the join. And, for any rows where there are now matching rows on the left side, those values are replaced with null.
In most queries, RIGHT JOIN would look like this:
SELECT a.trackid, a.name, a.composer, b.invoicelineid, b.invoiceidFROM tracks AS aRIGHT JOIN invoice_items AS bON a.trackid = b.trackid;
Notice that this setup looks almost exactly like the example in the LEFT JOIN example, except that it now says RIGHT JOIN instead.
Now, for those of you using SQL programs that do not support RIGHT JOIN, the RIGHT JOIN keyword is essentially a flipped LEFT JOIN. Here’s what it looks like with the workaround:
SELECT b.trackid, b.name, b.composer, a.invoicelineid, a.invoiceidFROM invoice_items AS aLEFT JOIN tracks AS bON a.trackid = b.trackid;
It’s adjusted slightly from the RIGHT JOIN keyword, but this will output the same thing as the RIGHT JOIN example.
The output of this query would show invoices with songs that were actually ordered. Any songs not ordered in any of the invoices would not show up in this query.
Now that we have examples for these, let’s move on to the FULL JOIN!
The FULL JOIN keyword creates the result by combining the result of both a LEFT JOIN and a RIGHT JOIN. For any rows that aren’t matching, the result will contain null values.
This keyword is rarely used, but can be used to find duplicates, missing rows, or similar rows between two tables.
If your SQL program supports it, your query would look very simple:
SELECT table1.column1,table1.column2,table2.column1,....FROM table1 FULL JOIN table2ON table1.matching_column = table2.matching_column;
It’s useful to know that FULL JOIN is essentially a LEFT JOIN combined with a RIGHT JOIN. Assuming our SQL program doesn’t support FULL JOIN nor RIGHT JOIN, our query would look like this:
SELECT * FROM invoice_items AS aLEFT JOIN tracks AS bON a.trackid = b.trackidUNIONSELECT *FROM tracks AS aLEFT JOIN invoice_items AS bON a.trackid =b.trackidWHERE a.trackid IS NULL;
In this example there are three major things going on:
The first LEFT JOINThe second LEFT JOIN (pseudo-RIGHT JOIN)The UNION that merges the two together
The first LEFT JOIN
The second LEFT JOIN (pseudo-RIGHT JOIN)
The UNION that merges the two together
The IS NULL line makes sure duplicate rows don’t get entered into the result set and is very useful to have readable results.
The result will output 2240 rows of data, combining invoice data with individual song data.
Phew! That was a lot of queries, and I appreciate you hanging around this long!
In summary here’s how each JOIN keyword acts:
INNER JOIN: Selects all rows from both tables that have a matching common key.
LEFT JOIN: Uses all rows of the table on the left side and finds matching rows from the table on the right side.
RIGHT JOIN: Uses all rows of the table on the right side and finds matching rows from the table on the left side.
FULL JOIN: Combines all rows from both tables.
Hopefully this helped give some insight into how to create join queries with your database and I hope you’ll join me for the next post!