Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
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

Assignment: Advanced SQL Techniques
Connect to the database
Let us first load the SQL extension and establish a connection with the database
In [1]:
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)
In [2]:
In [3]:
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.
In [19]:
* 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.
In [22]:
* 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.
In [23]:
* 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)
In [43]:
* 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)
In [ ]:
In [ ]: