There's really two (2) main types of SQL joins you need to master. The LEFT JOIN and the INNER JOIN, let's jump in:
LEFT JOIN
A LEFT JOIN basically means, "Pull everything from the table in our FROM statement, and match it to the table in our LEFT JOIN statement. And, for any match that cannot be found mark it as NULL".
Here's a visual of what a LEFT JOIN looks like:
And as an example query, let's look at two users from our Getting Started: Users dataset, and then match them to our Getting Started: Payments table:
SELECT *
FROM getting_started.users
LEFT JOIN getting_started.payments ON users.user_id = payments.user_id
WHERE users.user_id IN (1,2)
;
Which gives us the following output (notice how there's no match in the Payments table for User ID: 2, thus all values in the Payments table are marked NULL):
INNER JOIN
An INNER JOIN basically means, "Pull only the data from both tables where a match is found".
Here's a visual of what an INNER JOIN looks like:
And as an example query, let's look at two users from our Getting Started: Users dataset, and then match them to our Getting Started: Payments table:
SELECT *
FROM getting_started.users
INNER JOIN getting_started.payments ON users.user_id = payments.user_id
WHERE users.user_id IN (1,2)
;
Which gives us the following output (notice how it doesn't pull any information for User ID: 2, because there not in both tables):