CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
DanielBarnes18

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: DanielBarnes18/IBM-Data-Science-Professional-Certificate
Path: blob/main/06. Databases and SQL for Data Science with Python/06. Bonus Module - Advanced SQL for Data Engineering/Advanced SQL Techniques.ipynb
Views: 5163
Kernel: Python 3
cognitiveclass.ai logo

Assignment: Advanced SQL Techniques

Connect to the database

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

!pip install sqlalchemy==1.3.9 !pip install ibm_db_sa
Requirement already satisfied: sqlalchemy==1.3.9 in c:\users\dabarnes\anaconda3\lib\site-packages (1.3.9) Requirement already satisfied: ibm_db_sa in c:\users\dabarnes\anaconda3\lib\site-packages (0.3.6) Requirement already satisfied: sqlalchemy>=0.7.3 in c:\users\dabarnes\anaconda3\lib\site-packages (from ibm_db_sa) (1.3.9) Requirement already satisfied: ibm-db>=2.0.0 in c:\users\dabarnes\anaconda3\lib\site-packages (from ibm_db_sa) (3.0.4)
%load_ext sql
# 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://kfm42587:6zkhf3chpx0-m9cl@dashdb-txn-sbox-yp-lon02-13.services.eu-gb.bluemix.net:50000/BLUDB
DB2/LINUXX8664

Exercise 1: Using Joins

You have been asked to produce some reports about the communities and crimes in the Chicago area. You will need to use SQL join queries to access the data stored across multiple tables.

Question 1

Write and execute a SQL query to list the school names, community names and average attendance for communities with a hardship index of 98.
%%sql SELECT S.NAME_OF_SCHOOL, S.COMMUNITY_AREA_NAME, S.AVERAGE_STUDENT_ATTENDANCE FROM CHICAGO_PUBLIC_SCHOOLS S LEFT OUTER JOIN CENSUS_DATA C ON S.COMMUNITY_AREA_NUMBER = C.COMMUNITY_AREA_NUMBER WHERE C.HARDSHIP_INDEX = 98.0
* ibm_db_sa://kfm42587:***@dashdb-txn-sbox-yp-lon02-13.services.eu-gb.bluemix.net:50000/BLUDB Done.

Question 2

Write and execute a SQL query to list all crimes that took place at a school. Include case number, crime type and community name.

%%sql SELECT CR.CASE_NUMBER, CR.PRIMARY_TYPE, CE.COMMUNITY_AREA_NAME FROM CHICAGO_CRIME_DATA CR LEFT OUTER JOIN CENSUS_DATA CE ON CR.COMMUNITY_AREA_NUMBER = CE.COMMUNITY_AREA_NUMBER WHERE CR.LOCATION_DESCRIPTION LIKE '%SCHOOL%'
* ibm_db_sa://kfm42587:***@dashdb-txn-sbox-yp-lon02-13.services.eu-gb.bluemix.net:50000/BLUDB Done.

Exercise 2: Creating a View

Question 1

Write and execute a SQL statement to create a view showing the columns listed in the following table, with new column names as shown in the second column.

Write and execute a SQL statement that returns all of the columns from the view.

Write and execute a SQL statement that returns just the school name and leaders rating from the view.

%%sql CREATE VIEW AS SELECT FROM CHICAGO_PUBLIC_SCHOOLS
* ibm_db_sa://kfm42587:***@dashdb-txn-sbox-yp-lon02-13.services.eu-gb.bluemix.net:50000/BLUDB (ibm_db_dbi.ProgrammingError) ibm_db_dbi::ProgrammingError: Statement Execute Failed: [IBM][CLI Driver][DB2/LINUXX8664] SQL0104N An unexpected token "SELECT" was found following "CREATE VIEW AS ". Expected tokens may include: "AS". SQLSTATE=42601\r SQLCODE=-104 [SQL: CREATE VIEW AS SELECT FROM CHICAGO_PUBLIC_SCHOOLS] (Background on this error at: http://sqlalche.me/e/f405)
%%sql --#SET TERMINATOR@ CREATE PROCEDURE UPDATE_LEADERS_SCORE(IN in_School_ID INTEGER, IN in_Leader_Score INTEGER) BEGIN UPDATE SCHOOLS SET "Leaders_Score" = in_Leader_Score WHERE "School_ID" = in_School_ID; IF in_Leader_Score > 0 AND in_Leader_Score < 20 THEN UPDATE CHICAGO_PUBLIC_SCHOOLS SET "Leaders_Icon" = 'Very Weak'; ELSEIF in_Leader_Score < 40 THEN UPDATE CHICAGO_PUBLIC_SCHOOLS SET "Leaders_Icon" = 'Weak'; ELSEIF in_Leader_Score < 60 THEN UPDATE CHICAGO_PUBLIC_SCHOOLS SET "Leaders_Icon" = 'Average'; ELSEIF in_Leader_Score < 80 THEN UPDATE CHICAGO_PUBLIC_SCHOOLS SET "Leaders_Icon" = 'Strong'; ELSEIF in_Leader_Score < 100 THEN UPDATE CHICAGO_PUBLIC_SCHOOLS SET "Leaders_Icon" = 'Very Strong'; END IF; END@
* ibm_db_sa://kfm42587:***@dashdb-txn-sbox-yp-lon02-13.services.eu-gb.bluemix.net:50000/BLUDB (ibm_db_dbi.ProgrammingError) ibm_db_dbi::ProgrammingError: Statement Execute Failed: [IBM][CLI Driver][DB2/LINUXX8664] SQL0104N An unexpected token "END-OF-STATEMENT" was found following "l_ID" = in_School_ID". Expected tokens may include: "<psm_semicolon>". LINE NUMBER=5. SQLSTATE=42601\r SQLCODE=-104 [SQL: --#SET TERMINATOR@ CREATE PROCEDURE UPDATE_LEADERS_SCORE(IN in_School_ID INTEGER, IN in_Leader_Score INTEGER) BEGIN UPDATE SCHOOLS SET "Leaders_Score" = in_Leader_Score WHERE "School_ID" = in_School_ID;] (Background on this error at: http://sqlalche.me/e/f405)