0%

Coursera | Databases and SQL for Data Science (IBM) | Assignment

其他链接:
Coursera | Databases and SQL for Data Science(IBM) | Quiz
SQL | SQL+Python API相关入门教程(SQL4DS笔记)

这个assignment也也也也不难,除了一开始的数据导入可能有点小麻烦,其他和quiz差不多。

!!!!!:
这里是导入我的数据库,用自己的数据库需要先根据教程自己导入相关tables,和相关credential等操作,不赘述,这里直接运行应该也没问题。但是记得改成自己的!后面SQL语言无需变动 :)

Course Assignment

Using this Python notebook you will:

  1. Understand 3 Chicago datasets
  2. Load the 3 datasets into 3 tables in a Db2 database
  3. Execute SQL queries to answer assignment questions

Understand the datasets

To complete the assignment problems in this notebook you will be using three datasets that are available on the city of Chicago’s Data Portal:

  1. Socioeconomic Indicators in Chicago
  2. Chicago Public Schools
  3. Chicago Crime Data

Socioeconomic Indicators in Chicago

This dataset contains a selection of six socioeconomic indicators of public health significance and a “hardship index,” for each Chicago community area, for the years 2008 – 2012.

For this assignment you will use a snapshot of this dataset which can be downloaded from: Census Data

A detailed description of this dataset and the original dataset can be obtained from the Chicago Data Portal at:
https://data.cityofchicago.org/Health-Human-Services/Census-Data-Selected-socioeconomic-indicators-in-C/kn9c-c2s2

Chicago Public Schools

This dataset shows all school level performance data used to create CPS School Report Cards for the 2011-2012 school year. This dataset is provided by the city of Chicago’s Data Portal.

For this assignment you will use a snapshot of this dataset which can be downloaded from: Chicago Public School

A detailed description of this dataset and the original dataset can be obtained from the Chicago Data Portal at:
https://data.cityofchicago.org/Education/Chicago-Public-Schools-Progress-Report-Cards-2011-/9xs2-f89t

Chicago Crime Data

This dataset reflects reported incidents of crime (with the exception of murders where data exists for each victim) that occurred in the City of Chicago from 2001 to present, minus the most recent seven days.

This dataset is quite large - over 1.5GB in size with over 6.5 million rows. For the purposes of this assignment we will use a much smaller sample of this dataset which can be downloaded from: Chicago Crime Data

A detailed description of this dataset and the original dataset can be obtained from the Chicago Data Portal at:
https://data.cityofchicago.org/Public-Safety/Crimes-2001-to-present/ijzp-q8t2

Download the datasets

In many cases the dataset to be analyzed is available as a .CSV (comma separated values) file, perhaps on the internet. Click on the links below to download and save the datasets (.CSV files):

  1. CENSUS_DATA: Census Dataset

  2. CHICAGO_PUBLIC_SCHOOLS Chicago Public School

  3. CHICAGO_CRIME_DATA: Chicago Crime Data

NOTE: Ensure you have downloaded the datasets using the links above instead of directly from the Chicago Data Portal. The versions linked here are subsets of the original datasets and have some of the column names modified to be more database friendly which will make it easier to complete this assignment.

Store the datasets in database tables

To analyze the data using SQL, it first needs to be stored in the database.

While it is easier to read the dataset into a Pandas dataframe and then PERSIST it into the database as we saw in Week 3 Lab 3, it results in mapping to default datatypes which may not be optimal for SQL querying. For example a long textual field may map to a CLOB instead of a VARCHAR.

Therefore, it is highly recommended to manually load the table using the database console LOAD tool, as indicated in Week 2 Lab 1 Part II. The only difference with that lab is that in Step 5 of the instructions you will need to click on create “(+) New Table” and specify the name of the table you want to create and then click “Next”.

Now open the Db2 console, open the LOAD tool, Select / Drag the .CSV file for the first dataset, Next create a New Table, and then follow the steps on-screen instructions to load the data. Name the new tables as folows:

  1. CENSUS_DATA
  2. CHICAGO_PUBLIC_SCHOOLS
  3. CHICAGO_CRIME_DATA

