Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
Binary file not shown.
101 changes: 90 additions & 11 deletions 02_activities/assignments/assignment1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,128 @@

--SELECT
/* 1. Write a query that returns everything in the customer table. */

SELECT *
FROM customer;


/* 2. Write a query that displays all of the columns and 10 rows from the cus- tomer table,
sorted by customer_last_name, then customer_first_ name. */


SELECT *
FROM customer
ORDER BY customer_last_name, customer_first_name
LIMIT 10;

--WHERE
/* 1. Write a query that returns all customer purchases of product IDs 4 and 9. */
-- option 1

SELECT * FROM customer_purchases
WHERE product_id = 4
OR product_id = 9;

-- option 2


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

/*2. Write a query that returns all customer purchases and a new calculated column 'price' (quantity * cost_to_customer_per_qty),
filtered by vendor IDs between 8 and 10 (inclusive) using either:
1. two conditions using AND
2. one condition using BETWEEN
*/
-- option 1
-- option 1 X
--ALTER TABLE customer_purchases ADD price AS (quantity*cost_to_customer_per_qty);
SELECT *
FROM customer_purchases
WHERE vendor_id >=8
AND vendor_id <= 10;
--GROUP BY customer_id, product_id, vendor_id


-- option 2
SELECT
customer_id,
product_id,
vendor_id,

SUM(quantity*cost_to_customer_per_qty) as price

FROM customer_purchases
WHERE vendor_id BETWEEN 8 AND 10;

--CASE
/* 1. Products can be sold by the individual unit or by bulk measures like lbs. or oz.
Using the product table, write a query that outputs the product_id and product_name
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.” */

SELECT product_id, product_name

,CASE
WHEN product_qty_type = 'unit' THEN 'unit'
ELSE 'bulk'
END as prod_qty_type_condensed

FROM product;


/* 2. We want to flag all of the different types of pepper products that are sold at the market.
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. */


SELECT product_id, product_name

,CASE
WHEN product_qty_type = 'unit' THEN 'unit'
ELSE 'bulk'
END as prod_qty_type_condensed

,CASE
WHEN product_name LIKE '%pepper%'
THEN '1'
ELSE '0'
END as pepper_flag

FROM product;

--JOIN
/* 1. Write a query that INNER JOINs the vendor table to the vendor_booth_assignments table on the
vendor_id field they both have in common, and sorts the result by vendor_name, then market_date. */

SELECT *

FROM vendor INNER JOIN vendor_booth_assignments
ON vendor.vendor_id = vendor_booth_assignments.vendor_id

GROUP BY vendor_name, market_date;

/* SECTION 3 */

-- AGGREGATE
/* 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. */


SELECT vendor_id, COUNT (vendor_id) as num_of_rented
FROM vendor_booth_assignments
GROUP BY vendor_id;

/* 2. The Farmer’s Market Customer Appreciation Committee wants to give a bumper
sticker to everyone who has ever spent more than $2000 at the market. Write a query that generates a list
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. */

SELECT
c.customer_first_name,
c.customer_last_name,
c.customer_id,
SUM (cp.quantity*cp.cost_to_customer_per_qty) AS total_spent

FROM customer c
LEFT JOIN customer_purchases cp
ON c.customer_id = cp.customer_id

GROUP BY c.customer_first_name, c.customer_last_name, c.customer_id
HAVING SUM (cp.quantity*cp.cost_to_customer_per_qty) > 2000
ORDER BY c.customer_last_name, c.customer_first_name;

--Temp Table
/* 1. Insert the original vendor table into a temp.new_vendor and then add a 10th vendor:
Expand All @@ -82,19 +139,41 @@ When inserting the new vendor, you need to appropriately align the columns to be
VALUES(col1,col2,col3,col4,col5)
*/

CREATE TABLE temp.new_vendor AS

SELECT * FROM vendor;

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

SELECT * FROM temp.new_vendor;

-- Date
/*1. Get the customer_id, month, and year (in separate columns) of every purchase in the customer_purchases table.

HINT: you might need to search for strfrtime modifers sqlite on the web to know what the modifers for month
and year are! */

and year are! */ X

SELECT
customer_id,
strftime('%m', market_date) AS month,
strftime('%Y', market_date) AS year

FROM customer_purchases

