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
108 changes: 94 additions & 14 deletions 02_activities/assignments/DC_Cohort/assignment1.sql
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
/* ASSIGNMENT 1 */
/* SECTION 2 */


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

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


/*2. Write a query that returns all customer purchases and a new calculated column 'price' (quantity * cost_to_customer_per_qty),
Expand All @@ -23,30 +28,64 @@ filtered by customer IDs between 8 and 10 (inclusive) using either:
2. one condition using BETWEEN
*/
-- option 1

SELECT
*,
(quantity * cost_to_customer_per_qty) AS PRICE
FROM customer_purchases
WHERE customer_id >= 8
AND customer_id <=10;

-- option 2

SELECT
*,
(quantity * cost_to_customer_per_qty) AS price
FROM customer_purchases
WHERE customer_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. */


WITH product_flagged AS (
SELECT
product_id,
product_name,
CASE
WHEN product_qty_type = 'unit' THEN 'unit'
ELSE 'bulk'
END AS prod_qty_type_condensed
FROM product
)
SELECT
*,
CASE
WHEN LOWER(product_name) LIKE '%pepper%' THEN 1
ELSE 0
END AS pepper_flag
FROM product_flagged;

--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 AS v
INNER JOIN vendor_booth_assignments AS vba
ON v.vendor_id = vba.vendor_id
ORDER BY v.vendor_name, vba.market_date;



Expand All @@ -55,15 +94,27 @@ vendor_id field they both have in common, and sorts the result by vendor_name, t
-- 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(*) AS booth_rental_count
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,
FROM customer AS c
INNER JOIN customer_purchases AS cp
ON c.customer_id = cp.customer_id
GROUP BY c.customer_id, c.customer_first_name, c.customer_last_name
HAVING SUM (cp.quantity * cp.cost_to_customer_per_qty) > 2000
ORDER BY c.customer_last_name, c.customer_first_name ;


--Temp Table
Expand All @@ -78,14 +129,30 @@ 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
(vendor_id, vendor_name, vendor_type, cendor_owner_first_name, vendor_owner_last_name)
VALUES
(10,'Thomass Superfood Store', ' Fresh Focused store', 'Thomas', 'Rosenthal');

SELECT *
FROM temp.new_vendor
ORDER BY vendor_id;

-- 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! */

SELECT
customer_id,
strftime('%m', purchase_date) AS month,
strftime('%Y', purchase_date) AS year
FROM customer_purchases;


/* 2. Using the previous query as a base, determine how much money each customer spent in April 2022.
Expand All @@ -94,3 +161,16 @@ 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,
strftime('%m', purchase_date) AS month,
strftime('%Y', purchase_date) AS year
FROM customer_purchases;

SELECT
customer_id,
SUM(quantity * cost_to_customer_per_qty) AS total_spent
FROM customer_purchases
WHERE strftime('%m', purchase_date) = '04'
AND strftime('%Y',purchase_date) = '2022'
GROUP BY customer_id;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 11 additions & 11 deletions 03_instructional_team/sqbpro_originals/module_2.sqbpro
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="/Users/thomas/Documents/GitHub/02-intro_sql/05_src/sql/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="12168"/><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="000000ff00000000fd000000010000000200000536000003a5fc0100000002fb000000160064006f0063006b00420072006f00770073006500310100000000ffffffff0000000000000000fb000000160064006f0063006b00420072006f00770073006500320100000000000005360000019b00ffffff000005360000000000000004000000040000000800000008fc00000000"/><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="136"/><column index="2" value="164"/><column index="3" value="300"/><column index="4" value="108"/></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 2">/* MODULE 2 */
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><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="12168"/><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="000000ff00000000fd000000010000000200000536000003a5fc0100000002fb000000160064006f0063006b00420072006f00770073006500310100000000ffffffff0000000000000000fb000000160064006f0063006b00420072006f00770073006500320100000000000005360000012300ffffff000005360000000000000004000000040000000800000008fc00000000"/><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="136"/><column index="2" value="164"/><column index="3" value="300"/><column index="4" value="108"/></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="SELECT*">/* MODULE 2 */
/* SELECT */


Expand All @@ -13,11 +13,11 @@ SELECT



/* 4. Select multiple specific columns * /
/* 4. Select multiple specific columns */



/* 5. Add a static value in a column */</sql><sql name="SQL 7">/* MODULE 2 */
/* 5. Add a static value in a column */</sql><sql name="WHERE">/* MODULE 2 */
/* WHERE */

/* 1. Select only customer 1 from the customer table */
Expand All @@ -38,11 +38,11 @@ WHERE



/* 5. Nulls and Blanks* /
/* 5. Nulls and Blanks*/



/* 6. BETWEEN x AND y */</sql><sql name="SQL 10">/* MODULE 2 */
/* 6. BETWEEN x AND y */</sql><sql name="CASE">/* MODULE 2 */
/* CASE */


Expand All @@ -61,7 +61,7 @@ SELECT *
/* 4. Experiment with selecting a different column instead of just a string value */


FROM vendor</sql><sql name="SQL 9">/* MODULE 2 */
FROM vendor</sql><sql name="DISTINCT">/* MODULE 2 */
/* DISTINCT */


Expand All @@ -81,15 +81,15 @@ SELECT customer_id FROM customer_purchases



