-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from LaunchCodeEducation/sql-3
SQL Part 3
- Loading branch information
Showing
13 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
+++ | ||
pre = "<b>20. </b>" | ||
chapter = true | ||
title = "SQL Part 3" | ||
date = 2024-04-02T14:13:14-05:00 | ||
draft = false | ||
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 %}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
+++ | ||
title = "Exercises" | ||
date = 2021-10-01T09:28:27-05:00 | ||
draft = false | ||
weight = 2 | ||
+++ | ||
|
||
## Getting Started | ||
|
||
Open up the `SQL-Part-3-Exercises.ipynb` notebook in `data-analysis-projects/sql-part-3/exercises` using Azure Data Studio. | ||
|
||
## In Your Notebook | ||
|
||
Connect to `BooksDB` before starting work on the exercises. | ||
|
||
## Submitting Your Work | ||
|
||
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*. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
+++ | ||
title = "Next Steps" | ||
date = 2021-10-01T09:28:27-05:00 | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
+++ | ||
title = "Reading" | ||
date = 2024-04-02T14:13:14-05:00 | ||
draft = false | ||
weight = 1 | ||
+++ | ||
|
||
## Reading Content | ||
|
||
{{% children %}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
+++ | ||
title = "Full Outer Joins" | ||
date = 2024-04-02T14:13:14-05:00 | ||
draft = false | ||
weight = 4 | ||
+++ | ||
|
||
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 | ||
`mary_events` and `leah_events`. | ||
|
||
```sql {linenos=table} | ||
SELECT * | ||
FROM mary_events | ||
FULL OUTER JOIN leah_events ON mary_events.month = leah_events.month | ||
WHERE mary_events.month = 08; | ||
``` | ||
|
||
![Venn diagram with the entirety of both circles highlighted](./pictures/fullouterjoin.png) | ||
|
||
The Venn diagram above shows the result set highlighted in blue. | ||
|
||
## Check Your Understanding | ||
|
||
{{% 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 %}} | ||
|
||
<!-- 4 --> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
+++ | ||
title = "Inner Joins" | ||
date = 2024-04-02T14:13:14-05:00 | ||
draft = false | ||
weight = 2 | ||
+++ | ||
|
||
Joining two tables with an **inner join** produces a result set that only | ||
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. | ||
|
||
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 | ||
FROM johnson_vow_renewal | ||
INNER JOIN johnson_wedding ON johnson_vow_renewal.guest_id = johnson_wedding.guest_id; | ||
``` | ||
|
||
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. | ||
|
||
![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. | ||
|
||
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
+++ | ||
title = "What is a Join?" | ||
date = 2024-04-02T14:13:14-05:00 | ||
draft = false | ||
weight = 1 | ||
+++ | ||
|
||
A **join** combines two tables into one result set. | ||
We can use joins when we want to query two tables at the same time. | ||
Whenever we join two tables, we have to specify the condition upon which the | ||
tables need to be joined. | ||
|
||
In SQL, there are four different types of joins: | ||
|
||
1. Inner Join | ||
1. Left Outer Join | ||
1. Right Outer Join | ||
1. Full Outer Join | ||
|
||
No matter which join you are working with, the general syntax for the query | ||
looks like so: | ||
|
||
```sql {linenos=table} | ||
SELECT column_name_1, column_name_2, .... | ||
FROM table_a | ||
TYPEOFJOIN JOIN table_b ON table_a.column_name_1 = table_b.column_name_1; | ||
``` | ||
|
||
In this general query, we specified what columns we want (or we could have used | ||
the `*` to read data from all columns). We have also specified that | ||
`table_a` is the *left* table and that `table_b` is the *right* table. On | ||
line 3, we need to include the type of join as part of our query with the | ||
`JOIN` keyword and the condition upon which we are joining the tables. Our | ||
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. | ||
|
||
Let's dive into the specific type of joins and how each one works. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
+++ | ||
title = "Left and Right Joins" | ||
date = 2024-04-02T14:13:14-05:00 | ||
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 | ||
|
||
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. | ||
|
||
```sql {linenos=table} | ||
SELECT last_name, first_name | ||
FROM johnson_wedding | ||
LEFT JOIN johnson_vow_renewal ON johnson_wedding.guest_id = johnson_vow_renewal.guest_id; | ||
``` | ||
|
||
![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 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. | ||
|
||
```sql {linenos=table} | ||
SELECT last_name, first_name | ||
FROM johnson_wedding | ||
RIGHT JOIN johnson_vow_renewal ON johnson_wedding.guest_id = johnson_vow_renewal.guest_id; | ||
``` | ||
|
||
![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 %}} |
Binary file added
BIN
+24.9 KB
content/sql-part-3/reading/left-right-joins/pictures/leftouterjoin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+26.7 KB
content/sql-part-3/reading/left-right-joins/pictures/rightouterjoin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
+++ | ||
title = "Studio" | ||
date = 2021-10-01T09:28:27-05:00 | ||
draft = false | ||
weight = 3 | ||
+++ | ||
|
||
## Getting Started | ||
|
||
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 | ||
|
||
The focus of this studio is to get more familiar with `JOIN` statements and to practice using them. | ||
|
||
1. Break into small groups and work together to answer the questions. | ||
1. Each member of the group should be coding along in their own notebooks. | ||
1. In Part 2 of the studio, each person will pick their own month and create two events or promotions, creating queries to support their choices in their own notebook. | ||
1. Each member of the group should have different questions and queries. | ||
|
||
## Present to the Class | ||
|
||
When the whole class comes back together, each person will present one of the events they created and the query used to solve it. | ||
|
||
## Submitting Your Work | ||
|
||
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 **Studio: SQL Part 3** and click *Submit*. |