From e5c640c07b2dfa5f716a30e872c6739018831e9d Mon Sep 17 00:00:00 2001 From: Joseph Akpu <63398509+akpu-jo@users.noreply.github.com> Date: Tue, 17 Jun 2025 19:20:57 +0100 Subject: [PATCH] Month1 and month 2 --- README.md | 32 +++--- .../__pycache__/student_data.cpython-312.pyc | Bin 0 -> 3332 bytes month1_student_management/student_data.py | 67 +++++++++++- .../report_generator.cpython-312.pyc | Bin 0 -> 1224 bytes .../python/__pycache__/utils.cpython-312.pyc | Bin 0 -> 1019 bytes month2_minimart_analysis/python/main.py | 46 +++++++++ .../python/report_generator.py | 26 +++++ month2_minimart_analysis/python/utils.py | 18 ++++ .../sql/create_tables.sql | 25 +++++ month2_minimart_analysis/sql/insert_data.sql | 97 ++++++++++++++++++ .../sql/json-report/Product(where query).json | 51 +++++++++ .../json-report/customers_202506171654.json | 93 +++++++++++++++++ .../json-report/products_202506171657.json | 93 +++++++++++++++++ month2_minimart_analysis/sql/queries.sql | 73 +++++++++++++ 14 files changed, 605 insertions(+), 16 deletions(-) create mode 100644 month1_student_management/__pycache__/student_data.cpython-312.pyc create mode 100644 month2_minimart_analysis/python/__pycache__/report_generator.cpython-312.pyc create mode 100644 month2_minimart_analysis/python/__pycache__/utils.cpython-312.pyc create mode 100644 month2_minimart_analysis/sql/json-report/Product(where query).json create mode 100644 month2_minimart_analysis/sql/json-report/customers_202506171654.json create mode 100644 month2_minimart_analysis/sql/json-report/products_202506171657.json diff --git a/README.md b/README.md index 4446309..8da2dbb 100644 --- a/README.md +++ b/README.md @@ -50,12 +50,14 @@ Define each table with the following columns: - `name` - `email` - `join_date` + - **`products`** - `product_id` (Primary Key) - `product_name` - `category` - `price` + - **`orders`** - `order_id` (Primary Key) @@ -70,21 +72,23 @@ Insert at least **5 rows** into each table with realistic sample values. ### 3. Practice the Following SQL Queries -* **Basic Queries** +- **Basic Queries** + + - Use `SELECT` to retrieve all customers or all products. + - Use `WHERE` to filter products by category (e.g., `"Drinks"`). + - Use `ORDER BY` to list recent orders by date. + +- **Aggregation** - * Use `SELECT` to retrieve all customers or all products. - * Use `WHERE` to filter products by category (e.g., `"Drinks"`). - * Use `ORDER BY` to list recent orders by date. -* **Aggregation** + - Use `COUNT()` to find the number of total orders. + - Use `SUM()` to calculate revenue per product (price × quantity). + - Use `AVG()` to find the average product price. - * Use `COUNT()` to find the number of total orders. - * Use `SUM()` to calculate revenue per product (price × quantity). - * Use `AVG()` to find the average product price. -* **Joins** +- **Joins** - * Use `INNER JOIN` to get detailed order information (with customer and product details). - * Use `LEFT JOIN` to list all customers and include their orders (if any). - * Use `LEFT JOIN` to show products even if they haven’t been ordered. + - Use `INNER JOIN` to get detailed order information (with customer and product details). + - Use `LEFT JOIN` to list all customers and include their orders (if any). + - Use `LEFT JOIN` to show products even if they haven’t been ordered. #### 🧩 Python Tasks @@ -114,6 +118,7 @@ python main.py git clone https://github.com/SamDewriter/first-semester-projects.git cd first-semester-projects ``` + 3. Complete all tasks inside `month1_student_management/` and `month2_minimart_analysis/`. 4. **Commit and Push**: @@ -122,6 +127,7 @@ python main.py git commit -m "Completed Month 1 & 2 projects" git push origin main ``` + 5. **Submit your GitHub repo link**. --- @@ -133,3 +139,5 @@ python main.py - Any text editor or IDE (VSCode, PyCharm, etc.) Happy coding! 🎉 + +/Users/jakpu/dev/data/alt-schl/first-semester-projects/month2_minimart_analysis/sql/json-report diff --git a/month1_student_management/__pycache__/student_data.cpython-312.pyc b/month1_student_management/__pycache__/student_data.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d6e9ad7a1c47bea30efd2701d2a4e92904a94f62 GIT binary patch literal 3332 zcmbtWUu+vm8K3p;I%}`(#BOYQ=n7MsOY0-C-8Q{KlqNbgp@LRP;RJU*apbn%vEz2V zYwheNjg?3N6%oowMB+rlwFiCR;ZQ+31P}Lkr{m$IdvUN&HCA}w=mR_vnm!!ig>S}Q z|7oHVh><*=o%#OFH{b6!^I(F%3GM$B!^VD&5Vmk9h z&D8QBWIAbYN(Vcs(sCor{mr*_WkcS1zUXIQu!bHhNDfwllQn5-TMDcX-Mm=bzGWc?b`d!l(7t7rw$kkjWa>piCuKu*A0>9Q<&C--TV zJ(*QuMbPEzpGC;-_T%@~1+**%5O@k7;7ReM{wq(u6(4wl+)x=tf$P&x_zZg1nm$+1 ziU4w?{!MLS}DKaaJ=;%K>QPtA=J?PtBGO^{PT_MVE424VNrha0-hA z1Dr{o*eQ3cNBXPgqGl9yw$&8AY!Ho-Rw^{yT=5jk@!kM+VE@q+>?^cyx9zlHp4`t&l6#WSZ&#?bQ>x};1QqASWJZA$G`+e)|+N!OH2U5V`Y`sDuYNc8n*Du+j_vDAau*j8+; z8hfr9IlC^_)yT%tTVL7^MJus$wa{ce6x)$eXmBUOvsdM(m+#i0 z>%L7pyy5`UH5)K^o8vpY56t81n(t<*Hv4pJ=+dv{`z?#p=#^&>`Zj#6qE(?|gY%sq z_`L5aa17k1z&Owv2VtzTu^zf3z1hWTuN{}V?)^^u%f4i|e1Y@TCCe%Sr`VR8U)awr z99R4T#YD?4c)WC+0_byAlX=|3;Cbg4Klu@!wLAd|uLsxEQ^8r6RVZJ*1YCu!oQEX} z0B4lVo&?mw2nND|FNTP6U<7mQ!#4v*SxlYE0c6;9ZV$4Kqb)!H z&BWFmlJo)e^c!{p)+kG^2cPg_fh>9Wq4p72I?ZIt&^6x!0H8mshj^tWYs#|#mWNWH z5`FpvaS(PKHxAJf2-yy#> zD`#fjz4F0Q<;Cx8&COR{%~odhN`!o*V)_PgT-)Sjq%FcRzqFI|^7b?Y<%IqtYy}(4CnB+#uo-7aLi8J4dWxjcr zOc)k~NhecnsY_ss#-aNg`eY6LF+TOy*^T^W{-eb3R($GTl%41I`lCWpezIsq>J}TK zK}>*x>R66uG<{IE%NFTJ_eEd>PoVSgqZ82WtK5m2at_G$VJr^bf4@+RPgP@6YZsf8 zswVCpy8F_dBU|clMH=SM-C2l>|#|o&JFwr!(je@ literal 0 HcmV?d00001 diff --git a/month1_student_management/student_data.py b/month1_student_management/student_data.py index 38ded7e..b122063 100644 --- a/month1_student_management/student_data.py +++ b/month1_student_management/student_data.py @@ -1,20 +1,79 @@ students = [] +def print_newline(): + """ + Print a newline for better readability. + """ + print("--------------------------------------------------") + print("\n") # Print a newline for spacing + pass + def add_student(): """ - TODO: Prompt the user to enter student name, age, and grade. + Prompt the user to enter student name, age, and grade. Append the student as a dictionary to the students list. """ + print_newline() + + name = input("Enter student name: ").strip() + + if not name: + print(" ⚠️ Name cannot be blank.") + return + try: + age = int(input("Enter student age: ").strip()) + if age <= 0: + print(" ⚠️ Age must be a positive integer.") + return + except ValueError: + print(" ⚠️ Invalid age. Please enter a valid integer.") + return + + try: + grade = float(input("Enter student grade: ").strip()) + if grade < 0 or grade > 100: + print(" ⚠️ Grade must be between 0 and 100.") + return + except ValueError: + print(" ⚠️ Invalid grade. Please enter a valid number.") + return + + student = { + 'name': name, + 'age': age, + 'grade': grade + } + students.append(student) + print(f"✅ Student {name} added successfully.\n") + print_newline() pass def view_students(): """ - TODO: Loop through the students list and print each student's info. + Loop through the students list and print each student's info. """ + print_newline() + if not students: + print("📋 No students found.\n") + return + + print("📋 List of Students: ") + for idx, student in enumerate(students, start=1): + print(f"{idx}. Name: {student['name']}, Age: {student['age']}, Grade: {student['grade']:.2f}") + + print_newline() pass def get_average_grade(): """ - TODO: Return the average grade of all students. + Return the average grade of all students. """ - pass \ No newline at end of file + print_newline() + if not students: + print("📊 No students to calculate average grade.") + return 0.0 + + total_grade = sum(student['grade'] for student in students) + print_newline() + return total_grade / len(students) + \ No newline at end of file diff --git a/month2_minimart_analysis/python/__pycache__/report_generator.cpython-312.pyc b/month2_minimart_analysis/python/__pycache__/report_generator.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6e88ef01ce0c8ef95f05977c011104ffbffadf84 GIT binary patch literal 1224 zcmZuw&2Jk;6rcU{#7>HcUEB{d~-_1wnbF>ih=4g((30bM0F1+%j_@04~%qjfef2t#1DmcMD+5i{UtG|nW zHXq3ogN({)nPOm7&RLbD4iy)TD!`}0D|ry8R4-2G;-9pyQD@;Am8}uwm_}~maLaQA zzMI$~_xMm4G4&iGu#+$z1%$$-Xm`j^l;#&D^w17SM-$6D;5RU;S)7Vc1f5JD<%|fcD}Jm|Hu8Bv!|CU8-Q zo{>pQ1$Soqgu&R%H?`)Kgpub_*-^UJiQK88GM# z>^pJNbIERR%jR~^_IZ~%eZRNkQ3j9%gmFTvvB?06P>DxdG1_rSwpzfpDS|iizhqVh2 zv}vt<|BJ(V>%sc8-pTaZ^Rx9A2pb>o8?%+Q@#bXZ(tdfiygFW+EVuXZtSV8peQi-fDQ+F~AFoUgBBcvPVF`Nor>zJbAM7|H*^P-TQOox$h&u3J=Rw_G?x8fn9=Oum% l)Jwb>z!>}FGui@EK6O?Fb)+eZ@)vsd4|MrhKc|>^#(%#IP7weA literal 0 HcmV?d00001 diff --git a/month2_minimart_analysis/python/__pycache__/utils.cpython-312.pyc b/month2_minimart_analysis/python/__pycache__/utils.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..50e86af5d7b47e1a544840e1eb3c64433c9c0372 GIT binary patch literal 1019 zcmb7CzfTlF6rS1J<9=}h29!{k(HL-zcZ-OLCI&*pN@G-V4b5iQ8MuYL-OJ1zx|b`i zAcm3-w4x<8wEhh?AQogpVWNp4HYR69<(q{ATEFDIH$T34Z|0l#rC7`(!28|TRo_79 zhn{o+^8}p{2%Ctd8D7Kli1jc7B4(*IG;c(vmHG7`!S89edqkfz!BSuy0&Wv23?V@a zwQw_y5W}C&_;biInvb7`!73MuROV)h3JDEDRpo-X4Iy~oM&u4JFO~0+XO%aEDl%Sv z^pFUuxK%#ZX?Ad&7GWb`mJ#P^4MxgoNEVx#BQ=AN6UVMdE~LFoSLzL$@l|`4Dr!?- zm84ts?L|*W81fpIii=WRgv;Dj(yoPps!ljHFYszqD2E2rkEAEg(*6y5e`tL7=0V@&UUu@Z(BFO`3P5$i&Y>%4>x_fD+&$1WXPtyE^%OHM zR-=?^b+RTN==ApB HTyp9ki}Cfc literal 0 HcmV?d00001 diff --git a/month2_minimart_analysis/python/main.py b/month2_minimart_analysis/python/main.py index 583f1c8..60534b6 100644 --- a/month2_minimart_analysis/python/main.py +++ b/month2_minimart_analysis/python/main.py @@ -1 +1,47 @@ # Entry point for the MiniMart data reporting system + +from report_generator import generate_sales_summary +from utils import convert_currency, apply_discount +from utils import print_newline + +# Sample product catalog +products = { + 1: {"name": "Laptop", "price": 999.99}, + 2: {"name": "Headphones", "price": 199.99}, + 3: {"name": "Notebook", "price": 4.99}, + 4: {"name": "Mouse", "price": 49.99} +} + +# Simulated orders list +orders = [ + {"customer": "Alice", "product_id": 1, "quantity": 1}, + {"customer": "Bob", "product_id": 2, "quantity": 2}, + {"customer": "Alice", "product_id": 3, "quantity": 5}, + {"customer": "Diana", "product_id": 4, "quantity": 3}, + {"customer": "Ethan", "product_id": 2, "quantity": 1} +] + +print_newline() +# Apply conditional flags +for order in orders: + product = products[order['product_id']] + total_cost = product['price'] * order['quantity'] + if total_cost > 100: + print(f"⚠️ Large order flagged for {order['customer']}: ${total_cost:.2f}") + print('applying discount of 10%...') + + discounted = apply_discount(total_cost) + print(f"Final price after discount: ${discounted:.2f}") + print(f"Price in EUR: €{convert_currency(discounted)}") + print_newline() + +# Generate report +report = generate_sales_summary(orders, products) + +# Print the report summary +print("\n📊 MiniMart Sales Report") +print("---------------------------") +print(f"Total products sold: {report['total_products_sold']}") +print(f"Most popular product: {report['most_popular_product']}") +print(f"Revenue per customer: {report['revenue_per_customer']}\n") + diff --git a/month2_minimart_analysis/python/report_generator.py b/month2_minimart_analysis/python/report_generator.py index fb6ddb8..abeff98 100644 --- a/month2_minimart_analysis/python/report_generator.py +++ b/month2_minimart_analysis/python/report_generator.py @@ -1 +1,27 @@ # Code to generate dictionary summary reports +from collections import defaultdict +from utils import convert_currency, apply_discount + +def generate_sales_summary(orders, products): + total_products_sold = 0 + product_sales = defaultdict(int) + customer_revenue = defaultdict(float) + + for order in orders: + product_id = order['product_id'] + quantity = order['quantity'] + price = products[product_id]['price'] + customer = order['customer'] + + total_products_sold += quantity + product_sales[product_id] += quantity + customer_revenue[customer] += quantity * price + + most_popular_product_id = max(product_sales, key=product_sales.get, default=None) + most_popular_product = products[most_popular_product_id]['name'] if most_popular_product_id else None + + return { + 'total_products_sold': total_products_sold, + 'most_popular_product': most_popular_product, + 'revenue_per_customer': dict(customer_revenue) + } diff --git a/month2_minimart_analysis/python/utils.py b/month2_minimart_analysis/python/utils.py index 7049a76..3a7fd1f 100644 --- a/month2_minimart_analysis/python/utils.py +++ b/month2_minimart_analysis/python/utils.py @@ -1 +1,19 @@ # Utility functions for data conversion and filtering + +def convert_currency(amount_usd, rate=0.85): + """Convert USD to another currency (e.g., EUR at 0.85 rate).""" + return round(amount_usd * rate, 2) + +def apply_discount(price, threshold=100, discount_rate=0.10): + """Apply discount if price exceeds threshold.""" + if price > threshold: + return round(price * (1 - discount_rate), 2) + return price + +def print_newline(): + """ + Print a newline for better readability. + """ + print("--------------------------------------------------") + print("\n") # Print a newline for spacing + pass \ No newline at end of file diff --git a/month2_minimart_analysis/sql/create_tables.sql b/month2_minimart_analysis/sql/create_tables.sql index 1411bac..b0644da 100644 --- a/month2_minimart_analysis/sql/create_tables.sql +++ b/month2_minimart_analysis/sql/create_tables.sql @@ -1 +1,26 @@ -- SQL script to create necessary tables +CREATE TABLE customers ( + customer_id SERIAL PRIMARY KEY, + name VARCHAR(100) NOT NULL, + email VARCHAR(100) UNIQUE NOT NULL, + join_date DATE DEFAULT CURRENT_DATE + ); + +CREATE TABLE products ( + product_id SERIAL PRIMARY KEY, + product_name VARCHAR(100) NOT NULL, + category VARCHAR(100), + price DECIMAL(10, 2) +); + +CREATE TABLE orders ( + order_id SERIAL PRIMARY KEY, + customer_id INT, + product_id INT, + quantity INT, + order_date DATE NOT NULL DEFAULT CURRENT_DATE, + + FOREIGN KEY (customer_id) REFERENCES customers(customer_id), + FOREIGN KEY (product_id) REFERENCES products(product_id) + + ); diff --git a/month2_minimart_analysis/sql/insert_data.sql b/month2_minimart_analysis/sql/insert_data.sql index 97dc4da..30d4e1d 100644 --- a/month2_minimart_analysis/sql/insert_data.sql +++ b/month2_minimart_analysis/sql/insert_data.sql @@ -1 +1,98 @@ -- SQL script to insert sample data +INSERT INTO customers (name, email, join_date) +VALUES +('Alice Smith', 'alice.smith@example.com', '2024-02-14'), +('Bob Johnson', 'bob.johnson@example.com', '2024-03-21'), +('Charlie Davis', 'charlie.davis@example.com', '2024-05-10'), +('Diana Garcia', 'diana.garcia@example.com', '2024-06-01'), +('Ethan Brown', 'ethan.brown@example.com', '2024-06-15'), +('Fiona Lee', 'fiona.lee@example.com', '2024-07-09'), +('George Clark', 'george.clark@example.com', '2024-08-23'), +('Hannah Wilson', 'hannah.wilson@example.com', '2024-09-05'), +('Isaac Martinez', 'isaac.martinez@example.com','2024-10-11'), +('Julia Adams', 'julia.adams@example.com', '2024-11-29'), +('Kevin Turner', 'kevin.turner@example.com', '2025-01-03'), +('Laura Perez', 'laura.perez@example.com', '2025-01-17'), +('Michael Scott', 'michael.scott@example.com', '2025-02-01'), +('Natalie King', 'natalie.king@example.com', '2025-02-14'), +('Oliver Young', 'oliver.young@example.com', '2025-03-02'); + +INSERT INTO products (product_name, category, price) +VALUES +('Laptop Pro 15', 'Electronics', 1299.99), +('Wireless Mouse', 'Electronics', 29.99), +('Office Desk', 'Furniture', 249.99), +('Ergonomic Chair', 'Furniture', 399.99), +('Noise‑Cancelling Headphones','Electronics',199.99), +('Smartphone X', 'Electronics', 999.99), +('Standing Desk Converter', 'Furniture', 159.99), +('4K Monitor 27"', 'Electronics', 449.99), +('Portable SSD 1TB', 'Electronics', 119.99), +('LED Desk Lamp', 'Home', 39.99), +('Bluetooth Speaker', 'Electronics', 79.99), +('Gaming Keyboard', 'Electronics', 89.99), +('Coffee Maker', 'Home', 59.99), +('Water Bottle Stainless', 'Sports', 24.99), +('Yoga Mat', 'Sports', 34.99); + +INSERT INTO orders (customer_id, product_id, quantity, order_date) +VALUES +((SELECT customer_id FROM customers WHERE email = 'alice.smith@example.com'), + (SELECT product_id FROM products WHERE product_name = 'Laptop Pro 15'), + 1, '2025-03-10'), + +((SELECT customer_id FROM customers WHERE email = 'bob.johnson@example.com'), + (SELECT product_id FROM products WHERE product_name = 'Wireless Mouse'), + 2, '2025-03-11'), + +((SELECT customer_id FROM customers WHERE email = 'charlie.davis@example.com'), + (SELECT product_id FROM products WHERE product_name = 'Ergonomic Chair'), + 1, '2025-03-12'), + +((SELECT customer_id FROM customers WHERE email = 'diana.garcia@example.com'), + (SELECT product_id FROM products WHERE product_name = '4K Monitor 27"'), + 2, '2025-03-13'), + +((SELECT customer_id FROM customers WHERE email = 'ethan.brown@example.com'), + (SELECT product_id FROM products WHERE product_name = 'Smartphone X'), + 1, '2025-03-13'), + +((SELECT customer_id FROM customers WHERE email = 'fiona.lee@example.com'), + (SELECT product_id FROM products WHERE product_name = 'Noise‑Cancelling Headphones'), + 1, '2025-03-14'), + +((SELECT customer_id FROM customers WHERE email = 'george.clark@example.com'), + (SELECT product_id FROM products WHERE product_name = 'Office Desk'), + 1, '2025-03-15'), + +((SELECT customer_id FROM customers WHERE email = 'hannah.wilson@example.com'), + (SELECT product_id FROM products WHERE product_name = 'Portable SSD 1TB'), + 3, '2025-03-15'), + +((SELECT customer_id FROM customers WHERE email = 'isaac.martinez@example.com'), + (SELECT product_id FROM products WHERE product_name = 'Standing Desk Converter'), + 1, '2025-03-15'), + +((SELECT customer_id FROM customers WHERE email = 'julia.adams@example.com'), + (SELECT product_id FROM products WHERE product_name = 'LED Desk Lamp'), + 2, '2025-03-16'), + +((SELECT customer_id FROM customers WHERE email = 'kevin.turner@example.com'), + (SELECT product_id FROM products WHERE product_name = 'Bluetooth Speaker'), + 1, '2025-03-16'), + +((SELECT customer_id FROM customers WHERE email = 'laura.perez@example.com'), + (SELECT product_id FROM products WHERE product_name = 'Gaming Keyboard'), + 1, '2025-03-16'), + +((SELECT customer_id FROM customers WHERE email = 'michael.scott@example.com'), + (SELECT product_id FROM products WHERE product_name = 'Coffee Maker'), + 1, '2025-03-17'), + +((SELECT customer_id FROM customers WHERE email = 'natalie.king@example.com'), + (SELECT product_id FROM products WHERE product_name = 'Water Bottle Stainless'), + 4, '2025-03-17'), + +((SELECT customer_id FROM customers WHERE email = 'oliver.young@example.com'), + (SELECT product_id FROM products WHERE product_name = 'Yoga Mat'), + 2, '2025-03-18'); diff --git a/month2_minimart_analysis/sql/json-report/Product(where query).json b/month2_minimart_analysis/sql/json-report/Product(where query).json new file mode 100644 index 0000000..6e236c9 --- /dev/null +++ b/month2_minimart_analysis/sql/json-report/Product(where query).json @@ -0,0 +1,51 @@ +{ +"SELECT * FROM products WHERE category = 'Electronics'": [ + { + "product_id" : 1, + "product_name" : "Laptop Pro 15", + "category" : "Electronics", + "price" : 1299.99 + }, + { + "product_id" : 2, + "product_name" : "Wireless Mouse", + "category" : "Electronics", + "price" : 29.99 + }, + { + "product_id" : 5, + "product_name" : "Noise‑Cancelling Headphones", + "category" : "Electronics", + "price" : 199.99 + }, + { + "product_id" : 6, + "product_name" : "Smartphone X", + "category" : "Electronics", + "price" : 999.99 + }, + { + "product_id" : 8, + "product_name" : "4K Monitor 27\"", + "category" : "Electronics", + "price" : 449.99 + }, + { + "product_id" : 9, + "product_name" : "Portable SSD 1TB", + "category" : "Electronics", + "price" : 119.99 + }, + { + "product_id" : 11, + "product_name" : "Bluetooth Speaker", + "category" : "Electronics", + "price" : 79.99 + }, + { + "product_id" : 12, + "product_name" : "Gaming Keyboard", + "category" : "Electronics", + "price" : 89.99 + } +]} diff --git a/month2_minimart_analysis/sql/json-report/customers_202506171654.json b/month2_minimart_analysis/sql/json-report/customers_202506171654.json new file mode 100644 index 0000000..fb0e229 --- /dev/null +++ b/month2_minimart_analysis/sql/json-report/customers_202506171654.json @@ -0,0 +1,93 @@ +{ +"SELECT * FROM customers": [ + { + "customer_id" : 1, + "name" : "Alice Smith", + "email" : "alice.smith@example.com", + "join_date" : "2024-02-14" + }, + { + "customer_id" : 2, + "name" : "Bob Johnson", + "email" : "bob.johnson@example.com", + "join_date" : "2024-03-21" + }, + { + "customer_id" : 3, + "name" : "Charlie Davis", + "email" : "charlie.davis@example.com", + "join_date" : "2024-05-10" + }, + { + "customer_id" : 4, + "name" : "Diana Garcia", + "email" : "diana.garcia@example.com", + "join_date" : "2024-06-01" + }, + { + "customer_id" : 5, + "name" : "Ethan Brown", + "email" : "ethan.brown@example.com", + "join_date" : "2024-06-15" + }, + { + "customer_id" : 6, + "name" : "Fiona Lee", + "email" : "fiona.lee@example.com", + "join_date" : "2024-07-09" + }, + { + "customer_id" : 7, + "name" : "George Clark", + "email" : "george.clark@example.com", + "join_date" : "2024-08-23" + }, + { + "customer_id" : 8, + "name" : "Hannah Wilson", + "email" : "hannah.wilson@example.com", + "join_date" : "2024-09-05" + }, + { + "customer_id" : 9, + "name" : "Isaac Martinez", + "email" : "isaac.martinez@example.com", + "join_date" : "2024-10-11" + }, + { + "customer_id" : 10, + "name" : "Julia Adams", + "email" : "julia.adams@example.com", + "join_date" : "2024-11-29" + }, + { + "customer_id" : 11, + "name" : "Kevin Turner", + "email" : "kevin.turner@example.com", + "join_date" : "2025-01-03" + }, + { + "customer_id" : 12, + "name" : "Laura Perez", + "email" : "laura.perez@example.com", + "join_date" : "2025-01-17" + }, + { + "customer_id" : 13, + "name" : "Michael Scott", + "email" : "michael.scott@example.com", + "join_date" : "2025-02-01" + }, + { + "customer_id" : 14, + "name" : "Natalie King", + "email" : "natalie.king@example.com", + "join_date" : "2025-02-14" + }, + { + "customer_id" : 15, + "name" : "Oliver Young", + "email" : "oliver.young@example.com", + "join_date" : "2025-03-02" + } +]} diff --git a/month2_minimart_analysis/sql/json-report/products_202506171657.json b/month2_minimart_analysis/sql/json-report/products_202506171657.json new file mode 100644 index 0000000..0e5941b --- /dev/null +++ b/month2_minimart_analysis/sql/json-report/products_202506171657.json @@ -0,0 +1,93 @@ +{ +"SELECT * FROM products": [ + { + "product_id" : 1, + "product_name" : "Laptop Pro 15", + "category" : "Electronics", + "price" : 1299.99 + }, + { + "product_id" : 2, + "product_name" : "Wireless Mouse", + "category" : "Electronics", + "price" : 29.99 + }, + { + "product_id" : 3, + "product_name" : "Office Desk", + "category" : "Furniture", + "price" : 249.99 + }, + { + "product_id" : 4, + "product_name" : "Ergonomic Chair", + "category" : "Furniture", + "price" : 399.99 + }, + { + "product_id" : 5, + "product_name" : "Noise‑Cancelling Headphones", + "category" : "Electronics", + "price" : 199.99 + }, + { + "product_id" : 6, + "product_name" : "Smartphone X", + "category" : "Electronics", + "price" : 999.99 + }, + { + "product_id" : 7, + "product_name" : "Standing Desk Converter", + "category" : "Furniture", + "price" : 159.99 + }, + { + "product_id" : 8, + "product_name" : "4K Monitor 27\"", + "category" : "Electronics", + "price" : 449.99 + }, + { + "product_id" : 9, + "product_name" : "Portable SSD 1TB", + "category" : "Electronics", + "price" : 119.99 + }, + { + "product_id" : 10, + "product_name" : "LED Desk Lamp", + "category" : "Home", + "price" : 39.99 + }, + { + "product_id" : 11, + "product_name" : "Bluetooth Speaker", + "category" : "Electronics", + "price" : 79.99 + }, + { + "product_id" : 12, + "product_name" : "Gaming Keyboard", + "category" : "Electronics", + "price" : 89.99 + }, + { + "product_id" : 13, + "product_name" : "Coffee Maker", + "category" : "Home", + "price" : 59.99 + }, + { + "product_id" : 14, + "product_name" : "Water Bottle Stainless", + "category" : "Sports", + "price" : 24.99 + }, + { + "product_id" : 15, + "product_name" : "Yoga Mat", + "category" : "Sports", + "price" : 34.99 + } +]} diff --git a/month2_minimart_analysis/sql/queries.sql b/month2_minimart_analysis/sql/queries.sql index 883d8ae..ad4e4a0 100644 --- a/month2_minimart_analysis/sql/queries.sql +++ b/month2_minimart_analysis/sql/queries.sql @@ -1 +1,74 @@ -- SQL queries for retrieving insights +-- Basic Queries +-- Use SELECT to retrieve all customers or all products. +SELECT * FROM customers; +SELECT * FROM products; + +-- Use WHERE to filter products by category (e.g., "Drinks"). +SELECT * FROM products WHERE category = 'Electronics'; + +-- Use ORDER BY to list recent orders by date. +SELECT * FROM orders ORDER BY order_date DESC; + + +-- Aggregation Queries +-- Use COUNT() to find the number of total orders. +SELECT COUNT(*) AS total_orders FROM orders; + +-- Use SUM() to calculate revenue per product (price × quantity). +SELECT p.product_name, SUM(p.price * o.quantity) AS total_revenue +FROM orders o +JOIN products p ON o.product_id = p.product_id +GROUP BY p.product_name; + + +-- Use AVG() to find the average product price. +SELECT ROUND(AVG(price), 2) AS average_price FROM products; + + + +-- JOINS +-- Use INNER JOIN to get detailed order information. +SELECT + o.order_id, + o.order_date, + o.quantity, + c.customer_id, + c.name AS customer_name, + c.email, + p.product_id, + p.product_name, + p.price +FROM orders AS o +INNER JOIN customers AS c ON o.customer_id = c.customer_id +INNER JOIN products AS p ON o.product_id = p.product_id +ORDER BY o.order_date DESC; + + +-- Use LEFT JOIN to list all customers and include their orders (if any). +SELECT + c.customer_id, + c.name, + c.email, + o.order_id, + o.order_date, + o.quantity, + p.product_name +FROM customers AS c +LEFT JOIN orders AS o ON o.customer_id = c.customer_id +LEFT JOIN products AS p ON p.product_id = o.product_id +ORDER BY c.customer_id, o.order_date; + + +-- Use LEFT JOIN to show products even if they haven’t been ordered. +SELECT + p.product_id, + p.product_name, + p.category, + p.price, + COALESCE(SUM(o.quantity), 0) AS total_units_sold, + COALESCE(SUM(o.quantity * p.price), 0)::numeric AS total_revenue +FROM products AS p +LEFT JOIN orders AS o ON o.product_id = p.product_id +GROUP BY p.product_id, p.product_name, p.category, p.price +ORDER BY total_units_sold DESC;