/* 2. Using the previous query as a base, determine how much money each customer spent in April 2022.
Remember that money spent is quantity*cost_to_customer_per_qty.

HINTS: you will need to AGGREGATE, GROUP BY, and filter...
but remember, STRFTIME returns a STRING for your WHERE statement!! */

SELECT
customer_id,
SUM (quantity*cost_to_customer_per_qty) AS total_spent


FROM customer_purchases
WHERE strftime('%m', market_date) = '04'
AND strftime('%Y', market_date) = '2022'

GROUP BY customer_id
Binary file added 05_src/sql/assignment1.db
Binary file not shown.
24 changes: 24 additions & 0 deletions 05_src/sql/assignment1.sqbpro
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="assignment1.db" readonly="0" foreign_keys="0" case_sensitive_like="0" temp_store="0" wal_autocheckpoint="1000" synchronous="2"/><attached/><window><main_tabs open="structure browser pragmas query" current="3"/></window><tab_structure><column_width id="0" width="300"/><column_width id="1" width="0"/><column_width id="2" width="100"/><column_width id="3" width="7306"/><column_width id="4" width="0"/><expanded_item id="0" parent="1"/><expanded_item id="1" parent="1"/><expanded_item id="2" parent="1"/><expanded_item id="3" parent="1"/></tab_structure><tab_browse><table title="booth" custom_title="0" dock_id="2" table="4,5:mainbooth"/><dock_state state="000000ff00000000fd00000001000000020000000000000000fc0100000002fb000000160064006f0063006b00420072006f00770073006500310100000000ffffffff0000000000000000fb000000160064006f0063006b00420072006f00770073006500320100000000ffffffff0000010100ffffff000000000000000000000004000000040000000800000008fc00000000"/><default_encoding codec=""/><browse_table_settings><table schema="main" name="booth" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="104"/><column index="2" value="125"/><column index="3" value="300"/><column index="4" value="83"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table></browse_table_settings></tab_browse><tab_sql><sql name="assignment1.sql*" filename="/Users/Hiro/sql/02_activities/assignments/assignment1.sql">-- Reference to file &quot;/Users/Hiro/sql/02_activities/assignments/assignment1.sql&quot; (not supported by this version) --</sql><sql name="SQL 4">SELECT
c.customer_first_name,
c.customer_last_name,
c.customer_id,
SUM(cp.quantity * cp.cost_to_customer_per_qty) AS total_spent
FROM customer c
LEFT JOIN customer_purchases cp
ON c.customer_id = cp.customer_id
GROUP BY c.customer_first_name, c.customer_last_name, c.customer_id
HAVING SUM(cp.quantity * cp.cost_to_customer_per_qty) &gt; 2000
ORDER BY c.customer_last_name, c.customer_first_name;</sql><sql name="SQL 2">SELECT
c.customer_first_name,
c.customer_last_name,
c.customer_id,
SUM(cp.quantity * cp.cost_to_customer_per_qty) AS total_spent
FROM customer c
LEFT JOIN customer_purchases cp
ON c.customer_id = cp.customer_id
GROUP BY c.customer_first_name, c.customer_last_name, c.customer_id
HAVING SUM(cp.quantity * cp.cost_to_customer_per_qty) &gt; 2000
ORDER BY c.customer_last_name, c.customer_first_name;


</sql><current_tab id="0"/></tab_sql></sqlb_project>
138 changes: 138 additions & 0 deletions 05_src/sql/farmersmarket.sqbpro
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="farmersmarket.db" readonly="0" foreign_keys="1" case_sensitive_like="0" temp_store="0" wal_autocheckpoint="1000" synchronous="2"/><attached/><window><main_tabs open="structure browser pragmas query" current="3"/></window><tab_structure><column_width id="0" width="300"/><column_width id="1" width="0"/><column_width id="2" width="100"/><column_width id="3" width="7306"/><column_width id="4" width="0"/><expanded_item id="0" parent="1"/><expanded_item id="1" parent="1"/><expanded_item id="2" parent="1"/><expanded_item id="3" parent="1"/></tab_structure><tab_browse><table title="booth" custom_title="0" dock_id="1" table="4,5:mainbooth"/><dock_state state="000000ff00000000fd0000000100000002000001c30000021ffc0100000001fb000000160064006f0063006b00420072006f00770073006500310100000000000001c30000010a00ffffff000001c30000000000000004000000040000000800000008fc00000000"/><default_encoding codec=""/><browse_table_settings><table schema="main" name="booth" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="104"/><column index="2" value="125"/><column index="3" value="300"/><column index="4" value="83"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table></browse_table_settings></tab_browse><tab_sql><sql name="SQL 1*">-- SELECT

--selecting everything from customer
SELECT *
FROM customer;

-- add a static value
SELECT 2025 as this_year, 'April' as this_month, customer_id
FROM customer;

-- add an order by + limit
SELECT *
FROM customer
ORDER BY customer_last_name
LIMIT 10;

--WHERE

SELECT * FROM customer
WHERE customer_id = 1
AND customer_id = 2;

-- IN
SELECT * From customer_purchases
WHERE customer_id IN (3,4,5) -- only custmoer 3, 4, 5
AND vendor_id IN (3,4,5);

-- LLIKE
SELECT * FROM product
WHERE product_name LIKE '%pepper%';

-- customers with a last name starting with a
SELECT * FROM customer
WHERE customer_last_name LIKE 'a%';

SELECT * FROM product
WHERE product_size IS NULL
OR product_size = '';

</sql><sql name="SQL 2*">SELECT *
,CASE WHEN vendor_type = 'Fresh Focused'
THEN 'Wednesday'
WHEN vendor_type = 'Prepared Foods'
THEN 'Thursday'
ELSE 'Saturday'
END as day_of_specialty
--pie day, otherwise nothing
,CASE WHEN vendor_name LIKE '%pie%'
THEN 'Wednesday'
END as only_pie_day
,CASE WHEN vendor_name = &quot;Annie's Pies&quot; -- double quote work, but not for all editors!
THEN 'annie is best'
END
FROM vendor</sql><sql name="SQL 3*">--DISTINCT

-- Without distinct 4421 rows oof various customer_id
SELECT customer_id FROM customer_purchases;

SELECT DISTINCT customer_id FROM customer_purchases

-- without distinctio, only we/sad 150 tiems OVER
SELECT *
FROM market_date_info;

-- which vendor has sold product a customer
SELECT DISTINCT vendor_id
FROM customer_purchases; -- 3 rows, vendor id 7, 8 4

-- which vendor has sold products to a customer
SELECT DISTINCT vendor_id, product_id
FROM customer_purchases;

-- which vencor has sold products to a customer and which product was it and to whom it was sold?
SELECT DISTINCT vendor_id, customer_id, product_id
FROM customer_purchases

SELECT *
,CASE WHEN vendor_type = 'Fresh Focused'
THEN 'Wednesday'
WHEN vendor_type = 'Prepared Foods'
THEN 'Thursday'
ELSE 'Saturday'
END as day_of_specialty
--pie day, otherwise nothing
,CASE WHEN vendor_name LIKE '%pie%'
THEN 'Wednesday'
END as only_pie_day
,CASE WHEN vendor_name = &quot;Annie's Pies&quot; -- double quote work, but not for all editors!
THEN 'annie is best'
END as annie_is_king
FROM vendor

</sql><sql name="SQL 4*">-- INNER JOIN

-- get prodcut names along customer_purchases
SELECT
product_name,
market_date,
customer_id,
product_id,
customer_purchases.product_id

FROM customer_purchases
INNER JOIN product
ON customer_purchases.product_id = product.product_id</sql><sql name="SQL 5*">/* get product names alongside customer_purchases...only products that a customer has purchased will be present */
SELECT
product_name, -- coming from product
vendor_id, -- coming from cp...below
market_date,
customer_id,
customer_purchases.product_id

FROM customer_purchases
INNER JOIN product
ON customer_purchases.product_id = product.product_id;

/* which vendor has sold products to a customer AND which product was it AND to whom was it sold ? */
SELECT DISTINCT vendor_id,
product_id,
--customer_id -- this is dissatisfying...let's have a name!
customer_first_name,
customer_last_name

FROM customer_purchases as cp
INNER JOIN customer as c
ON cp.customer_id = c.customer_id</sql><sql name="SQL 6*">-- LEFT JOIN

-- there are produts being bought, but not have not?
SELECT
p.product_id, cp.product_id,
cp.*--all colums in cp TABLE

FROM product p
LEFT JOIN customer_purchases cp
ON p.product_id = cp.product_id

</sql><sql name="SQL 7*">SELECT DISTINCT
p.product_id, product_name, </sql><sql name="SQL 8"></sql><current_tab id="5"/></tab_sql></sqlb_project>