Writing JOINS in SQL
Tags: database, sql, joins
Published: 2019-06-13 15:27:02

---
title: Writing JOINS in SQL
published: true
tags: Databases, SQL, JOINS
canonical_url: https://medium.com/@sebastiensanzdesantamaria/writing-joins-in-sql-5f3a5d546c91
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnszx9gmxywmx1drptr5.jpeg
---

Week two at Flatiron School had us jumping straight into SQL. I was keen to learn more about SQL since I have experience running Wordpress sites, like the artist residency program I co-founded, [Residency Unlimited](http://residencyunlimited.org). 

I have spent unhealthy amounts of time googling SQL queries to figure out, for example, how to [assign single posts from one post-type to another](https://stackoverflow.com/questions/12090643/is-it-possible-to-convert-wordpress-custom-post-type-to-normal-categories) in phpMyAdmin (and back again, yuck!).

With a deeper grasp of databases and their management via SQL, I find it easier to grasp Object Orientated Programming and particularly value how [Active Record](https://guides.rubyonrails.org/active_record_basics.html#crud-reading-and-writing-data) streamlines the process between a database and your code.

Databases themselves are about as exhilarating as tanks of gas, but that’s ok, they don’t need to be exciting. Their purpose is to hold the fuel (in our case, content) that our car needs to function. SQL is a means to get the gas from tank to engine.

Lets dive into SQL JOINs by observing the following situation:

![](https://cdn-images-1.medium.com/max/1024/1*DF0XIwYg7Q869rfaEnCPOQ.jpeg)

This diagram shows us how we can apply a JOIN to the above scenario.

![](https://cdn-images-1.medium.com/max/880/1*tkL0F_oELYKwwCxlrgbGXg.png)

A JOIN in SQL returns rows from multiple tables by identification through these three sections: INNER, LEFT, and RIGHT.

Let’s take a look at how different JOIN types do different things.

### INNER JOIN

![](https://cdn-images-1.medium.com/max/880/1*Roq-OQxi010NUBWh36AQgA.png)

INNER JOIN — or simply JOIN, can be used to find a match between two tables. Both tables need to share a key. This key is used to make the match. Non-matching results are discarded.

```
SELECT \*
FROM TableD Dogs 
INNER JOIN TableC Car
ON Dogs.key = Car.key;
```

### LEFT JOIN

![](https://cdn-images-1.medium.com/max/880/1*T4IoI65u3nBn2keU0vSm8A.png)

LEFT JOIN — Returns all rows from the left table, and the matched rows from the right table.

```
SELECT \*
FROM TableD Dogs 
LEFT JOIN TableC Cars
ON Dogs.key = Cars.key;
```

![](https://cdn-images-1.medium.com/max/880/1*6gZu4qf-c-3bWWgZ7DXogg.png)

You can also add a WHERE clause to return only rows from the outer section:

```
SELECT \*
FROM TableD Dogs 
LEFT JOIN TableC Cars
ON Dogs.key = Cars.key
WHERE Cars.key IS NULL;
```

### RIGHT JOIN

![](https://cdn-images-1.medium.com/max/880/1*lYwaXVtrx1Wt6U-V0ILbQQ.png)

RIGHT JOIN — The same as LEFT, but in reverse. Returns all rows from the right table, and the matched rows from the left table.

```
SELECT \*
FROM TableD d 
RIGHT JOIN TableC c
ON c.key = d.key;
```

![](https://cdn-images-1.medium.com/max/880/1*Q90C8gmhlxnygy7L5hR9aQ.png)

And like in the previous example, return only rows from the outer section.

```
SELECT \*
FROM TableD Dogs 
RIGHT JOIN TableC Cars
ON Dogs.key = Cars.key
WHERE Dogs.key IS NULL;
```

### FULL OUTER JOIN

![](https://cdn-images-1.medium.com/max/880/1*SPde8BXaYD697DMciPZhAw.png)

FULL OUTER JOIN — Returns all rows when there is a match in ONE of the tables.

```
SELECT \*
FROM TableD Dogs 
FULL OUTER JOIN TableC Cars
ON Dogs.key = Cars.key;
```

![](https://cdn-images-1.medium.com/max/880/1*FkWYT9uS6h0TkJkH81Gd3g.png)

Just want OUTER JOINS, excluding INNER?

```
SELECT \*
FROM TableD Dogs 
FULL OUTER JOIN TableC Cars
ON Dogs.key = Cars.key
WHERE Dogs.key IS NULL
OR Cars.key IS NULL;
```

### Why is this important?

It reflects the DRY (Don’t Repeat Yourself) rule.

Only using INNER JOINs on every query returns a unique match discarding any non-matching entry. There is no way to know which dogs have never been in a car, or which car has never had a dog in it. You quickly find yourself going back and forth to find that specific information when you need it.

Using LEFT, RIGHT or FULL JOINs provides more detail in one fell swoop, resulting in faster insight to what your database contains and how its contents are related.

### Other JOINS

There are other operators in SQL that help you parse rows. For example, using CROSS will join everything with everything, and UNION joins the results of two queries into one column and removes duplicate entries.

Check out [SQL definitions on W3schools](https://www.w3schools.com/sql/sql_join.asp) and try out your [JOIN skills on Hack Array.](https://www.hackerrank.com/domains/sql?filters%5Bsubdomains%5D%5B%5D=join)