diff --git a/content/sql-part-3/_index.md b/content/sql-part-3/_index.md index d49945a7..b049a11a 100644 --- a/content/sql-part-3/_index.md +++ b/content/sql-part-3/_index.md @@ -16,6 +16,8 @@ weight = 20 ## Key Terminology +1. join +1. inner join ## Content Links {{% children %}} diff --git a/content/sql-part-3/exercises/_index.md b/content/sql-part-3/exercises/_index.md index af0a9afa..3b918a82 100644 --- a/content/sql-part-3/exercises/_index.md +++ b/content/sql-part-3/exercises/_index.md @@ -5,4 +5,16 @@ draft = false weight = 2 +++ -## Next Steps +## Getting Started + +Fork this [GitHub repository](https://github.com/launchcodeeducation/SQL-Part-3-Exercises) and clone to your computer. + +## In Your Notebook + +Open Azure Data Studio and connect to the `BookDB`. + +## 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*. diff --git a/content/sql-part-3/next-steps.md b/content/sql-part-3/next-steps.md index 84511e46..617177d3 100644 --- a/content/sql-part-3/next-steps.md +++ b/content/sql-part-3/next-steps.md @@ -5,4 +5,4 @@ draft = false weight = 4 +++ -## Next Steps +1. [Joins](https://learn.microsoft.com/en-us/sql/relational-databases/performance/joins?view=sql-server-ver16) diff --git a/content/sql-part-3/reading/full-joins/_index.md b/content/sql-part-3/reading/full-joins/_index.md index 13359fcc..18a00a4a 100644 --- a/content/sql-part-3/reading/full-joins/_index.md +++ b/content/sql-part-3/reading/full-joins/_index.md @@ -5,4 +5,36 @@ draft = false weight = 4 +++ -Finally, write a full outer join \ No newline at end of file +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. + +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; +``` + +.. figure:: figures/fullouterjoin.png + :alt: Venn diagram with the entirety of both circles highlighted. + +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: + +```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; +``` + +`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 diff --git a/content/sql-part-3/reading/inner-joins/_index.md b/content/sql-part-3/reading/inner-joins/_index.md index 14fb89c7..ccff2c0f 100644 --- a/content/sql-part-3/reading/inner-joins/_index.md +++ b/content/sql-part-3/reading/inner-joins/_index.md @@ -5,4 +5,20 @@ draft = false weight = 2 +++ -Write an inner join and be able to explain what makes it special \ No newline at end of file +Joining two tables with an **inner join** produces a result set that only +includes the values that are present in *both* tables. + +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. + +```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. + +.. figure:: figures/innerjoin.png + :alt: Venn diagram highlighting just the center where the two circles meet. + +The Venn diagram above shows the result set highlighted in blue. \ No newline at end of file diff --git a/content/sql-part-3/reading/intro/_index.md b/content/sql-part-3/reading/intro/_index.md index 2ce719a8..df7bb231 100644 --- a/content/sql-part-3/reading/intro/_index.md +++ b/content/sql-part-3/reading/intro/_index.md @@ -5,4 +5,33 @@ draft = false weight = 1 +++ -What is a join and why would we need them? \ No newline at end of file +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. \ 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 e44cd2c1..04eab9bf 100644 --- a/content/sql-part-3/reading/left-right-joins/_index.md +++ b/content/sql-part-3/reading/left-right-joins/_index.md @@ -5,4 +5,40 @@ draft = false weight = 3 +++ -Write a left and right join and be able to explain why one might use them \ No newline at end of file +## 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. + +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; +``` + +.. figure:: figures/leftouterjoin.png + :alt: Venn diagram highlighting the center and entirety of left circle. + +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. + +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; +``` + +.. figure:: figures/rightouterjoin.png + :alt: Venn diagram highlighting the center and entirety of right circle. + +The Venn diagram above shows the result set highlighted in blue. diff --git a/content/sql-part-3/studio/_index.md b/content/sql-part-3/studio/_index.md index 95e3ec2d..233ba7ef 100644 --- a/content/sql-part-3/studio/_index.md +++ b/content/sql-part-3/studio/_index.md @@ -5,4 +5,27 @@ draft = false weight = 3 +++ -## Studio +## 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. + +## 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*. \ No newline at end of file