You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: instructors/instructor-notes.md
-96Lines changed: 0 additions & 96 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -240,102 +240,6 @@ plt.show()
240
240
241
241
[This page][matplotlib-mathtext] contains more information.
242
242
243
-
## 09-working-with-sql
244
-
245
-
### Challenge - SQL
246
-
247
-
- Create a query that contains survey data collected between 1998 - 2001 for observations of sex "male" or "female" that includes observation's genus and species and site type for the sample. How many records are returned?
248
-
249
-
```python
250
-
#Connect to the database
251
-
con = sqlite3.connect("data/portal_mammals.sqlite")
252
-
253
-
cur = con.cursor()
254
-
255
-
# Return all results of query: year, plot type (site type), genus, species and sex
256
-
# from the join of the tables surveys, plots and species, for the years 1998-2001 where sex is 'M' or 'F'.
FROM surveys INNER JOIN plots ON surveys.plot_id = plots.plot_id INNER JOIN species ON \
259
-
surveys.species_id = species.species_id WHERE surveys.year>=1998 AND surveys.year<=2001 \
260
-
AND ( surveys.sex = "M" OR surveys.sex = "F")')
261
-
262
-
print(len(cur.fetchall()))
263
-
264
-
# Close the connection
265
-
con.close()
266
-
```
267
-
268
-
```output
269
-
5546
270
-
```
271
-
272
-
Answer: 5546 records are found.
273
-
274
-
- Create a dataframe that contains the total number of observations (count) made for all years, and sum of observation weights for each site, ordered by site ID.
275
-
276
-
This question is a little ambiguous but we could e.g. do two SQL queries into dataframes, then pivot the second and merge them to create a table of observation count and plot total weight per year. The PIVOT operation could alternatively be performed in SQL.
277
-
278
-
```python
279
-
import pandas as pd
280
-
import sqlite3
281
-
282
-
# Create two sqlite queries results, read as pandas DataFrame
283
-
# Include 'year' in both queries so we have something to merge (join) on.
284
-
con = sqlite3.connect("data/portal_mammals.sqlite")
285
-
df1 = pd.read_sql_query("SELECT year,COUNT(*) FROM surveys GROUP BY year", con)
286
-
df2 = pd.read_sql_query("SELECT year,plot_id,SUM(weight) FROM surveys GROUP BY \
287
-
year,plot_id ORDER BY plot_id ASC",con)
288
-
289
-
# Turn the plot_id column values into column names by pivoting
- What are some of the reasons you might want to save the results of your queries back into the database? What are some of the reasons you might avoid doing this?
335
-
336
-
If the database is shared with others and common queries (and potentially data corrections) are likely to be required by many it may be efficient for one person to perform the work and save it back to the database as a new table so others can access the results directly instead of performing the query themselves, particularly if it is complex.
337
-
However, we might avoid doing this if the database is an authoritative source (potentially version controlled) which should not be modified by users. Instead, we might save the qeury results to a new database that is more appropriate for downstream work.
0 commit comments