Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 68 additions & 3 deletions module1-introduction-to-sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,79 @@ randomized, the numeric and boolean fields were left as defaults.
Use `sqlite3` to load and write queries to explore the data, and answer the
following questions:

- How many total Characters are there?
- How many total Characters are there? 302
SELECT
count(character_id)
FROM charactercreator_character

- How many of each specific subclass?
- How many total Items?
- How many of the Items are weapons? How many are not?
Cleric = 75
Fighter = 68
Mage = 108
Necromancer = 11
Thief = 51
SELECT
COUNT()
FROM charactercreator_cleric
SELECT
COUNT()
FROM charactercreator_fighter
SELECT
COUNT()
FROM charactercreator_mage
SELECT
COUNT()
FROM charactercreator_necromancer
SELECT
COUNT()
FROM charactercreator_thief

- How many total Items? 211
SELECT
(select COUNT(item_id) FROM armory_item)
+ (select COUNT(item_ptr_id) FROM armory_weapon)
AS Total_Items

- How many of the Items are weapons? How many are not?
Weapons = 37, Other items = 174
SELECT
COUNT()
FROM armory_weapon

- How many Items does each character have? (Return first 20 rows)
SELECT
character_id
,count(item_id)
FROM charactercreator_character_inventory
GROUP BY character_id
LIMIT 20

- How many Weapons does each character have? (Return first 20 rows)
SELECT character_id, count(item_id)
FROM charactercreator_character_inventory LEFT JOIN armory_weapon
ON item_id = item_ptr_id
GROUP BY character_id
LIMIT 20

- On average, how many Items does each Character have?
SELECT
character_id
,COUNT(item_id) as 'No_of_Items'
,ROUND(AVG(item_id))
FROM charactercreator_character_inventory
GROUP BY character_id
ORDER BY character_id
LIMIT 20

- On average, how many Weapons does each character have?
SELECT
character_id
,count(item_id)
,round(avg(item_ptr_id))
FROM charactercreator_character_inventory LEFT JOIN armory_weapon
ON item_id = item_ptr_id
GROUP BY character_id
LIMIT 20

You do not need all the tables - in particular, the `account_*`, `auth_*`,
`django_*`, and `socialaccount_*` tables are for the application and do not have
Expand Down
26 changes: 26 additions & 0 deletions module1-introduction-to-sql/buddymove_holidayiq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pandas as pd
import os.path
import sqlite3

file_path = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00476/buddymove_holidayiq.csv'
df = pd.read_csv(file_path)
print(f'DATA FRAME SHAPE : {df.shape}')

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DB_FILEPATH = os.path.join(BASE_DIR, 'buddymove_holidayiq.sqlite3')

connection = sqlite3.connect(DB_FILEPATH)
print("CONNECTION:", connection)

cursor = connection.cursor()
print("CURSOR", cursor)

df.to_sql(name='review', con=connection)

no_rows = 'SELECT COUNT(*) FROM review'
result_rows = cursor.execute(no_rows).fetchall()
print('No. of Rows :', result_rows)

reviews = 'SELECT COUNT(Nature), COUNT(Shopping) FROM review WHERE Nature >= 100 AND Shopping >= 100'
result_reviews = cursor.execute(reviews).fetchall()
print('No. of Users Reviewing Nature & Shopping at >= 100 :', result_reviews)
Binary file not shown.
Empty file.
Binary file added module1-introduction-to-sql/data/rpg_db.sqlite3
Binary file not shown.
33 changes: 33 additions & 0 deletions module1-introduction-to-sql/rpg_queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os.path
import sqlite3

# construct a path to wherever your database exists
# DB_FILEPATH = 'https://github.com/RAV10K1/DS-Unit-3-Sprint-2-SQL-and-Databases/blob/master/module1-introduction-to-sql/rpg_db.sqlite3'
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DB_FILEPATH = os.path.join(BASE_DIR, 'rpg_db.sqlite3')

connection = sqlite3.connect(DB_FILEPATH)
print("CONNECTION:", connection)

cursor = connection.cursor()
print("CURSOR", cursor)

total_char = "SELECT count(character_id) FROM charactercreator_character"
no_clerics = "SELECT COUNT() FROM charactercreator_cleric"
no_fighters = "SELECT COUNT() FROM charactercreator_fighter"
no_mages = "SELECT COUNT() FROM charactercreator_mage"
no_necro = "SELECT COUNT() FROM charactercreator_necromancer"
no_thief = "SELECT COUNT() FROM charactercreator_thief"
total_items = "SELECT (select COUNT(item_id) FROM armory_item) + (select COUNT(item_ptr_id) FROM armory_weapon) AS Total_Items"
item_count = "SELECT COUNT() FROM armory_weapon"
char_item = 'SELECT character_id, count(item_id) FROM charactercreator_character_inventory GROUP BY character_id LIMIT 20'
char_wpn = 'SELECT character_id, count(item_id) FROM charactercreator_character_inventory LEFT JOIN armory_weapon ON item_id = item_ptr_id GROUP BY character_id LIMIT 20'
avg_char_wpn = "SELECT character_id, COUNT(item_id) as 'No_of_Items',ROUND(AVG(item_id)) FROM charactercreator_character_inventory GROUP BY character_id ORDER BY character_id LIMIT 20"
avg_char_item = "SELECT character_id, count(item_id), round(avg(item_ptr_id)) FROM charactercreator_character_inventory LEFT JOIN armory_weapon ON item_id = item_ptr_id GROUP BY character_id LIMIT 20"

queries = [total_char, no_clerics, no_fighters, no_mages, no_necro, no_thief,
total_items, item_count, char_item, char_wpn, avg_char_item, avg_char_wpn]

for query in queries:
result = cursor.execute(query).fetchall()
print('Result :', result[0])
1 change: 1 addition & 0 deletions module2-sql-for-analysis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
3 changes: 3 additions & 0 deletions module2-sql-for-analysis/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "C:\\Users\\Ravi\\.virtualenvs\\DS-Unit-3-Sprint-2-SQL-and-Databases-kzZYgH90\\Scripts\\python.exe"
}
16 changes: 16 additions & 0 deletions module2-sql-for-analysis/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
pylint = "*"

[packages]
python-dotenv = "*"
psycopg2-binary = "*"
pandas = "*"
sqlalchemy = "*"

[requires]
python_version = "3.7"
Loading