diff --git a/1_intro-sql-1.md b/1_intro-sql-1.md index 8695e96..6c83dee 100644 --- a/1_intro-sql-1.md +++ b/1_intro-sql-1.md @@ -62,8 +62,8 @@ In general, it's easy to create a new table! The syntax `CREATE TABLE AS Recreate the table called `weather` by selecting all columns in the {Download}`washington_weather.csv<./data/washington_weather.csv>` file. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_1.01.sql +# Uncomment and run to show solution +# !cat ./answers/answer_1.01.sql ``` ## Describe the table @@ -114,8 +114,8 @@ In DuckDB, strings are indicated with single quotes, like so: `'my string value' Filter rows where the station name is `'TACOMA NUMBER 1, WA US'`. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_1.02.sql +# Uncomment and run to show solution +# !cat ./answers/answer_1.02.sql ``` ### Pick the Columns that You Want @@ -131,16 +131,16 @@ SELECT name, date, temperature_min, temperature_max FROM weather; Run a `DESCRIBE` query on the `weather` table to inspect the column names, and try selecting a few different ones! For example, select the `name`, `date`, `elevation`, `precipitation`, and/or `temperature_obs` columns. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_1.03.sql +# Uncomment and run to show solution +# !cat ./answers/answer_1.03.sql ``` ```{admonition} Exercise 1.04 Select the `temperature_max` and `temperature_min` columns, and filter down to only see the rows where both of those values are under 60 and above 50. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_1.04.sql +# Uncomment and run to show solution +# !cat ./answers/answer_1.04.sql ``` ### Add a calculated Column @@ -159,16 +159,16 @@ This command creates a new column called `mean_temperature` that contains the av Add a new calculated column called `temperature_range` that gets the difference between `temperature_max` and `temperature_min` columns. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_1.05.sql +# Uncomment and run to show solution +# !cat ./answers/answer_1.05.sql ``` ```{admonition} Exercise 1.06 Create a new calculated column, `temperature_obs_celcius`, that converts the observed temperature to °C using the equation: `(32°F − 32) × 5/9 = 0°C`. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_1.06.sql +# Uncomment and run to show solution +# !cat ./answers/answer_1.06.sql ``` ### Order Rows (ORDER BY Clause) @@ -187,14 +187,14 @@ This command sorts the rows by the `precipitation` column in descending order. Use the query you created in the previous exercise and order the rows by `precipitation` in ascending order. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_1.07.sql +# Uncomment and run to show solution +# !cat ./answers/answer_1.07.sql ``` ```{admonition} Exercise 1.08 Get the station `name`, `date`, `temperature_obs` and `precipitation`, and sort the table such that the row with the lowest temperature observed is at the top of the result table. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_1.08.sql +# Uncomment and run to show solution +# !cat ./answers/answer_1.08.sql ``` diff --git a/2_intro-sql-2.md b/2_intro-sql-2.md index 39ec148..18afa81 100644 --- a/2_intro-sql-2.md +++ b/2_intro-sql-2.md @@ -64,16 +64,16 @@ SUMMARIZE ducks; Create a new table `birds_measurements` from the file `birds.csv` (this file contains the names and measurements of individuals from over 10k bird species). ``` ```{code-cell} -# Show solution -!cat ./answers/answer_2.01.sql +# Uncomment and run to show solution +# !cat ./answers/answer_2.01.sql ``` ```{admonition} Exercise 2.02 Create a new table `ducks_species` from the file `ducks.csv` (this file contains species names and common names of ducks). ``` ```{code-cell} -# Show solution -!cat ./answers/answer_2.02.sql +# Uncomment and run to show solution +# !cat ./answers/answer_2.02.sql ``` ## 1. Aggregate Functions @@ -135,15 +135,15 @@ GROUP BY Run a query that gets the average `Beak_Length_Culmen`, `Wing_Length` and `Tail_Length` for all birds. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_2.03.sql +# Uncomment and run to show solution +# !cat ./answers/answer_2.03.sql ``` ```{admonition} Exercise 2.04 Run a query that finds the average `Tail_Length` by `Species_Common_Name` and by `Country_WRI`. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_2.04.sql +# Uncomment and run to show solution +# !cat ./answers/answer_2.04.sql ``` ### Getting the 95th percentile of a column value @@ -161,16 +161,16 @@ FROM birds; Run a query that gets the 95th percentile and 99th percentile of `Beak_Length_Culmen` for all birds. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_2.05.sql +# Uncomment and run to show solution +# !cat ./answers/answer_2.05.sql ``` ```{admonition} Exercise 2.06 Run a query that gets the 99th percentile of `Wing_Length` by `Species_Common_Name`. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_2.06.sql +# Uncomment and run to show solution +# !cat ./answers/answer_2.06.sql ``` @@ -228,16 +228,16 @@ Inequality conditions are also possible (as we will see later!). Run a query that gets the name, `Beak_Length_Culmen`, `Wing_Length` and `Tail_Length` of birds that are ducks. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_2.07.sql +# Uncomment and run to show solution +# !cat ./answers/answer_2.07.sql ``` ```{admonition} Exercise 2.08 Let's run a similar query, but group the ducks by species. Run a query that gets the `Species_Common_Name`, _average_ `Beak_Length_Culmen`, `Wing_Length` and `Tail_Length` of birds that are ducks, and sort the results by `Species_Common_Name`. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_2.08.sql +# Uncomment and run to show solution +# !cat ./answers/answer_2.08.sql ``` ### LEFT OUTER JOIN (LEFT JOIN) @@ -278,8 +278,8 @@ Hint: In Python (like in SQL), nothing equals None! Just like in Python, we use the `IS` keyword to check if a value is missing. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_2.09.sql +# Uncomment and run to show solution +# !cat ./answers/answer_2.09.sql ``` ## 3. Subqueries @@ -326,16 +326,16 @@ In this example, the subquery (`SELECT QUANTILE_CONT(birds.Beak_Length_Culmen, 0 Find the duck species that have a `Wing_Length` larger than the 99th percentile of all ducks. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_2.10.sql +# Uncomment and run to show solution +# !cat ./answers/answer_2.10.sql ``` ```{admonition} Exercise 2.11 Can you find any duck species that have both a `Wing_Length` _and_ `Beak_Length_Culmen` larger than the 95th percentile of all duck species? ``` ```{code-cell} -# Show solution -!cat ./answers/answer_2.11.sql +# Uncomment and run to show solution +# !cat ./answers/answer_2.11.sql ``` @@ -345,8 +345,8 @@ NOTE: This is extra credit! Instead of individual ducks, find the duck species that _on average_ have a measured beak size that is larger than the 95th percentile of all ducks. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_2.12.sql +# Uncomment and run to show solution +# !cat ./answers/answer_2.12.sql ``` @@ -390,8 +390,8 @@ In this example, the `WITH` clause creates two temporary result sets called `duc Find the duck species that have an average `Wing_Length` larger than the 95th percentile of all duck species. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_2.13.sql +# Uncomment and run to show solution +# !cat ./answers/answer_2.13.sql ``` @@ -399,6 +399,6 @@ Find the duck species that have an average `Wing_Length` larger than the 95 What about the duck species that have both a `Wing_Length` _or_ `Beak_Length_Culmen` larger than the 95sup>th percentile of all duck species? ``` ```{code-cell} -# Show solution -!cat ./answers/answer_2.14.sql +# Uncomment and run to show solution +# !cat ./answers/answer_2.14.sql ``` diff --git a/3_sql-and-python.md b/3_sql-and-python.md index 86ff9d3..30ab999 100644 --- a/3_sql-and-python.md +++ b/3_sql-and-python.md @@ -106,8 +106,8 @@ duckdb.sql("""SELECT * FROM ducks_arrow""").arrow() Read in the birds.csv file using Apache Arrow, then use the DuckDB Python library to execute a SQL statement on that Apache Arrow table to find the maximum `wing_length` in the dataset. Output that result as an Apache Arrow table. ``` ```{code-cell} -# Show solution -!cat ./answers/answer_3.01.py +# Uncomment and run to show solution +# !cat ./answers/answer_3.01.py ``` ```{admonition} Exercise 3.02 @@ -125,8 +125,8 @@ GROUP BY Species_Common_Name ``` ```{code-cell} -# Show solution -!cat ./answers/answer_3.02.py +# Uncomment and run to show solution +# !cat ./answers/answer_3.02.py ``` ## 2. Using `ibis` with a DuckDB backend @@ -278,8 +278,8 @@ ORDER BY "t2"."Count(name)" DESC ``` ```{code-cell} -# Show solution -!cat ./answers/answer_3.03.py +# Uncomment and run to show solution +# !cat ./answers/answer_3.03.py ``` @@ -305,7 +305,6 @@ Hint 2: Ibis uses `mean` instead of `avg`! Hint 3: Ibis aggregate documentation: https://ibis-project.org/reference/expression-tables#ibis.expr.types.relations.Table.aggregate ``` ```{code-cell} -# Show solution -!cat ./answers/answer_3.04.py +# Uncomment and run to show solution +# !cat ./answers/answer_3.04.py ``` - diff --git a/notebooks/1_intro-sql-1.ipynb b/notebooks/1_intro-sql-1.ipynb index 68799ee..7256330 100644 --- a/notebooks/1_intro-sql-1.ipynb +++ b/notebooks/1_intro-sql-1.ipynb @@ -115,7 +115,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_1.01.sql" ] }, @@ -238,7 +238,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_1.02.sql" ] }, @@ -284,7 +284,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_1.03.sql" ] }, @@ -307,7 +307,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_1.04.sql" ] }, @@ -362,7 +362,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_1.05.sql" ] }, @@ -385,7 +385,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_1.06.sql" ] }, @@ -440,7 +440,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_1.07.sql" ] }, @@ -463,7 +463,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_1.08.sql" ] } diff --git a/notebooks/2_intro-sql-2.ipynb b/notebooks/2_intro-sql-2.ipynb index 9909134..f4af1cd 100644 --- a/notebooks/2_intro-sql-2.ipynb +++ b/notebooks/2_intro-sql-2.ipynb @@ -121,7 +121,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_2.01.sql" ] }, @@ -144,7 +144,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_2.02.sql" ] }, @@ -258,7 +258,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_2.03.sql" ] }, @@ -281,7 +281,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_2.04.sql" ] }, @@ -327,7 +327,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_2.05.sql" ] }, @@ -350,7 +350,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_2.06.sql" ] }, @@ -439,7 +439,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_2.07.sql" ] }, @@ -462,7 +462,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_2.08.sql" ] }, @@ -537,7 +537,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_2.09.sql" ] }, @@ -616,7 +616,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_2.10.sql" ] }, @@ -639,7 +639,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_2.11.sql" ] }, @@ -664,7 +664,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_2.12.sql" ] }, @@ -740,7 +740,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_2.13.sql" ] }, @@ -763,7 +763,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Show solution\n", + "# Uncomment and run to show solution\n", "!cat ./answers/answer_2.14.sql" ] }