Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "nbformat_minor": 1,
- "cells": [
- {
- "execution_count": 6,
- "cell_type": "code",
- "metadata": {},
- "outputs": [],
- "source": "#load SQL magic\n%load_ext sql"
- },
- {
- "execution_count": 7,
- "cell_type": "code",
- "metadata": {},
- "outputs": [
- {
- "execution_count": 7,
- "metadata": {},
- "data": {
- "text/plain": "'Connected: mgb17542@BLUDB'"
- },
- "output_type": "execute_result"
- }
- ],
- "source": "# Enter the connection string for Db2 on Cloud database\n%sql ibm_db_sa://mgb17542:dkw5hl3%5Eqm57tf2c@dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net:50000/BLUDB"
- },
- {
- "execution_count": 8,
- "cell_type": "code",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": " * ibm_db_sa://mgb17542:***@dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net:50000/BLUDB\nDone.\n"
- },
- {
- "execution_count": 8,
- "metadata": {},
- "data": {
- "text/html": "<table>\n <tr>\n <th>Rows in Census Data</th>\n </tr>\n <tr>\n <td>78</td>\n </tr>\n</table>",
- "text/plain": "[(Decimal('78'),)]"
- },
- "output_type": "execute_result"
- }
- ],
- "source": "# Rows in Census Data (Socieconimic Indicators)\n%sql select Count(*) as \"Rows in Census Data\" from CENSUS_DATA"
- },
- {
- "execution_count": 9,
- "cell_type": "code",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": " * ibm_db_sa://mgb17542:***@dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net:50000/BLUDB\nDone.\n"
- },
- {
- "execution_count": 9,
- "metadata": {},
- "data": {
- "text/html": "<table>\n <tr>\n <th>Rows in Public Schools</th>\n </tr>\n <tr>\n <td>566</td>\n </tr>\n</table>",
- "text/plain": "[(Decimal('566'),)]"
- },
- "output_type": "execute_result"
- }
- ],
- "source": "# Rows in Public Schools\n%sql select Count(*) as \"Rows in Public Schools\" from CHICAGO_PUBLIC_SCHOOLS"
- },
- {
- "execution_count": 10,
- "cell_type": "code",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": " * ibm_db_sa://mgb17542:***@dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net:50000/BLUDB\nDone.\n"
- },
- {
- "execution_count": 10,
- "metadata": {},
- "data": {
- "text/html": "<table>\n <tr>\n <th>Rows in Crime Data</th>\n </tr>\n <tr>\n <td>533</td>\n </tr>\n</table>",
- "text/plain": "[(Decimal('533'),)]"
- },
- "output_type": "execute_result"
- }
- ],
- "source": "# Rows in Crime Data\n%sql select Count(*) as \"Rows in Crime Data\" from CHICAGO_CRIME_DATA"
- },
- {
- "execution_count": 20,
- "cell_type": "code",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": " * ibm_db_sa://mgb17542:***@dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net:50000/BLUDB\nDone.\n"
- },
- {
- "execution_count": 20,
- "metadata": {},
- "data": {
- "text/html": "<table>\n <tr>\n <th>Avg College Enrollment</th>\n <th>community_area_name</th>\n </tr>\n <tr>\n <td>858.000000</td>\n <td>ALBANY PARK</td>\n </tr>\n <tr>\n <td>2411.500000</td>\n <td>ARCHER HEIGHTS</td>\n </tr>\n <tr>\n <td>486.000000</td>\n <td>ARMOUR SQUARE</td>\n </tr>\n <tr>\n <td>810.375000</td>\n <td>ASHBURN</td>\n </tr>\n <tr>\n <td>417.500000</td>\n <td>AUBURN GRESHAM</td>\n </tr>\n <tr>\n <td>475.347826</td>\n <td>AUSTIN</td>\n </tr>\n <tr>\n <td>507.333333</td>\n <td>AVALON PARK</td>\n </tr>\n <tr>\n <td>910.000000</td>\n <td>AVONDALE</td>\n </tr>\n <tr>\n <td>1198.833333</td>\n <td>BELMONT CRAGIN</td>\n </tr>\n <tr>\n <td>409.000000</td>\n <td>BEVERLY</td>\n </tr>\n</table>",
- "text/plain": "[(Decimal('858.000000'), 'ALBANY PARK'),\n (Decimal('2411.500000'), 'ARCHER HEIGHTS'),\n (Decimal('486.000000'), 'ARMOUR SQUARE'),\n (Decimal('810.375000'), 'ASHBURN'),\n (Decimal('417.500000'), 'AUBURN GRESHAM'),\n (Decimal('475.347826'), 'AUSTIN'),\n (Decimal('507.333333'), 'AVALON PARK'),\n (Decimal('910.000000'), 'AVONDALE'),\n (Decimal('1198.833333'), 'BELMONT CRAGIN'),\n (Decimal('409.000000'), 'BEVERLY')]"
- },
- "output_type": "execute_result"
- }
- ],
- "source": "# Average college enrollments by community area\n%sql select AVG(college_enrollment) as \"Avg College Enrollment\", community_area_name from CHICAGO_PUBLIC_SCHOOLS group by community_area_name fetch first 10 rows only"
- },
- {
- "execution_count": 21,
- "cell_type": "code",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": " * ibm_db_sa://mgb17542:***@dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net:50000/BLUDB\nDone.\n"
- },
- {
- "execution_count": 21,
- "metadata": {},
- "data": {
- "text/html": "<table>\n <tr>\n <th>1</th>\n </tr>\n <tr>\n <td>16</td>\n </tr>\n</table>",
- "text/plain": "[(Decimal('16'),)]"
- },
- "output_type": "execute_result"
- }
- ],
- "source": "# Number of schools that are healthy school certified in Chicago\n%sql select Count(*) from CHICAGO_PUBLIC_SCHOOLS where healthy_school_certified='Yes'"
- },
- {
- "execution_count": 22,
- "cell_type": "code",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": " * ibm_db_sa://mgb17542:***@dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net:50000/BLUDB\nDone.\n"
- },
- {
- "execution_count": 22,
- "metadata": {},
- "data": {
- "text/html": "<table>\n <tr>\n <th>1</th>\n </tr>\n <tr>\n <td>6</td>\n </tr>\n</table>",
- "text/plain": "[(Decimal('6'),)]"
- },
- "output_type": "execute_result"
- }
- ],
- "source": "# How many observations have a Location Description value of GAS STATION?\n%sql select Count(*) from CHICAGO_CRIME_DATA where location_description='GAS STATION'"
- },
- {
- "execution_count": 23,
- "cell_type": "code",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": " * ibm_db_sa://mgb17542:***@dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net:50000/BLUDB\nDone.\n"
- },
- {
- "execution_count": 23,
- "metadata": {},
- "data": {
- "text/html": "<table>\n <tr>\n <th>1</th>\n <th>community_area_name</th>\n </tr>\n <tr>\n <td>23</td>\n <td>AUSTIN</td>\n </tr>\n <tr>\n <td>22</td>\n <td>SOUTH LAWNDALE</td>\n </tr>\n <tr>\n <td>20</td>\n <td>WEST TOWN</td>\n </tr>\n <tr>\n <td>17</td>\n <td>ENGLEWOOD</td>\n </tr>\n <tr>\n <td>16</td>\n <td>NEAR WEST SIDE</td>\n </tr>\n <tr>\n <td>16</td>\n <td>NORTH LAWNDALE</td>\n </tr>\n <tr>\n <td>13</td>\n <td>EAST GARFIELD PARK</td>\n </tr>\n <tr>\n <td>13</td>\n <td>ROSELAND</td>\n </tr>\n <tr>\n <td>13</td>\n <td>NEW CITY</td>\n </tr>\n <tr>\n <td>13</td>\n <td>HUMBOLDT PARK</td>\n </tr>\n</table>",
- "text/plain": "[(Decimal('23'), 'AUSTIN'),\n (Decimal('22'), 'SOUTH LAWNDALE'),\n (Decimal('20'), 'WEST TOWN'),\n (Decimal('17'), 'ENGLEWOOD'),\n (Decimal('16'), 'NEAR WEST SIDE'),\n (Decimal('16'), 'NORTH LAWNDALE'),\n (Decimal('13'), 'EAST GARFIELD PARK'),\n (Decimal('13'), 'ROSELAND'),\n (Decimal('13'), 'NEW CITY'),\n (Decimal('13'), 'HUMBOLDT PARK')]"
- },
- "output_type": "execute_result"
- }
- ],
- "source": "# Retrieve a list of the top 10 community areas which have most number of schools and sorted in descending order\n%sql select Count(*), community_area_name from CHICAGO_PUBLIC_SCHOOLS group by community_area_name order by 1 desc fetch first 10 rows only"
- },
- {
- "execution_count": 24,
- "cell_type": "code",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": " * ibm_db_sa://mgb17542:***@dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net:50000/BLUDB\nDone.\n"
- },
- {
- "execution_count": 24,
- "metadata": {},
- "data": {
- "text/html": "<table>\n <tr>\n <th>1</th>\n </tr>\n <tr>\n <td>24</td>\n </tr>\n</table>",
- "text/plain": "[(Decimal('24'),)]"
- },
- "output_type": "execute_result"
- }
- ],
- "source": "# How many observations have value MOTOR VEHICLE THEFT in the Primary Type variable?\n%sql select Count(*) from CHICAGO_CRIME_DATA where Primary_Type='MOTOR VEHICLE THEFT'"
- },
- {
- "execution_count": 28,
- "cell_type": "code",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": " * ibm_db_sa://mgb17542:***@dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net:50000/BLUDB\nDone.\n"
- },
- {
- "execution_count": 28,
- "metadata": {},
- "data": {
- "text/html": "<table>\n <tr>\n <th>min_average_student_attendance</th>\n </tr>\n <tr>\n <td>86.10%</td>\n </tr>\n</table>",
- "text/plain": "[('86.10%',)]"
- },
- "output_type": "execute_result"
- }
- ],
- "source": "# Find the minimum \u201cAverage Student Attendance\u201d for the community area where hardship is 96\n%sql select MIN(\"AVERAGE_STUDENT_ATTENDANCE\") as MIN_AVERAGE_STUDENT_ATTENDANCE from CHICAGO_PUBLIC_SCHOOLS INNER JOIN CENSUS_DATA ON CHICAGO_PUBLIC_SCHOOLS.\"COMMUNITY_AREA_NUMBER\"=CENSUS_DATA.\"COMMUNITY_AREA_NUMBER\" Where CENSUS_DATA.\"HARDSHIP_INDEX\"='96' \\\nGroup by \"AVERAGE_STUDENT_ATTENDANCE\" limit 1"
- },
- {
- "execution_count": null,
- "cell_type": "code",
- "metadata": {},
- "outputs": [],
- "source": ""
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3.5",
- "name": "python3",
- "language": "python"
- },
- "language_info": {
- "mimetype": "text/x-python",
- "nbconvert_exporter": "python",
- "version": "3.5.5",
- "name": "python",
- "file_extension": ".py",
- "pygments_lexer": "ipython3",
- "codemirror_mode": {
- "version": 3,
- "name": "ipython"
- }
- }
- },
- "nbformat": 4
- }
Add Comment
Please, Sign In to add comment