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
8 changes: 7 additions & 1 deletion 02_activities/assignments/DC_Cohort/Assignment1.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,11 @@ Consider, for example, concepts of fariness, inequality, social structures, marg


```
Your thoughts...
Databases are part of many areas of our everyday lives in today’s digital world, including university portals, banking apps, navigation systems, search engines, and streaming platforms. The way these systems are designed - their underlying structure or “schema” - shapes how people are categorized and understood within them. For example, many databases require users to select from fixed options such as marital status or gender, reflecting assumptions about what categories are allowed and used to link datapoints within the data base.

Another important value reflected in many modern data systems is a users consumption behavior. In particular, databases are often built to track, predict, and encourage user spending and engagement. For instance, online shopping platforms frequently require users to create an account rather than allowing guest checkout, enabling the system to store personal data and generate “recommended for you” suggestions.

A further value embedded in many databases is locality or residency. Many systems, such as those used for credit cards or Social Insurance Numbers, require a fixed address or proof of residence. This reflects an assumption that individuals are tied to a specific place. However, in an increasingly global and mobile world, where remote work and travel are more common, this emphasis on having a permanent address may not reflect everyone’s reality.

The underlying trend/challenge in values that are embedded in databases are the increasingly importance of personalization. Adds, medicine, consumer goods are being tailored more and more effectively to every individual. This, on the one hand necessitates databases to build a personalized user profile, however, in the future this will also raise the need for personalized answer options, making databases (which ultimately categorize and structure social networks/data) more fluid and dynamic.
```
Binary file not shown.
67 changes: 55 additions & 12 deletions 02_activities/assignments/DC_Cohort/assignment1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
--SELECT
/* 1. Write a query that returns everything in the customer table. */
--QUERY 1

SELECT * FROM customer;



Expand All @@ -16,9 +16,9 @@
/* 2. Write a query that displays all of the columns and 10 rows from the customer table,
sorted by customer_last_name, then customer_first_ name. */
--QUERY 2



SELECT * FROM customer
ORDER BY customer_first_name
LIMIT 10;

--END QUERY

Expand All @@ -27,7 +27,9 @@ sorted by customer_last_name, then customer_first_ name. */
/* 1. Write a query that returns all customer purchases of product IDs 4 and 9.
Limit to 25 rows of output. */
--QUERY 3

SELECT * FROM customer_purchases
WHERE product_id IN (4, 9)
LIMIT 25



Expand All @@ -42,7 +44,10 @@ filtered by customer IDs between 8 and 10 (inclusive) using either:
Limit to 25 rows of output.
*/
--QUERY 4

SELECT *, quantity * cost_to_customer_per_qty AS Price
FROM customer_purchases
WHERE customer_id BETWEEN 8 AND 10
LIMIT 25;



Expand All @@ -55,6 +60,13 @@ Using the product table, write a query that outputs the product_id and product_n
columns and add a column called prod_qty_type_condensed that displays the word “unit”
if the product_qty_type is “unit,” and otherwise displays the word “bulk.” */
--QUERY 5
SELECT product_id, product_name,
CASE
WHEN product_qty_type = 'unit' THEN 'unit'
ELSE 'bulk'

END AS prod_qty_type_condensed
FROM product



Expand All @@ -66,7 +78,18 @@ if the product_qty_type is “unit,” and otherwise displays the word “bulk.
add a column to the previous query called pepper_flag that outputs a 1 if the product_name
contains the word “pepper” (regardless of capitalization), and otherwise outputs 0. */
--QUERY 6

SELECT product_id, product_name,
CASE
WHEN product_qty_type = 'unit' THEN 'unit'
ELSE 'bulk'
END AS prod_qty_type_condensed,

CASE
WHEN LOWER(product_name) LIKE '%pepper%' THEN 1
ELSE 0
END AS pepper_flag

FROM product



Expand All @@ -79,7 +102,10 @@ vendor_id field they both have in common, and sorts the result by market_date, t
Limit to 24 rows of output. */
--QUERY 7


SELECT * FROM vendor_booth_assignments
INNER JOIN vendor ON vendor_booth_assignments.vendor_id = vendor.vendor_id
ORDER BY market_date, vendor_name
LIMIT 24


--END QUERY
Expand All @@ -92,8 +118,10 @@ Limit to 24 rows of output. */
/* 1. Write a query that determines how many times each vendor has rented a booth
at the farmer’s market by counting the vendor booth assignments per vendor_id. */
--QUERY 8


SELECT vendor_id,
COUNT(vendor_id) AS booth_count
FROM vendor_booth_assignments
GROUP BY vendor_id;


--END QUERY
Expand All @@ -106,6 +134,12 @@ of customers for them to give stickers to, sorted by last name, then first name.
HINT: This query requires you to join two tables, use an aggregate function, and use the HAVING keyword. */
--QUERY 9

SELECT customer.customer_last_name, customer.customer_first_name, customer.customer_id, SUM(customer_purchases.cost_to_customer_per_qty) AS total_spent
FROM customer_purchases
JOIN customer ON customer_purchases.customer_id = customer.customer_id
GROUP BY customer.customer_id
HAVING SUM(cost_to_customer_per_qty) > 2000
ORDER BY customer_last_name, customer_first_name;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

total_spent should be SUM(quantity * cost_to_customer_per_qty)

The HAVING clause can simply use total_spent > 2000 instead of recalculating.

The result set will give 19 rows instead of just 3.



Expand All @@ -124,7 +158,10 @@ When inserting the new vendor, you need to appropriately align the columns to be
VALUES(col1,col2,col3,col4,col5)
*/
--QUERY 10

CREATE TEMPORARY TABLE new_vendor AS
SELECT * FROM vendor
UNION ALL
SELECT '10', 'Thomas Superfood Store', 'Fresh Focused', 'Thomas', 'Rosenthal';


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct answer uses INSERT statement:

CREATE TABLE temp.new_vendor AS
SELECT * FROM vendor

INSERT INTO temp.new_vendor
VALUES (10, "Thomass Superfood Store" , "Fresh Focused", "Thomas", "Rosenthal")


Expand All @@ -138,7 +175,9 @@ HINT: you might need to search for strfrtime modifers sqlite on the web to know
and year are!
Limit to 25 rows of output. */
--QUERY 11

SELECT customer_id, strftime('%m', market_date) AS month, strftime('%Y', market_date) AS year
FROM customer_purchases
LIMIT 25



Expand All @@ -152,6 +191,10 @@ HINTS: you will need to AGGREGATE, GROUP BY, and filter...
but remember, STRFTIME returns a STRING for your WHERE statement...
AND be sure you remove the LIMIT from the previous query before aggregating!! */
--QUERY 12
SELECT customer_id, SUM(quantity * cost_to_customer_per_qty) AS total_spend
FROM customer_purchases
WHERE market_date LIKE '2022-04%'
GROUP BY customer_id;



Expand Down