Connect to the database

Let us first load the SQL extension and establish a connection with the database

1
%load_ext sql
The sql extension is already loaded. To reload it, use:
  %reload_ext sql

In the next cell enter your db2 connection string. Recall you created Service Credentials for your Db2 instance in first lab in Week 3. From the uri field of your Db2 service credentials copy everything after db2:// (except the double quote at the end) and paste it in the cell below after ibm_db_sa://

1
2
3
4
# Remember the connection string is of the format:
# %sql ibm_db_sa://my-username:my-password@my-hostname:my-port/my-db-name
# Enter the connection string for your Db2 on Cloud database instance below
%sql ibm_db_sa://wls55462:2ls9wcg2s5l7n%4008@dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net:50000/BLUDB
'Connected: wls55462@BLUDB'

Problems

Now write and execute SQL queries to solve assignment problems

Problem 1

Find the total number of crimes recorded in the CRIME table

1
%sql select COUNT(*) as TOTAL_CRIMES from CHICAGO_CRIME_DATA
total_crimes
533

Problem 2

Retrieve first 10 rows from the CRIME table

1
%sql SELECT * FROM CHICAGO_CRIME_DATA LIMIT 10
idcase_numberDATEblockiucrprimary_typedescriptionlocation_descriptionarrestdomesticbeatdistrictwardcommunity_area_numberfbicodex_coordinatey_coordinateYEARupdatedonlatitudelongitudelocation
3512276HK58771208/28/2004 05:50:56 PM047XX S KEDZIE AVE890THEFTFROM BUILDINGSMALL RETAIL STOREFALSEFALSE91191458611558381873050200402/10/2018 03:50:01 PM41.80744050-87.70395585(41.8074405, -87.703955849)
3406613HK45630606/26/2004 12:40:00 PM009XX N CENTRAL PARK AVE820THEFT$500 AND UNDEROTHERFALSEFALSE1112112723611522061906127200402/28/2018 03:56:25 PM41.89827996-87.71640551(41.898279962, -87.716405505)
8002131HT23359504/04/2011 05:45:00 AM043XX S WABASH AVE820THEFT$500 AND UNDERNURSING HOME/RETIREMENT HOMEFALSEFALSE2212338611774361876313201102/10/2018 03:50:01 PM41.81593313-87.62464213(41.815933131, -87.624642127)
7903289HT13352212/30/2010 04:30:00 PM083XX S KINGSTON AVE840THEFTFINANCIAL ID THEFT: OVER $300RESIDENCEFALSEFALSE4234746611946221850125201002/10/2018 03:50:01 PM41.74366532-87.56246276(41.743665322, -87.562462756)
10402076HZ13855102/02/2016 07:30:00 PM033XX W 66TH ST820THEFT$500 AND UNDERALLEYFALSEFALSE83181566611552401860661201602/10/2018 03:50:01 PM41.77345530-87.70648047(41.773455295, -87.706480471)
7732712HS54010609/29/2010 07:59:00 AM006XX W CHICAGO AVE810THEFTOVER $500PARKING LOT/GARAGE(NON.RESID.)FALSEFALSE1323122724611716681905607201002/10/2018 03:50:01 PM41.89644677-87.64493868(41.896446772, -87.644938678)
10769475HZ53477111/30/2016 01:15:00 AM050XX N KEDZIE AVE810THEFTOVER $500STREETFALSEFALSE1713173314611541331933314201602/10/2018 03:50:01 PM41.97284491-87.70860008(41.972844913, -87.708600079)
4494340HL79324312/16/2005 04:45:00 PM005XX E PERSHING RD860THEFTRETAIL THEFTGROCERY FOOD STORETRUEFALSE2132338611804481879234200502/28/2018 03:56:25 PM41.82387989-87.61350386(41.823879885, -87.613503857)
3778925HL14961001/28/2005 05:00:00 PM100XX S WASHTENAW AVE810THEFTOVER $500STREETFALSEFALSE2211221972611601291838040200502/28/2018 03:56:25 PM41.71128051-87.68917910(41.711280513, -87.689179097)
3324217HK36155105/13/2004 02:15:00 PM033XX W BELMONT AVE820THEFT$500 AND UNDERSMALL RETAIL STOREFALSEFALSE1733173521611535901921084200402/28/2018 03:56:25 PM41.93929582-87.71092344(41.939295821, -87.710923442)

