diff --git a/module1-introduction-to-sql/buddymove_holidayiq.py b/module1-introduction-to-sql/buddymove_holidayiq.py new file mode 100644 index 00000000..adff4c47 --- /dev/null +++ b/module1-introduction-to-sql/buddymove_holidayiq.py @@ -0,0 +1,15 @@ +import sqlite3 +import pandas as pd + +connect = sqlite3.connect('buddymove_holidayiq.sqlite3') +curs = connect.cursor() + +df = pd.read_csv('buddymove_holidayiq.csv') + +df.to_sql('buddy', connect) + +curs.execute('SELECT COUNT(*) FROM buddy') +print(f'There are {curs.fetchall()[0][0]} rows in the database.') + +curs.execute('SELECT COUNT(*) FROM buddy WHERE Nature > 99 AND Shopping > 99') +print(f'{curs.fetchall()[0][0]} users reviewed at least 100 in both Nature and Shopping.') \ No newline at end of file diff --git a/module1-introduction-to-sql/buddymove_holidayiq.sqlite3 b/module1-introduction-to-sql/buddymove_holidayiq.sqlite3 new file mode 100644 index 00000000..b4e7a00e Binary files /dev/null and b/module1-introduction-to-sql/buddymove_holidayiq.sqlite3 differ diff --git a/module1-introduction-to-sql/rpg_queries.py b/module1-introduction-to-sql/rpg_queries.py new file mode 100644 index 00000000..efaadfdc --- /dev/null +++ b/module1-introduction-to-sql/rpg_queries.py @@ -0,0 +1,132 @@ +import sqlite3 + + +def connect_to_db(db_name='rpg_db.sqlite3'): + return sqlite3.connect(db_name) + +def execute_query(cursor, query): + cursor.execute(query) + return cursor.fetchall() + + +CHARACTER_COUNT = """ +SELECT COUNT(character_id) +FROM charactercreator_character; +""" + +CLERIC_COUNT = """ +SELECT COUNT(character_ptr_id) +FROM charactercreator_cleric; +""" + +FIGHTER_COUNT = """ +SELECT COUNT(character_ptr_id) +FROM charactercreator_fighter; +""" + +MAGE_COUNT = """ +SELECT COUNT(character_ptr_id) +FROM charactercreator_mage; +""" + +NECRO_COUNT = """ +SELECT COUNT(mage_ptr_id) +FROM charactercreator_necromancer; +""" + +THIEF_COUNT = """ +SELECT COUNT(character_ptr_id) +FROM charactercreator_thief; +""" + +ITEM_COUNT = """ +SELECT COUNT(item_id) +FROM armory_item; +""" + +WEAPON_COUNT = """ +SELECT COUNT(DISTINCT item_id) +FROM armory_item, armory_weapon +WHERE item_id = item_ptr_id; +""" + +CHAR_INV_COUNT = """ +SELECT COUNT(item_id) +FROM charactercreator_character_inventory +GROUP BY character_id +LIMIT 20; +""" + +CHAR_WEAP_COUNT = """ +SELECT COUNT(item_id) +FROM charactercreator_character_inventory +WHERE item_id > 137 +GROUP BY character_id +LIMIT 20; +""" + +CHAR_INV_AVG = """ +SELECT AVG(items.avg_count) FROM +(SELECT COUNT(item_id) AS avg_count +FROM charactercreator_character_inventory +GROUP BY character_id) AS items; +""" + +CHAR_WEAP_AVG = """ +SELECT AVG(weapons.avg_count) FROM +(SELECT COUNT(item_id) AS avg_count +FROM charactercreator_character_inventory +WHERE item_id > 137 +GROUP BY character_id) AS weapons; +""" + + +if __name__ == '__main__': + conn = connect_to_db() + curs = conn.cursor() + + results1 = execute_query(curs, CHARACTER_COUNT)[0][0] + print('How many total characters are there?') + print(f'There are {results1} characters.') + print('***************') + + total_cleric = execute_query(curs, CLERIC_COUNT)[0][0] + total_fight = execute_query(curs, FIGHTER_COUNT)[0][0] + total_mage = execute_query(curs, MAGE_COUNT)[0][0] + total_necro = execute_query(curs, NECRO_COUNT)[0][0] + total_thief = execute_query(curs, THIEF_COUNT)[0][0] + print('How many of each specific subclass?') + print(f'Breakdown of classes: {total_cleric} clerics, {total_fight} fighters, {total_thief} thieves, and {total_mage} mages, {total_necro} of which are necromancers.') + print('***************') + + total_items = execute_query(curs, ITEM_COUNT)[0][0] + print('How many total items?') + print(f'There are {total_items} items in the game.') + print('***************') + + total_weap = execute_query(curs, WEAPON_COUNT)[0][0] + print ('How many of the items are weapons? How many are not?') + print(f'Of these, {total_weap} are weapons and {total_items - total_weap} are not.') + print('***************') + + char_items = execute_query(curs, CHAR_INV_COUNT)[0:20] + print('How many Items does each character have? (Return first 20 rows)') + print('Number of items in the top 20 inventories:') + print(char_items) + print('***************') + + char_weapons = execute_query(curs, CHAR_WEAP_COUNT)[0:20] + print('How many Weapons does each character have? (Return first 20 rows)') + print('And these are the number of weapons within them:') + print(char_weapons) + print('***************') + + char_avg_items = execute_query(curs, CHAR_INV_AVG)[0][0] + print('On average, how many Items does each Character have?') + print(f'The average number of items carried by players is {char_avg_items}') + print('***************') + + char_avg_weaps = execute_query(curs, CHAR_WEAP_AVG)[0][0] + print('On average, how many Weapons does each Character have?') + print(f'The average number of weapons carried by players is {char_avg_weaps}') + print('***************') diff --git a/module2-sql-for-analysis/Pipfile b/module2-sql-for-analysis/Pipfile new file mode 100644 index 00000000..53c32968 --- /dev/null +++ b/module2-sql-for-analysis/Pipfile @@ -0,0 +1,13 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +python-dotenv = "*" +psycopg2-binary = "*" + +[requires] +python_version = "3.8" diff --git a/module2-sql-for-analysis/Pipfile.lock b/module2-sql-for-analysis/Pipfile.lock new file mode 100644 index 00000000..aed2e55d --- /dev/null +++ b/module2-sql-for-analysis/Pipfile.lock @@ -0,0 +1,65 @@ +{ + "_meta": { + "hash": { + "sha256": "d2f7f8cb967ddbd2921f1a455faef3341e4ca3985397f61c4320aa4cf8e395fe" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.8" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "psycopg2-binary": { + "hashes": [ + "sha256:008da3ab51adc70a5f1cfbbe5db3a22607ab030eb44bcecf517ad11a0c2b3cac", + "sha256:07cf82c870ec2d2ce94d18e70c13323c89f2f2a2628cbf1feee700630be2519a", + "sha256:08507efbe532029adee21b8d4c999170a83760d38249936038bd0602327029b5", + "sha256:107d9be3b614e52a192719c6bf32e8813030020ea1d1215daa86ded9a24d8b04", + "sha256:17a0ea0b0eabf07035e5e0d520dabc7950aeb15a17c6d36128ba99b2721b25b1", + "sha256:3286541b9d85a340ee4ed42732d15fc1bb441dc500c97243a768154ab8505bb5", + "sha256:3939cf75fc89c5e9ed836e228c4a63604dff95ad19aed2bbf71d5d04c15ed5ce", + "sha256:40abc319f7f26c042a11658bf3dd3b0b3bceccf883ec1c565d5c909a90204434", + "sha256:51f7823f1b087d2020d8e8c9e6687473d3d239ba9afc162d9b2ab6e80b53f9f9", + "sha256:6bb2dd006a46a4a4ce95201f836194eb6a1e863f69ee5bab506673e0ca767057", + "sha256:702f09d8f77dc4794651f650828791af82f7c2efd8c91ae79e3d9fe4bb7d4c98", + "sha256:7036ccf715925251fac969f4da9ad37e4b7e211b1e920860148a10c0de963522", + "sha256:7b832d76cc65c092abd9505cc670c4e3421fd136fb6ea5b94efbe4c146572505", + "sha256:8f74e631b67482d504d7e9cf364071fc5d54c28e79a093ff402d5f8f81e23bfa", + "sha256:930315ac53dc65cbf52ab6b6d27422611f5fb461d763c531db229c7e1af6c0b3", + "sha256:96d3038f5bd061401996614f65d27a4ecb62d843eb4f48e212e6d129171a721f", + "sha256:a20299ee0ea2f9cca494396ac472d6e636745652a64a418b39522c120fd0a0a4", + "sha256:a34826d6465c2e2bbe9d0605f944f19d2480589f89863ed5f091943be27c9de4", + "sha256:a69970ee896e21db4c57e398646af9edc71c003bc52a3cc77fb150240fefd266", + "sha256:b9a8b391c2b0321e0cd7ec6b4cfcc3dd6349347bd1207d48bcb752aa6c553a66", + "sha256:ba13346ff6d3eb2dca0b6fa0d8a9d999eff3dcd9b55f3a890f12b0b6362b2b38", + "sha256:bb0608694a91db1e230b4a314e8ed00ad07ed0c518f9a69b83af2717e31291a3", + "sha256:c8830b7d5f16fd79d39b21e3d94f247219036b29b30c8270314c46bf8b732389", + "sha256:cac918cd7c4c498a60f5d2a61d4f0a6091c2c9490d81bc805c963444032d0dab", + "sha256:cc30cb900f42c8a246e2cb76539d9726f407330bc244ca7729c41a44e8d807fb", + "sha256:ccdc6a87f32b491129ada4b87a43b1895cf2c20fdb7f98ad979647506ffc41b6", + "sha256:d1a8b01f6a964fec702d6b6dac1f91f2b9f9fe41b310cbb16c7ef1fac82df06d", + "sha256:e004db88e5a75e5fdab1620fb9f90c9598c2a195a594225ac4ed2a6f1c23e162", + "sha256:eb2f43ae3037f1ef5e19339c41cf56947021ac892f668765cd65f8ab9814192e", + "sha256:fa466306fcf6b39b8a61d003123d442b23707d635a5cb05ac4e1b62cc79105cd" + ], + "index": "pypi", + "version": "==2.8.5" + }, + "python-dotenv": { + "hashes": [ + "sha256:8c10c99a1b25d9a68058a1ad6f90381a62ba68230ca93966882a4dbc3bc9c33d", + "sha256:c10863aee750ad720f4f43436565e4c1698798d763b63234fb5021b6c616e423" + ], + "index": "pypi", + "version": "==0.14.0" + } + }, + "develop": {} +} diff --git a/module2-sql-for-analysis/elephant_queries.py b/module2-sql-for-analysis/elephant_queries.py new file mode 100644 index 00000000..ba3d21cc --- /dev/null +++ b/module2-sql-for-analysis/elephant_queries.py @@ -0,0 +1,67 @@ +import os +import psycopg2 +from dotenv import load_dotenv + +load_dotenv() + +DB_NAME = os.getenv('DB_NAME') +DB_USER = os.getenv('DB_USER') +DB_PASS = os.getenv('DB_PASS') +DB_HOST = os.getenv('DB_HOST') + +### Connect to ElephantSQL-hosted PostgreSQL +conn = psycopg2.connect(dbname=DB_NAME, + user=DB_USER, + password=DB_PASS, + host=DB_HOST) + +### A "cursor", a structure to iterate over db records to perform queries +cursor = conn.cursor() + +### An example query +cursor.execute('SELECT * from test_table;') + +### Note - nothing happened yet! We need to actually *fetch* from the cursor +results = cursor.fetchone() +# print(results) + + +################ Connect to SQLite3 DB for RPG data ##################### + +import sqlite3 + +sl_conn = sqlite3.connect('rpg_db.sqlite3') +sl_cursor = sl_conn.cursor() +characters = sl_cursor.execute('SELECT * FROM charactercreator_character LIMIT 10').fetchall() +print(characters) + +################# Create Character Table in PostGRES #################### + +create_character_table_query = ''' +CREATE TABLE IF NOT EXISTS rpg_characters ( + character_id SERIAL PRIMARY KEY, + name VARCHAR(30), + level INT, + exp INT, + hp INT, + strength INT, + intelligence INT, + dexterity INT, + wisdom INT +) +''' + +cursor.execute(create_character_table_query) +conn.commit() + +################# Insert Character Data in PostGRES #################### + +for character in characters: + + insert_query = f''' INSERT INTO rpg_characters + (character_id, name, level, exp, hp, strength, intelligence, dexterity, wisdom) VALUES + {character} + ''' + cursor.execute(insert_query) + +conn.commit() diff --git a/module2-sql-for-analysis/elephant_queries_DS17.py b/module2-sql-for-analysis/elephant_queries_DS17.py new file mode 100644 index 00000000..1ecac508 --- /dev/null +++ b/module2-sql-for-analysis/elephant_queries_DS17.py @@ -0,0 +1,66 @@ +import os +import psycopg2 +from dotenv import load_dotenv +import sqlite3 + +load_dotenv() + +# Variables saved in .env file +DB_NAME = os.getenv('DB_NAME') +DB_USER = os.getenv('DB_USER') +DB_PASS = os.getenv('DB_PASS') +DB_HOST = os.getenv('DB_HOST') + +# Connect to ElephantSQL-hosted PostgreSQL and instantiate cursor +conn = psycopg2.connect(dbname=DB_NAME, + user=DB_USER, + password=DB_PASS, + host=DB_HOST) +cursor = conn.cursor() + +# An example query +# Note - nothing happened yet! We need to actually *fetch* from the cursor +cursor.execute('SELECT * from test_table;') +results = cursor.fetchone() + + +# Connect to SQLite3 DB for RPG data +sl_conn = sqlite3.connect('rpg_db.sqlite3') +sl_cursor = sl_conn.cursor() +characters = sl_cursor.execute('SELECT * FROM charactercreator_character LIMIT 10').fetchall() +print(characters) + +# Create Character Table in PostGRES + +create_character_table_query = ''' +CREATE TABLE IF NOT EXISTS rpg_characters ( + character_id SERIAL PRIMARY KEY, + name VARCHAR(30), + level INT, + exp INT, + hp INT, + strength INT, + intelligence INT, + dexterity INT, + wisdom INT +) +''' + +cursor.execute(create_character_table_query) +conn.commit() + +# Insert Character Data in PostGRES + +for character in characters: + insert_query = f''' INSERT INTO rpg_characters + (character_id, name, level, exp, hp, strength, intelligence, dexterity, wisdom) VALUES + {character} + ''' + cursor.execute(insert_query) + +# Commit and close out connection and cursors +conn.commit() +sl_cursor.close() +sl_conn.close() +cursor.close() +conn.close() diff --git a/module2-sql-for-analysis/insert_titanic.py b/module2-sql-for-analysis/insert_titanic.py new file mode 100644 index 00000000..83755a5c --- /dev/null +++ b/module2-sql-for-analysis/insert_titanic.py @@ -0,0 +1,59 @@ +import pandas as pd +import os +import psycopg2 +from dotenv import load_dotenv + +load_dotenv() + +########## Connect to PostGRES ########## + +DB_NAME = os.getenv('DB_NAME') +DB_USER = os.getenv('DB_USER') +DB_PASS = os.getenv('DB_PASS') +DB_HOST = os.getenv('DB_HOST') + +conn = psycopg2.connect(dbname=DB_NAME, + user=DB_USER, + password=DB_PASS, + host=DB_HOST) + +cursor = conn.cursor() + +########## Create Titanic SQL Table ########## + +create_titanic_table = ''' + CREATE TABLE IF NOT EXISTS titanic ( + pass_id SERIAL PRIMARY KEY, + survived INT, + pclass INT, + name VARCHAR(200), + sex VARCHAR(20), + age INT, + siblings_spouses INT, + parents_children INT, + fare NUMERIC(30) + ) +''' + +cursor.execute(create_titanic_table) +conn.commit() + +########## Read in CSV and populate Titanic Table ########## + +titanic_db = pd.read_csv('titanic.csv') +titanic_db['Name'] = titanic_db['Name'].apply(lambda x: x.replace("'","")) + +data = list(titanic_db.to_records()) + +for item in data: + + insert_query = f''' INSERT INTO titanic + (pass_id, survived, pclass, name, sex, age, siblings_spouses, + parents_children, fare) VALUES {item} + ''' + + cursor.execute(insert_query) + +conn.commit() + +# test edit for push \ No newline at end of file diff --git a/module2-sql-for-analysis/insert_titanic_DS17.py b/module2-sql-for-analysis/insert_titanic_DS17.py new file mode 100644 index 00000000..07f8fef3 --- /dev/null +++ b/module2-sql-for-analysis/insert_titanic_DS17.py @@ -0,0 +1,53 @@ +import pandas as pd +import os +import psycopg2 +from dotenv import load_dotenv + +load_dotenv() + +DB_NAME = os.getenv('DB_NAME') +DB_USER = os.getenv('DB_USER') +DB_PASS = os.getenv('DB_PASS') +DB_HOST = os.getenv('DB_HOST') + +conn = psycopg2.connect(dbname=DB_NAME, + user=DB_USER, + password=DB_PASS, + host=DB_HOST) + +cursor = conn.cursor() + +create_titanic_table = ''' + CREATE TABLE IF NOT EXISTS titanic ( + pass_id SERIAL PRIMARY KEY, + survived INT, + pclass INT, + name VARCHAR(200), + sex VARCHAR(20), + age INT, + siblings_spouses INT, + parents_children INT, + fare NUMERIC(30) + ) +''' + +cursor.execute(create_titanic_table) +conn.commit() + +titanic_db = pd.read_csv('titanic.csv') +titanic_db['Name'] = titanic_db['Name'].apply(lambda x: x.replace("'","")) + +data = list(titanic_db.to_records()) + +for item in data: + + insert_query = f''' INSERT INTO titanic + (pass_id, survived, pclass, name, sex, age, siblings_spouses, + parents_children, fare) VALUES {item} + ''' + + cursor.execute(insert_query) + +conn.commit() +cursor.close() +conn.close() diff --git a/module3-nosql-and-document-oriented-databases/mongodb.py b/module3-nosql-and-document-oriented-databases/mongodb.py new file mode 100644 index 00000000..35eeda90 --- /dev/null +++ b/module3-nosql-and-document-oriented-databases/mongodb.py @@ -0,0 +1,72 @@ +import pymongo +import os +import sqlite3 +from dotenv import load_dotenv + +load_dotenv() + +# Setup connection to MongoDB +DB_NAME = os.getenv('DB_NAME') +DB_PASS = os.getenv('DB_PASS') +connection = ('mongodb+srv://Me:' + DB_PASS + + '@cluster0.zrrji.mongodb.net/' + DB_NAME + + '?retryWrites=true&w=majority') + +client = pymongo.MongoClient(connection) +db = client.test + +# Set up connection to RPG_DB +sl_conn = sqlite3.connect('rpg_db.sqlite3') +sl_curs = sl_conn.cursor() + +def table_insert(table): + document = sl_curs.execute(table).fetchall() + db.test.insert_one({'document': document) + +# Get and copy the charactercreator_character table from +# SQLite3 to MongoDB +get_characters = 'SELECT * FROM charactercreator_character;' +table_insert(get_characters) + +# Repeat for classes +get_clerics = 'SELECT * FROM charactercreator_cleric;' +table_insert(get_clericss) + +get_fighters = 'SELECT * FROM charactercreator_fighter;' +table_insert(get_fighters) + +get_mages = 'SELECT * FROM charactercreator_mage;' +table_insert(get_mages) + +get_necros = 'SELECT * FROM charactercreator_necromancer;' +table_insert(get_necros) + +get_theives = 'SELECT * FROM charactercreator_thief;' +table_insert(get_thieves) + +# And again for items, weapons, and inventories +get_invs = 'SELECT * FROM charactercreator_character_inventory;' +table_insert(get_invs) + +get_items = 'SELECT * FROM armory_item;' +table_insert(get_items) + +get_weapons = 'SELECT * FROM armory_weapon;' +table_insert(get_weapons) + +# Commit and close connections +client.close() +sl_curs.close() +sl_conn.close() + +########################################## +# How was working with MongoDB different from working with PostgreSQL? +# What was easier, and what was harder? + +# I think the biggest difference in dealing with MongoDB vs PostgreSQL +# is the fact that MongoDB can handle NOSQL documents while PostgreSQL cannot. +# While it meant that MongoDB could take in more documents, it also meant that +# we had to be much more careful with the structure and schema of our inserts. +# MongoDB was also more secure, so sometimes I would find myself fighting +# security features when I didn't have to in PostgreSQL. Dotenv helped +# with that, but only to an extent. \ No newline at end of file diff --git a/module4-acid-and-database-scalability-tradeoffs/map_reduce_example.py b/module4-acid-and-database-scalability-tradeoffs/map_reduce_example.py new file mode 100644 index 00000000..7d7002cb --- /dev/null +++ b/module4-acid-and-database-scalability-tradeoffs/map_reduce_example.py @@ -0,0 +1,25 @@ +from functools import reduce + +my_list = [1, 2, 3, 4] + +# We want the sum of squared values +# (A fairly real statistical task) + +# Traditional (non-mapreduce approach) +ssv_trad = sum([i**2 for i in my_list]) + +# That works fine - but what it ew had 40 billion numbers? +# We could use a mapreduce approach + +# To be clear - this code still runs on one computer +# But mapreduce paradigm *could* be distributerd more directly + +squared_values = map(lambda i: i**2, my_list) + +def add_numbers(x1, x2): + return x1 + x2 + +ssv_mapreduce = reduce(add_numbers, squared_values) + +print('Sum of squared values (traditional): '+ str(ssv_trad)) +print('Sum of squared values (map-reduce): ' + str(ssv_mapreduce)) diff --git a/module4-acid-and-database-scalability-tradeoffs/postgresql_queries.py b/module4-acid-and-database-scalability-tradeoffs/postgresql_queries.py new file mode 100644 index 00000000..c33e3f04 --- /dev/null +++ b/module4-acid-and-database-scalability-tradeoffs/postgresql_queries.py @@ -0,0 +1,224 @@ +import os +from dotenv import load_dotenv +import psycopg2 + +load_dotenv() + +DB_NAME = os.getenv("DB_NAME") +DB_USER = os.getenv("DB_USER") +DB_PASS = os.getenv("DB_PASS") +DB_HOST = os.getenv("DB_HOST") + +conn = psycopg2.connect(dbname=DB_NAME, + user=DB_USER, + password=DB_PASS, + host=DB_HOST) +cursor = conn.cursor() + +# How many passengers survived, and how many died? +survivors = (""" + SELECT COUNT(*) + FROM titanic + WHERE survived = 1 +""") +deaths = (""" + SELECT COUNT(*) + FROM titanic + WHERE survived = 0 +""") +print(cursor.execute(survivors).fetchall() + " survived.") +print(cursor.execute(deaths).fetchall() + " died.") + +# How many passengers were in each class? +class_query = (""" + SELECT pclass, count(DISTINCT id) + FROM titanic + GROUP BY pclass +""") +class_result = cursor.execute(class_query).fetchall() + +for each in result: + if each[0] == 1: + print(f'{each[1]} passengers were in first class.') + elif each[0] == 2: + print(f'{each[1]} passengers were in second class.') + else: + print(f'{each[1]} passengers were in third class.') + +# How many passengers survived/died in each class? +class_surv_query = (""" + SELECT pclass, COUNT(DISTINCT id) + FROM titanic + WHERE survived = 1 + GROUP BY pclass +""") +class_surv_result = cursor.execute(class_surv_query).fetchall() + +class_died_query = (""" + SELECT pclass, COUNT(DISTINCT id) + FROM titanic + WHERE survived = 0 + GROUP BY pclass +""") +class_died_result = cursor.execute(class_died_query).fetchall() + +for each in class_died_result: + if each[0] == 1: + dead_first = each[1] + elif each[0] == 2: + dead_second = each[1] + else: + dead_third = each[1] + +for each in class_surv_result: + if each[0] == 1: + print(f'In first class {each[1]} passengers survived and + {dead_first} died.') + elif each[0] == 2: + print(f'In first class {each[1]} passengers survived and + {dead_second} died.') + else: + print(f'In first class {each[1]} passengers survived and + {dead_third} died.') + +# What was the average age of survivors vs. nonsurvivors? +age_query = (""" + SELECT survived, AVG(age) + FROM titanic + GROUP BY survived +""") +age_result = cursor.execute(age_query).fetchall() + +for each in age_result: + if each[0] == 1: + print(f'The average age of survivors is {each[1]}') + else: + print(f'The average age of the deceased is {each[1]}') + +# What was the average age of each passenger class? +class_age_query = (""" + SELECT pclass, AVG(age) + FROM titanic + GROUP BY pclass +""") +class_age_result = cursor.execute(class_age_query).fetchall() + +for each in class_age_result: + if each[0] == 1: + print(f'The average age of first class passengers is {each[1]}') + elif each[0] == 2: + print(f'The average age of second class passengers is {each[1]}') + else: + print(f'The average age of third class passengers is {each[1]}') + +# What was the average fare by passenger class? By survival? +class_fare_query = (""" + SELECT pclass, AVG(fare) + FROM titanic + GROUP BY pclass +""") +class_fare_result = cursor.execute(class_fare_query).fetchall() + +surv_fare_query = (""" + SELECT survived, AVG(fare) + FROM titanic + GROUP BY survived +""") +surv_fare_result = cursor.execute(surv_fare_query).fetchall() + +for each in class_fare_result: + if each[0] == 1: + print(f'The average fare for first class passengers is {each[1]}') + elif each[0] == 2: + print(f'The average fare for second class passengers is {each[1]}') + else: + print(f'The average fare for third class passengers is {each[1]}') + +for each in surv_fare_result: + if each[0] == 1: + print(f'The average fare for survivors is {each[1]}') + else: + print(f'The average fare for the deceased is {each[1]}') + +# How many siblings/spouses aboard on average, by passenger class? By survival? +class_sib_query = (""" + SELECT pclass, AVG(siblings_spouses) + FROM titanic + GROUP BY pclass +""") +class_sib_result = cursor.execute(class_sib_query).fetchall() + +surv_sib_query = (""" + SELECT survived, AVG(siblings_spouses) + FROM titanic + GROUP BY survived +""") +surv_sib_result = cursor.execute(surv_sib_query).fetchall() + +for each in class_sib_result: + if each[0] == 1: + print(f'The average number of siblings and spouses for survivors + is {each[1]}') + else: + print(f'The average number of siblings and spouses for the deceased + is {each[1]}') + +for each in surv_sib_result: + if each[0] == 1: + print(f'The average number of siblings and spouses for first class + passengers is {each[1]}') + elif each[0] == 2: + print(f'The average number of siblings and spouses for second class + passengers is {each[1]}') + else: + print(f'The average number of siblings and spouses for third class + passengers is {each[1]}') + +# How many parents/children aboard on average, by passenger class? By survival? +class_parent_query = (""" + SELECT pclass, AVG(parents_children) + FROM titanic + GROUP BY pclass +""") +class_parent_result = cursor.execute(class_parent_query).fetchall() + +surv_parent_query = (""" + SELECT survived, AVG(parents_children) + FROM titanic + GROUP BY survived +""") +surv_parent_result = cursor.execute(surv_parent_query).fetchall() + +for each in class_parent_result: + if each[0] == 1: + print(f'The average number of parents and children for survivors + is {each[1]}') + else: + print(f'The average number of parents and children for the deceased + is {each[1]}') + +for each in surv_parent_result: + if each[0] == 1: + print(f'The average number of parents and children for first class + passengers is {each[1]}') + elif each[0] == 2: + print(f'The average number of parents and children for second class + passengers is {each[1]}') + else: + print(f'The average number of parents and children for third class + passengers is {each[1]}') + +# Do any passengers have the same name? +unique_query = (""" + SELECT name, COUNT(*) + FROM titanic + GROUP BY name +""") +unique_result = cursor.execute(unique_query).fetchall() + +print("The following passengers have the same name:") +for each in unique_result: + if each[1] > 1: + print(f' {each[0]}') + else: + pass diff --git a/module4-acid-and-database-scalability-tradeoffs/sqlite_demo.py b/module4-acid-and-database-scalability-tradeoffs/sqlite_demo.py new file mode 100644 index 00000000..b72d9343 --- /dev/null +++ b/module4-acid-and-database-scalability-tradeoffs/sqlite_demo.py @@ -0,0 +1,32 @@ +import sqlite3 + +def create_table(conn): + curs = conn.cursor() + create_statement = """ + CREATE TABLE students ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name CHAR(20), + favorite_number INTEGER, + least_favorite_number INTEGER + ); + """ + curs.execute(create_statement) + curs.close() + conn.commit() + +def insert_data(conn) + my_data = [ + ('Malven', 7, 10,) + ('Steven', -3, 12,) + ('Dondre', 80, -1) + ] + for row in my_data: + pass + # TODO write an insert statement! + curs.close() + conn.commit() + +if __name__ == '__main__': + conn = sqlite3.connect('example_db.sqlite3') + create_table(conn) + insert_data(conn) \ No newline at end of file