Skip to content

Commit

Permalink
Final draft of chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
gildedgardenia committed Apr 10, 2024
1 parent 0c2b9d4 commit 9f88e89
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 36 deletions.
19 changes: 19 additions & 0 deletions content/sql-part-3/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,34 @@ weight = 20

## Learning Objectives

Upon completing all the content in this chapter, you should be able to do the following:

1. Understand what a join is.
1. Write queries that use inner, full, left, and right joins.
1. Explain the differences between the types of joins listed above.
1. Use the `HAVING` clause with joins to filter result sets.

## Key Terminology

Here is a list of key terms for this chapter broken down by the page the term first appears on. Make note of each term and its definition.

### What is a Join?

1. join

### Inner Joins

1. inner join

### Left and Right Joins

1. left outer join
1. right outer join

### Full Outer Joins

1. full outer join

## Content Links

{{% children %}}
6 changes: 3 additions & 3 deletions content/sql-part-3/exercises/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ weight = 2

## Getting Started

Fork this [GitHub repository](https://github.com/launchcodeeducation/SQL-Part-3-Exercises) and clone to your computer.
Open up the `SQL-Part-3-Exercises.ipynb` notebook in `data-analysis-projects/sql-part-3/exercises` using Azure Data Studio.

## In Your Notebook

Open Azure Data Studio and connect to the `BookDB`.
Connect to `BooksDB` before starting work on the exercises.

## Submitting Your Work

When finished make sure to push your changes up to GitHub.
When finished, make sure to push your changes up to GitHub.

Copy the link to your GitHub repository and paste it into the submission box in Canvas for **Exercises: SQL Part 3** and click *Submit*.
3 changes: 3 additions & 0 deletions content/sql-part-3/next-steps.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ draft = false
weight = 4
+++

You are now ready to go on to learning more about complex SQL queries. If you would like some additional resources on joins, here are a few of our favorites:

1. [Joins](https://learn.microsoft.com/en-us/sql/relational-databases/performance/joins?view=sql-server-ver16)
1. [SQL Join Types](https://www.metabase.com/learn/sql-questions/sql-join-types)
31 changes: 15 additions & 16 deletions content/sql-part-3/reading/full-joins/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ draft = false
weight = 4
+++

Joining two tables with a **full outer join** gives us a result set that
includes all records from both tables. Full outer joins are important to SQL,
but the syntax is not supported in MySQL. Instead, to achieve a full outer
join, you have to work with a left outer join and a right outer join. To show
what a full outer join looks like in other types of SQL, we have simulated some
possible syntax below.
Finally, we have a full outer join. Joining two tables with a **full outer join** gives us a result set that
includes all records from both tables with null values for unmatched rows.

Now that another event planner has joined Mary's company, to get all of the
events run by the company in August, we can use a full outer join to combine
Expand All @@ -23,18 +19,21 @@ FULL OUTER JOIN leah_events ON mary_events.month = leah_events.month
WHERE mary_events.month = 08;
```

.. figure:: figures/fullouterjoin.png
:alt: Venn diagram with the entirety of both circles highlighted.
![Venn diagram with the entirety of both circles highlighted](./pictures/fullouterjoin.png)

The Venn diagram above shows the result set highlighted in blue.

If you do want to try out a full outer join, the syntax to simulate it looks some like this:
## Check Your Understanding

```sql {linenos=table}
SELECT * FROM table_a LEFT JOIN table_b ON table_a.column_name_1 = table_b.column_name_1
UNION
SELECT * FROM table_a RIGHT JOIN table_b ON table_a.column_name_1 = table_b.column_name_2;
```
{{% notice green Question %}}

What does a `FULL JOIN` do?

1. Returns results with matching rows in both tables
1. Returns results with all the rows from the left table with null values for unmatched rows from the right table
1. Returns results with all the rows from the right table with null values for unmatched rows from the left table
1. Returns results from all the rows from both tables with null values filled in for all unmatched rows

{{% /notice %}}

`UNION` is used to bring together the result sets of 2 `SELECT` queries.
Check out the [documentation](https://dev.mysql.com/doc/refman/8.0/en/union.html) for more information on how `UNION` works.
<!-- 4 -->
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 47 additions & 5 deletions content/sql-part-3/reading/inner-joins/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ weight = 2
+++

Joining two tables with an **inner join** produces a result set that only
includes the values that are present in *both* tables.
includes the values that are present in *both* tables. For the rest of this chapter, we will be returning to Mary, the event planner, to see what different types of joins can do.

If we use an inner join to combine `johnson_wedding` and `johnson_vow_renewal` in a query, we can see what guests are going to both the vow renewal and the wedding.
Mary is working with the Johnsons again. She previously planned their wedding and is now planning their vow renewal. If we use an inner join to combine `johnson_wedding` and `johnson_vow_renewal` in a query, we can see what guests are invited to both the vow renewal and the wedding.

```sql {linenos=table}
SELECT last_name, first_name
Expand All @@ -18,7 +18,49 @@ INNER JOIN johnson_wedding ON johnson_vow_renewal.guest_id = johnson_wedding.gue

This query will give us a result set of the first and last names of the guests from the `johnson_vow_renewal` table that are also in the `johnson_wedding` table.

.. figure:: figures/innerjoin.png
:alt: Venn diagram highlighting just the center where the two circles meet.
![Venn diagram highlighting just the center where the two circles meet](./pictures/innerjoin.png)

The Venn diagram above shows the result set highlighted in blue.
The Venn diagram above shows the result set highlighted in blue.

You can filter a join with the `WHERE` clause as well. In the case of the Johnsons, Mary may want to see which guests who attended the wedding are confirmed for the vow renewal.

```sql {linenos=table}
SELECT last_name, first_name
FROM johnson_vow_renewal
INNER JOIN johnson_wedding ON johnson_vow_renewal.guest_id = johnson_wedding.guest_id
WHERE johnson_wedding.attending = 1 AND johnson_vow_renewal.attending = 1;
```

Now, let's say we want to use an aggregate function with our join. We can use `GROUP BY` to group the result sets by dietary restrictions. We can write the following inner join.

```sql {linenos=table}
SELECT last_name, first_name
FROM johnson_vow_renewal
INNER JOIN johnson_wedding ON johnson_vow_renewal.guest_id = johnson_wedding.guest_id
GROUP BY johnson_vow_renewal.diet;
```

The above query groups the result set by dietary restriction, but since Mary is currently working with the caterers to plan out the dinner options, she wants to make sure that she is only looking at guests who RSVP'd yes for the vow renewal. We cannot use `WHERE` with an aggregate function like `GROUP BY` so we need to use `HAVING` instead.

```sql {linenos=table}
SELECT last_name, first_name
FROM johnson_vow_renewal
INNER JOIN johnson_wedding ON johnson_vow_renewal.guest_id = johnson_wedding.guest_id
GROUP BY johnson_vow_renewal.diet
HAVING johnson_vow_renewal.attending = 1;
```

## Check Your Understanding

{{% notice green Question %}}

What does an inner join do?

1. Returns results with matching rows in both tables.
1. Returns results with all the rows from the left table with null values for unmatched rows from the right table.
1. Returns results with all the rows from the right table with null values for unmatched rows from the left table.
1. Returns results from all the rows from both tables with null values filled in for all unmatched rows.

{{% /notice %}}

<!-- 1 -->
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion content/sql-part-3/reading/intro/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ line 3, we need to include the type of join as part of our query with the
condition follows the `ON` keyword and tells SQL what we believe to be
matching records. This may mean we want to join on matching customer ids or
matching dollar amounts or matching dates depending on the tables we are
working with and what questions we need to answer.
working with and what questions we need to answer.

Let's dive into the specific type of joins and how each one works.
29 changes: 21 additions & 8 deletions content/sql-part-3/reading/left-right-joins/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ draft = false
weight = 3
+++

With different types of joins, we get different sizes of result sets. Inner joins are one of the most common joins you will see in SQL, however, with left and right outer joins, you can expand the result set to get more information if needed.

## Left Outer Join

Joining two tables with a **left outer join** gives us a result set which
includes all values in the left table and any matching records from the right
table.
Between left and right outer joins, the left outer join is more common. Joining two tables with a **left outer join** gives us a result set which includes all values in the left table and any matching records from the right
table with null values for unmatched rows.

If we use a left outer join to combine `johnson_wedding` and `johnson_vow_renewal` in a query, the result set includes all of the guests invited to the wedding and any guests who were also invited to the vow renewal.

Expand All @@ -19,16 +20,15 @@ FROM johnson_wedding
LEFT JOIN johnson_vow_renewal ON johnson_wedding.guest_id = johnson_vow_renewal.guest_id;
```

.. figure:: figures/leftouterjoin.png
:alt: Venn diagram highlighting the center and entirety of left circle.
![Venn diagram highlighting the center and entirety of left circle](pictures/leftouterjoin.png)

The Venn diagram above shows the result set highlighted in blue.

## Right Outer Join

Joining two tables with a **right outer join** gives us a result set that
includes all values in the right table and any matching records from the left
table.
table with null values for unmatched rows.

If we use a right inner join to combine `johnson_wedding` and `johnson_vow_renewal` in a query, the result set includes all of the guests that were invited to the vow renewal and any guests who were also invited to the wedding.

Expand All @@ -38,7 +38,20 @@ FROM johnson_wedding
RIGHT JOIN johnson_vow_renewal ON johnson_wedding.guest_id = johnson_vow_renewal.guest_id;
```

.. figure:: figures/rightouterjoin.png
:alt: Venn diagram highlighting the center and entirety of right circle.
![Venn diagram highlighting the center and entirety of right circle](pictures/rightouterjoin.png)

The Venn diagram above shows the result set highlighted in blue.

{{% notice blue Note %}}

One can argue that we could have gotten the same result set by switching which table was on the right and then doing a left join instead. This is one of the reasons why you might see a left join more often than a right. However, as you write different queries, you and your fellow analysts may find it helpful to stay consistent in what is considered the right table and what is considered the left.

{{% /notice %}}

## Check Your Understanding

{{% notice green Question %}}

In your own words what is the difference between a `RIGHT JOIN` and a `LEFT JOIN`?

{{% /notice %}}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions content/sql-part-3/studio/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ weight = 3

## Getting Started

For this weeks studio fork this [GitHub repository](https://github.com/launchcodeeducation/SQL-Part-3-Studio) and clone to your computer.

In Azure Data Studio, click on the open button and navigate to where you saved the git repository you just cloned and open the notebook.
For this week's studio, open up the `SQL-Part-3-Studio.ipynb` notebook in `data-analysis-projects/sql-part-3/studio` using Azure Data Studio.

## In Your Notebook

Expand Down

0 comments on commit 9f88e89

Please sign in to comment.