{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction\n", "\n", "In the [Intro to SQL micro-course](https://www.kaggle.com/learn/intro-to-sql), you learned how to use [**INNER JOIN**](https://www.kaggle.com/dansbecker/joining-data) to consolidate information from two different tables. Now you'll learn about a few more types of **JOIN**, along with how to use **UNIONs** to pull information from multiple tables. \n", "\n", "Along the way, we'll work with two imaginary tables, called `owners` and `pets`. \n", "\n", "![two tables](https://storage.googleapis.com/kaggle-media/learn/images/dYVwS4T.png)\n", "\n", "Each row of the `owners` table identifies a different pet owner, where the `ID` column is a unique identifier. The `Pet_ID` column (in the `owners` table) contains the ID for the pet that belongs to the owner (this number matches the ID for the pet from the `pets` table).\n", "\n", "For example, \n", "- the `pets` table shows that Dr. Harris Bonkers is the pet with ID 1.\n", "- The `owners` table shows that Aubrey Little is the owner of the pet with ID 1.\n", "\n", "Putting these two facts together, Dr. Harris Bonkers is owned by Aubrey Little. Likewise, since Veronica Dunn does not have a corresponding `Pet_ID`, she does not have a pet. And, since 5 does not appear in the `Pet_ID` column, Maisie does not have an owner.\n", "\n", "# JOINs\n", "\n", "Recall that we can use an **INNER JOIN** to pull rows from both tables where the value in the `Pet_ID` column in the `owners` table has a match in the `ID` column of the `pets` table.\n", "\n", "![...](https://storage.googleapis.com/kaggle-media/learn/images/C5wimKT.png)\n", "\n", "In this case, Veronica Dunn and Maisie are not included in the results. But what if we instead want to create a table containing all pets, regardless of whether they have owners? Or, what if we want to combine all of the rows in both tables? In these cases, we need only use a different type of **JOIN**.\n", "\n", "For instance, to create a table containing all rows from the `owners` table, we use a **LEFT JOIN**. In this case, \"left\" refers to the table that appears before the **JOIN** in the query. (\"Right\" refers to the table that is after the **JOIN**.)\n", "\n", "![...](https://storage.googleapis.com/kaggle-media/learn/images/tnOqw2S.png)\n", "\n", "Replacing **INNER JOIN** in the query above with **LEFT JOIN** returns all rows where the two tables have matching entries, along with all of the rows in the left table (whether there is a match or not). \n", "\n", "If we instead use a **RIGHT JOIN**, we get the matching rows, along with all rows in the right table (whether there is a match or not).\n", "\n", "Finally, a **FULL JOIN** returns all rows from both tables. Note that in general, any row that does not have a match in both tables will have NULL entries for the missing values. You can see this in the image below.\n", "\n", "![...](https://storage.googleapis.com/kaggle-media/learn/images/1Dvmg8S.png)\n", "\n", "\n", "# UNIONs\n", "\n", "As you've seen, **JOINs** horizontally combine results from different tables. If you instead would like to vertically concatenate columns, you can do so with a **UNION**. The example query below combines the `Age` columns from both tables.\n", "\n", "![...](https://storage.googleapis.com/kaggle-media/learn/images/oa6VDig.png)\n", "\n", "Note that with a **UNION**, the data types of both columns must be the same, but the column names can be different. (So, for instance, we cannot take the **UNION** of the `Age` column from the `owners` table and the `Pet_Name` column from the `pets` table.) \n", "\n", "We use **UNION ALL** to include duplicate values - you'll notice that `9` appears in both the `owners` table and the `pets` table, and shows up twice in the concatenated results. If you'd like to drop duplicate values, you need only change **UNION ALL** in the query to **UNION DISTINCT**.\n", "\n", "# Example\n", "\n", "We'll work with the [Hacker News](https://www.kaggle.com/hacker-news/hacker-news) dataset. We begin by reviewing the first several rows of the `comments` table. (_The corresponding code is hidden, but you can un-hide it by clicking on the \"Code\" button below._)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_kg_hide-input": true }, "outputs": [], "source": [ "#$HIDE_INPUT$\n", "from google.cloud import bigquery\n", "\n", "# Create a \"Client\" object\n", "client = bigquery.Client()\n", "\n", "# Construct a reference to the \"hacker_news\" dataset\n", "dataset_ref = client.dataset(\"hacker_news\", project=\"bigquery-public-data\")\n", "\n", "# API request - fetch the dataset\n", "dataset = client.get_dataset(dataset_ref)\n", "\n", "# Construct a reference to the \"comments\" table\n", "table_ref = dataset_ref.table(\"comments\")\n", "\n", "# API request - fetch the table\n", "table = client.get_table(table_ref)\n", "\n", "# Preview the first five lines of the table\n", "client.list_rows(table, max_results=5).to_dataframe()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You'll also work with the `stories` table." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_kg_hide-input": true }, "outputs": [], "source": [ "# Construct a reference to the \"stories\" table\n", "table_ref = dataset_ref.table(\"stories\")\n", "\n", "# API request - fetch the table\n", "table = client.get_table(table_ref)\n", "\n", "# Preview the first five lines of the table\n", "client.list_rows(table, max_results=5).to_dataframe()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since you are already familiar with **JOINs** from the [Intro to SQL micro-course](https://www.kaggle.com/learn/intro-to-sql), we'll work with a relatively complex example of a JOIN that uses a [common table expression (CTE)](https://www.kaggle.com/dansbecker/as-with).\n", "\n", "The query below pulls information from the `stories` and `comments` tables to create a table showing all stories posted on January 1, 2012, along with the corresponding number of comments. We use a **LEFT JOIN** so that the results include stories that didn't receive any comments." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Query to select all stories posted on January 1, 2012, with number of comments\n", "join_query = \"\"\"\n", " WITH c AS\n", " (\n", " SELECT parent, COUNT(*) as num_comments\n", " FROM `bigquery-public-data.hacker_news.comments` \n", " GROUP BY parent\n", " )\n", " SELECT s.id as story_id, s.by, s.title, c.num_comments\n", " FROM `bigquery-public-data.hacker_news.stories` AS s\n", " LEFT JOIN c\n", " ON s.id = c.parent\n", " WHERE EXTRACT(DATE FROM s.time_ts) = '2012-01-01'\n", " ORDER BY c.num_comments DESC\n", " \"\"\"\n", "\n", "# Run the query, and return a pandas DataFrame\n", "join_result = client.query(join_query).result().to_dataframe()\n", "join_result.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since the results are ordered by the `num_comments` column, stories without comments appear at the end of the DataFrame. (Remember that **NaN** stands for \"not a number\".)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# None of these stories received any comments\n", "join_result.tail()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we write a query to select all usernames corresponding to users who wrote stories or comments on January 1, 2014. We use **UNION DISTINCT** (instead of **UNION ALL**) to ensure that each user appears in the table at most once." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Query to select all users who posted stories or comments on January 1, 2014\n", "union_query = \"\"\"\n", " SELECT c.by\n", " FROM `bigquery-public-data.hacker_news.comments` AS c\n", " WHERE EXTRACT(DATE FROM c.time_ts) = '2014-01-01'\n", " UNION DISTINCT\n", " SELECT s.by\n", " FROM `bigquery-public-data.hacker_news.stories` AS s\n", " WHERE EXTRACT(DATE FROM s.time_ts) = '2014-01-01'\n", " \"\"\"\n", "\n", "# Run the query, and return a pandas DataFrame\n", "union_result = client.query(union_query).result().to_dataframe()\n", "union_result.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get the number of users who posted on January 1, 2014, we need only take the length of the DataFrame." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Number of users who posted stories or comments on January 1, 2014\n", "len(union_result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Your turn \n", "\n", "Use what you've learned to **[pull information from multiple tables](#$NEXT_NOTEBOOK_URL$)**." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 1 }