Skip to content

Commit

Permalink
comment out solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
guenp committed Jul 9, 2024
1 parent 2e82930 commit 45e8d8e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 75 deletions.
32 changes: 16 additions & 16 deletions 1_intro-sql-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ In general, it's easy to create a new table! The syntax `CREATE TABLE <name> 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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
```
56 changes: 28 additions & 28 deletions 2_intro-sql-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 95<sup>th</sup> percentile of a column value
Expand All @@ -161,16 +161,16 @@ FROM birds;
Run a query that gets the 95<sup>th</sup> percentile and 99<sup>th</sup> 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 99<sup>th</sup> 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
```


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 99<sup>th</sup> 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 95<sup>th</sup> 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
```


Expand All @@ -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 95<sup>th</sup> 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
```


Expand Down Expand Up @@ -390,15 +390,15 @@ 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 95<sup>th</sup> 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
```


```{admonition} Exercise 2.14
What about the duck species that have both a `Wing_Length` _or_ `Beak_Length_Culmen` larger than the 95sup>th</sup> 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
```
17 changes: 8 additions & 9 deletions 3_sql-and-python.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
```


Expand All @@ -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
```

16 changes: 8 additions & 8 deletions notebooks/1_intro-sql-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Show solution\n",
"# Uncomment and run to show solution\n",
"!cat ./answers/answer_1.01.sql"
]
},
Expand Down Expand Up @@ -238,7 +238,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Show solution\n",
"# Uncomment and run to show solution\n",
"!cat ./answers/answer_1.02.sql"
]
},
Expand Down Expand Up @@ -284,7 +284,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Show solution\n",
"# Uncomment and run to show solution\n",
"!cat ./answers/answer_1.03.sql"
]
},
Expand All @@ -307,7 +307,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Show solution\n",
"# Uncomment and run to show solution\n",
"!cat ./answers/answer_1.04.sql"
]
},
Expand Down Expand Up @@ -362,7 +362,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Show solution\n",
"# Uncomment and run to show solution\n",
"!cat ./answers/answer_1.05.sql"
]
},
Expand All @@ -385,7 +385,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Show solution\n",
"# Uncomment and run to show solution\n",
"!cat ./answers/answer_1.06.sql"
]
},
Expand Down Expand Up @@ -440,7 +440,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Show solution\n",
"# Uncomment and run to show solution\n",
"!cat ./answers/answer_1.07.sql"
]
},
Expand All @@ -463,7 +463,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Show solution\n",
"# Uncomment and run to show solution\n",
"!cat ./answers/answer_1.08.sql"
]
}
Expand Down
Loading

0 comments on commit 45e8d8e

Please sign in to comment.