diff --git a/content/sql-part-3/_index.md b/content/sql-part-3/_index.md index b049a11a..9f3fd324 100644 --- a/content/sql-part-3/_index.md +++ b/content/sql-part-3/_index.md @@ -9,6 +9,8 @@ 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. @@ -16,8 +18,25 @@ weight = 20 ## 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 %}} diff --git a/content/sql-part-3/exercises/_index.md b/content/sql-part-3/exercises/_index.md index 3b918a82..39b7397f 100644 --- a/content/sql-part-3/exercises/_index.md +++ b/content/sql-part-3/exercises/_index.md @@ -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*. diff --git a/content/sql-part-3/next-steps.md b/content/sql-part-3/next-steps.md index 617177d3..94edbde8 100644 --- a/content/sql-part-3/next-steps.md +++ b/content/sql-part-3/next-steps.md @@ -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) \ No newline at end of file diff --git a/content/sql-part-3/reading/full-joins/_index.md b/content/sql-part-3/reading/full-joins/_index.md index 18a00a4a..5b04ec13 100644 --- a/content/sql-part-3/reading/full-joins/_index.md +++ b/content/sql-part-3/reading/full-joins/_index.md @@ -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 @@ -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. \ No newline at end of file + \ No newline at end of file diff --git a/content/sql-part-3/reading/full-joins/pictures/fullouterjoin.png b/content/sql-part-3/reading/full-joins/pictures/fullouterjoin.png new file mode 100644 index 00000000..5e07ff17 Binary files /dev/null and b/content/sql-part-3/reading/full-joins/pictures/fullouterjoin.png differ diff --git a/content/sql-part-3/reading/inner-joins/_index.md b/content/sql-part-3/reading/inner-joins/_index.md index ccff2c0f..5b6c3d12 100644 --- a/content/sql-part-3/reading/inner-joins/_index.md +++ b/content/sql-part-3/reading/inner-joins/_index.md @@ -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 @@ -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. \ No newline at end of file +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 %}} + + \ No newline at end of file diff --git a/content/sql-part-3/reading/inner-joins/pictures/innerjoin.png b/content/sql-part-3/reading/inner-joins/pictures/innerjoin.png new file mode 100755 index 00000000..d2f2e31f Binary files /dev/null and b/content/sql-part-3/reading/inner-joins/pictures/innerjoin.png differ diff --git a/content/sql-part-3/reading/intro/_index.md b/content/sql-part-3/reading/intro/_index.md index df7bb231..6ec5bb56 100644 --- a/content/sql-part-3/reading/intro/_index.md +++ b/content/sql-part-3/reading/intro/_index.md @@ -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. \ No newline at end of file +working with and what questions we need to answer. + +Let's dive into the specific type of joins and how each one works. \ No newline at end of file diff --git a/content/sql-part-3/reading/left-right-joins/_index.md b/content/sql-part-3/reading/left-right-joins/_index.md index 04eab9bf..08728d0d 100644 --- a/content/sql-part-3/reading/left-right-joins/_index.md +++ b/content/sql-part-3/reading/left-right-joins/_index.md @@ -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. @@ -19,8 +20,7 @@ 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. @@ -28,7 +28,7 @@ The Venn diagram above shows the result set highlighted in blue. 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. @@ -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 %}} \ No newline at end of file diff --git a/content/sql-part-3/reading/left-right-joins/pictures/leftouterjoin.png b/content/sql-part-3/reading/left-right-joins/pictures/leftouterjoin.png new file mode 100755 index 00000000..4e2ad883 Binary files /dev/null and b/content/sql-part-3/reading/left-right-joins/pictures/leftouterjoin.png differ diff --git a/content/sql-part-3/reading/left-right-joins/pictures/rightouterjoin.png b/content/sql-part-3/reading/left-right-joins/pictures/rightouterjoin.png new file mode 100755 index 00000000..f1667dc9 Binary files /dev/null and b/content/sql-part-3/reading/left-right-joins/pictures/rightouterjoin.png differ diff --git a/content/sql-part-3/studio/_index.md b/content/sql-part-3/studio/_index.md index 233ba7ef..f670997a 100644 --- a/content/sql-part-3/studio/_index.md +++ b/content/sql-part-3/studio/_index.md @@ -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