Problem 3

How many crimes involve an arrest?

1
2
3
%sql SELECT COUNT(*) AS NUMBER_OF_ARRESTS \
FROM CHICAGO_CRIME_DATA \
WHERE ARREST = "TRUE"
number_of_arrests
163

Problem 4

Which unique types of crimes have been recorded at GAS STATION locations?

1
2
3
%sql SELECT DISTINCT PRIMARY_TYPE \
FROM CHICAGO_CRIME_DATA \
WHERE LOCATION_DESCRIPTION = 'GAS STATION'
primary_type
CRIMINAL TRESPASS
NARCOTICS
ROBBERY
THEFT

Hint: Which column lists types of crimes e.g. THEFT?

Problem 5

In the CENUS_DATA table list all Community Areas whose names start with the letter ‘B’.

1
2
3
%sql SELECT COMMUNITY_AREA_NAME \
FROM CENSUS_DATA \
WHERE COMMUNITY_AREA_NAME LIKE 'B%'
community_area_name
Belmont Cragin
Burnside
Brighton Park
Bridgeport
Beverly

Problem 6

Which schools in Community Areas 10 to 15 are healthy school certified?

1
2
3
4
%sql SELECT NAME_OF_SCHOOL \
FROM CHICAGO_PUBLIC_SCHOOLS \
WHERE COMMUNITY_AREA_NUMBER BETWEEN 10 AND 15 \
AND HEALTHY_SCHOOL_CERTIFIED = 'Yes'
name_of_school
Rufus M Hitch Elementary School

Problem 7

What is the average school Safety Score?

1
2
%sql SELECT AVG(SAFETY_SCORE) AS AVERAGE \
FROM CHICAGO_PUBLIC_SCHOOLS
average
49.504873

Problem 8

List the top 5 Community Areas by average College Enrollment [number of students]

1
2
3
4
%sql SELECT COMMUNITY_AREA_NAME, AVG(COLLEGE_ENROLLMENT) as Number_of_Students\
FROM CHICAGO_PUBLIC_SCHOOLS \
GROUP BY COMMUNITY_AREA_NAME \
ORDER BY Number_of_Students DESC LIMIT 5
community_area_namenumber_of_students
ARCHER HEIGHTS2411.500000
MONTCLARE1317.000000
WEST ELSDON1233.333333
BRIGHTON PARK1205.875000
BELMONT CRAGIN1198.833333

Problem 9

Use a sub-query to determine which Community Area has the least value for school Safety Score?

1
2
3
4
%sql SELECT COMMUNITY_AREA_NAME, SAFETY_SCORE \
FROM CHICAGO_PUBLIC_SCHOOLS \
WHERE SAFETY_SCORE = \
(SELECT MIN(SAFETY_SCORE) FROM CHICAGO_PUBLIC_SCHOOLS)
community_area_namesafety_score
WASHINGTON PARK1

Problem 10

[Without using an explicit JOIN operator] Find the Per Capita Income of the Community Area which has a school Safety Score of 1.

1
2
3
4
%sql SELECT CD.COMMUNITY_AREA_NAME, CD.PER_CAPITA_INCOME \
FROM CENSUS_DATA CD, CHICAGO_PUBLIC_SCHOOLS CPS \
WHERE CD.COMMUNITY_AREA_NUMBER = CPS.COMMUNITY_AREA_NUMBER \
AND CPS.SAFETY_SCORE = 1
community_area_nameper_capita_income
Washington Park13785
------------------   The End    Thanks for reading   ------------------