/* 4. Which vendor has sold products to a customer ... and which product was it * /
/* 4. Which vendor has sold products to a customer ... and which product was it */



/* 5. Which vendor has sold products to a customer
... and which product was it?
... AND to whom was it sold*/

</sql><sql name="SQL 8">/* MODULE 2 */
</sql><sql name="INNER_JOIN">/* MODULE 2 */
/* INNER JOIN */


Expand All @@ -109,7 +109,7 @@ SELECT customer_id FROM customer_purchases
-- using table aliases


</sql><sql name="SQL 11">/* MODULE 2 */
</sql><sql name="LEFT_JOIN">/* MODULE 2 */
/* LEFT JOIN */


Expand Down Expand Up @@ -141,7 +141,7 @@ LEFT JOIN product AS p
...Note how the row count changed from 24 to 23
*/

</sql><sql name="SQL 12">/* MODULE 2 */
</sql><sql name="multiple_table_joins">/* MODULE 2 */
/* Multiple Table JOINs */


Expand All @@ -157,4 +157,4 @@ LEFT JOIN product AS p

Why do we have more rows now?*/

</sql><current_tab id="6"/></tab_sql></sqlb_project>
</sql><current_tab id="0"/></tab_sql></sqlb_project>
20 changes: 10 additions & 10 deletions 03_instructional_team/sqbpro_originals/module_3.sqbpro
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="/Users/thomas/Documents/GitHub/02-intro_sql/05_src/sql/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="12168"/><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="4" table="4,5:mainbooth"/><dock_state state="000000ff00000000fd000000010000000200000536000003a5fc0100000003fb000000160064006f0063006b00420072006f00770073006500310100000000ffffffff0000000000000000fb000000160064006f0063006b00420072006f00770073006500320100000000000005360000000000000000fb000000160064006f0063006b00420072006f00770073006500340100000000ffffffff0000012300ffffff000005360000000000000004000000040000000800000008fc00000000"/><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="136"/><column index="2" value="164"/><column index="3" value="300"/><column index="4" value="108"/></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">/* MODULE 3 */
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><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="12168"/><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="4" table="4,5:mainbooth"/><dock_state state="000000ff00000000fd000000010000000200000536000003a5fc0100000003fb000000160064006f0063006b00420072006f00770073006500310100000000ffffffff0000000000000000fb000000160064006f0063006b00420072006f00770073006500320100000000000005360000000000000000fb000000160064006f0063006b00420072006f00770073006500340100000000ffffffff0000012300ffffff000005360000000000000004000000040000000800000008fc00000000"/><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="136"/><column index="2" value="164"/><column index="3" value="300"/><column index="4" value="108"/></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="COUNT">/* MODULE 3 */
/* COUNT */


Expand All @@ -18,7 +18,7 @@
4. How many unique products were bought */


</sql><sql name="SQL 10">/* MODULE 3 */
</sql><sql name="SUM_AVG">/* MODULE 3 */
/* SUM &amp; AVG */


Expand All @@ -29,7 +29,7 @@
/* 2. How much does each customer spend on average */


</sql><sql name="SQL 11">/* MODULE 3 */
</sql><sql name="MIN_MAX">/* MODULE 3 */
/* MIN &amp; MAX */


Expand All @@ -54,7 +54,7 @@
... not particularly useful? */


</sql><sql name="SQL 12">/* MODULE 3 */
</sql><sql name="arithmitic">/* MODULE 3 */
/* Arithmitic */


Expand All @@ -68,7 +68,7 @@ SELECT

/* 3. What about every third? */

</sql><sql name="SQL 13">/* MODULE 3 */
</sql><sql name="HAVING">/* MODULE 3 */
/* HAVING */


Expand All @@ -81,7 +81,7 @@ Filter to customer_id between 1 and 5 and total_cost &gt; 50
/* 2. How many products were bought?
Filter to number of purchases between 300 and 500 */

</sql><sql name="SQL 13">/* MODULE 3 */
</sql><sql name="subquery_joins">/* MODULE 3 */
/* Subquery FROM */


Expand All @@ -94,7 +94,7 @@ Filter to number of purchases between 300 and 500 */
/* 2. What is the single item that has been bought in the greatest quantity?*/


</sql><sql name="SQL 14">/* MODULE 3 */
</sql><sql name="subquery_where">/* MODULE 3 */
/* Subquery WHERE */


Expand All @@ -105,7 +105,7 @@ Filter to number of purchases between 300 and 500 */

/* 2. What is the name of the vendor who sells pie */

</sql><sql name="SQL 15">/* MODULE 3 */
</sql><sql name="CTE">/* MODULE 3 */
/* Common Table Expression (CTE) */


Expand All @@ -118,7 +118,7 @@ SELECT

/* ... re-aggregate the daily sales for each WEEK instead now */

</sql><sql name="SQL 16">/* MODULE 3 */
</sql><sql name="temp_table">/* MODULE 3 */
/* Temp Tables */


Expand All @@ -142,7 +142,7 @@ CREATE TABLE temp.new_vendor_inventory AS
/* 2. put the previous table into another temp table, e.g. as temp.new_new_vendor_inventory */


</sql><sql name="SQL 18">/* MODULE 3 */
</sql><sql name="dates">/* MODULE 3 */
/* Date functions */


Expand Down
Loading