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
@@ -749,102 +749,6 @@ plt.show()
749
749
750
750
[This page][matplotlib-mathtext] contains more information.
751
751
752
-
## 09-working-with-sql
753
-
754
-
### Challenge - SQL
755
-
756
-
- 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?
757
-
758
-
```python
759
-
#Connect to the database
760
-
con = sqlite3.connect("data/portal_mammals.sqlite")
761
-
762
-
cur = con.cursor()
763
-
764
-
# Return all results of query: year, plot type (site type), genus, species and sex
765
-
# 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 \
768
-
surveys.species_id = species.species_id WHERE surveys.year>=1998 AND surveys.year<=2001 \
769
-
AND ( surveys.sex = "M" OR surveys.sex = "F")')
770
-
771
-
print(len(cur.fetchall()))
772
-
773
-
# Close the connection
774
-
con.close()
775
-
```
776
-
777
-
```output
778
-
5546
779
-
```
780
-
781
-
Answer: 5546 records are found.
782
-
783
-
- 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.
784
-
785
-
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.
786
-
787
-
```python
788
-
import pandas as pd
789
-
import sqlite3
790
-
791
-
# Create two sqlite queries results, read as pandas DataFrame
792
-
# Include 'year' in both queries so we have something to merge (join) on.
793
-
con = sqlite3.connect("data/portal_mammals.sqlite")
794
-
df1 = pd.read_sql_query("SELECT year,COUNT(*) FROM surveys GROUP BY year", con)
795
-
df2 = pd.read_sql_query("SELECT year,plot_id,SUM(weight) FROM surveys GROUP BY \
796
-
year,plot_id ORDER BY plot_id ASC",con)
797
-
798
-
# 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?
844
-
845
-
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.
846
-
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