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
65 changes: 55 additions & 10 deletions 02_activities/assignments/assignment1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@
--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),
Expand All @@ -27,30 +35,52 @@ filtered by vendor 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 vendor_id >=8 AND vendor_id <=10;
-- option 2

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



Expand All @@ -60,6 +90,11 @@ vendor_id field they both have in common, and sorts the result by vendor_name, t
/* 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 (booth_number)
FROM vendor_booth_assignments
GROUP BY booth_number;



/* 2. The Farmer’s Market Customer Appreciation Committee wants to give a bumper
Expand All @@ -68,7 +103,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. */


SELECT c.customer_first_name, c.customer_last_name
,sum(cp.quantity*cp.cost_to_customer_per_qty) as spent
FROM customer as c
INNER JOIN customer_purchases as cp ON c.customer_id = cp.customer_id
GROUP BY c.customer_first_name, c.customer_last_name
HAVING spent > 2000;

--Temp Table
/* 1. Insert the original vendor table into a temp.new_vendor and then add a 10th vendor:
Expand All @@ -81,7 +121,12 @@ When inserting the new vendor, you need to appropriately align the columns to be
-> To insert the new row use VALUES, specifying the value you want for each column:
VALUES(col1,col2,col3,col4,col5)
*/
CREATE TABLE temp.new_vendor AS
SELECT *
FROM vendor;

INSERT INTO new_vendor
VALUES(10, 'Thomass Superfood Store', 'a Fresh Focused store', 'Thomas', 'Rosenthal');


-- Date
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<mxfile host="app.diagrams.net" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36" version="26.2.13">
<diagram name="Page-1" id="0IkqfYOMCBTJ9_aRLvQl">
<mxGraphModel dx="1428" dy="743" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="8CKtVp2OdXmB3I-kPGJz-9" value="Customer Purchases" style="swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=30;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="350" y="120" width="170" height="230" as="geometry" />
</mxCell>
<mxCell id="8CKtVp2OdXmB3I-kPGJz-10" value="product_Id&lt;div&gt;&lt;br&gt;&lt;div&gt;vendor_id&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;market_date&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;div&gt;customer_id&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;quantity&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;cost_to_customer_per_qty&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;transaction_time&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;/div&gt;" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;spacingLeft=4;spacingRight=4;overflow=hidden;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rotatable=0;whiteSpace=wrap;html=1;" vertex="1" parent="8CKtVp2OdXmB3I-kPGJz-9">
<mxGeometry y="30" width="170" height="200" as="geometry" />
</mxCell>
<mxCell id="8CKtVp2OdXmB3I-kPGJz-19" value="Customer" style="swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=30;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="640" y="130" width="140" height="190" as="geometry" />
</mxCell>
<mxCell id="8CKtVp2OdXmB3I-kPGJz-20" value="customer_id&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;customer_first_name&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;customer_last_name&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;customer_postal_code&lt;/div&gt;" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;spacingLeft=4;spacingRight=4;overflow=hidden;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rotatable=0;whiteSpace=wrap;html=1;" vertex="1" parent="8CKtVp2OdXmB3I-kPGJz-19">
<mxGeometry y="30" width="140" height="160" as="geometry" />
</mxCell>
<mxCell id="8CKtVp2OdXmB3I-kPGJz-27" value="" style="endArrow=none;html=1;rounded=0;exitX=1;exitY=0.071;exitDx=0;exitDy=0;exitPerimeter=0;entryX=0;entryY=0.05;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="8CKtVp2OdXmB3I-kPGJz-29" target="8CKtVp2OdXmB3I-kPGJz-10">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="280" y="159" as="sourcePoint" />
<mxPoint x="348" y="159" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="8CKtVp2OdXmB3I-kPGJz-28" value="Product" style="swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=30;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="90" y="120" width="150" height="170" as="geometry" />
</mxCell>
<mxCell id="8CKtVp2OdXmB3I-kPGJz-29" value="&lt;span style=&quot;text-align: center;&quot;&gt;product_id&lt;/span&gt;&lt;div&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;text-align: center; text-wrap-mode: nowrap;&quot;&gt;product_name&lt;/span&gt;&lt;span style=&quot;text-align: center; text-wrap-mode: nowrap;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;text-align: center; text-wrap-mode: nowrap;&quot;&gt;&lt;br&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;text-align: center; text-wrap-mode: nowrap;&quot;&gt;product_size&lt;/span&gt;&lt;span style=&quot;text-align: center; text-wrap-mode: nowrap;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;text-align: center; text-wrap-mode: nowrap;&quot;&gt;&lt;br&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;text-align: center; text-wrap-mode: nowrap;&quot;&gt;product_category_id&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;text-align: center; text-wrap-mode: nowrap;&quot;&gt;&lt;br&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;text-align: center; text-wrap-mode: nowrap;&quot;&gt;product_qty_type&lt;/span&gt;&lt;span style=&quot;text-align: center; text-wrap-mode: nowrap;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;spacingLeft=4;spacingRight=4;overflow=hidden;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rotatable=0;whiteSpace=wrap;html=1;" vertex="1" parent="8CKtVp2OdXmB3I-kPGJz-28">
<mxGeometry y="30" width="150" height="140" as="geometry" />
</mxCell>
<mxCell id="8CKtVp2OdXmB3I-kPGJz-32" value="" style="endArrow=none;html=1;rounded=0;entryX=0.018;entryY=0.247;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" target="8CKtVp2OdXmB3I-kPGJz-20">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="520" y="250" as="sourcePoint" />
<mxPoint x="560" y="250" as="targetPoint" />
<Array as="points">
<mxPoint x="580" y="250" />
<mxPoint x="580" y="220" />
<mxPoint x="580" y="199" />
</Array>
</mxGeometry>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
Binary file not shown.
1 change: 1 addition & 0 deletions 02_activities/assignments/images/farmersmarket.sqbpro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="C:/Users/aktur/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="8651"/><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"/><expanded_item id="4" parent="1"/></tab_structure><tab_browse><table title="temp.new_vendor" custom_title="0" dock_id="1" table="4,10:tempnew_vendor"/><dock_state state="000000ff00000000fd0000000100000002000004f200000386fc0100000001fb000000160064006f0063006b00420072006f00770073006500310100000000000004f20000011f00ffffff000004f20000000000000004000000040000000800000008fc00000000"/><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="90"/><column index="2" value="108"/><column index="3" value="300"/><column index="4" value="72"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="customer" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="77"/><column index="2" value="127"/><column index="3" value="125"/><column index="4" value="136"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="customer_purchases" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort><column index="0" mode="0"/></sort><column_widths><column index="1" value="69"/><column index="2" value="64"/><column index="3" value="85"/><column index="4" value="79"/><column index="5" value="55"/><column index="6" value="158"/><column index="7" value="103"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="market_date_info" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="85"/><column index="2" value="78"/><column index="3" value="85"/><column index="4" value="79"/><column index="5" value="113"/><column index="6" value="107"/><column index="7" value="84"/><column index="8" value="140"/><column index="9" value="111"/><column index="10" value="114"/><column index="11" value="105"/><column index="12" value="112"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="postal_data" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="89"/><column index="2" value="62"/><column index="3" value="93"/><column index="4" value="68"/><column index="5" value="160"/><column index="6" value="111"/><column index="7" value="103"/><column index="8" value="79"/><column index="9" value="235"/><column index="10" value="238"/><column index="11" value="71"/><column index="12" value="99"/><column index="13" value="132"/><column index="14" value="121"/><column index="15" value="116"/><column index="16" value="209"/><column index="17" value="206"/><column index="18" value="131"/><column index="19" value="178"/><column index="20" value="262"/><column index="21" value="256"/><column index="22" value="300"/><column index="23" value="146"/><column index="24" value="198"/><column index="25" value="201"/><column index="26" value="201"/><column index="27" value="111"/><column index="28" value="151"/><column index="29" value="85"/><column index="30" value="93"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="product" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="69"/><column index="2" value="296"/><column index="3" value="117"/><column index="4" value="126"/><column index="5" value="107"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="product_category" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="124"/><column index="2" value="234"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="temp" name="new_vendor" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="64"/><column index="2" value="257"/><column index="3" value="234"/><column index="4" value="156"/><column index="5" value="154"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="vendor" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="64"/><column index="2" value="257"/><column index="3" value="234"/><column index="4" value="156"/><column index="5" value="154"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="vendor_booth_assignments" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort><column index="0" mode="0"/></sort><column_widths><column index="1" value="66"/><column index="2" value="92"/><column index="3" value="85"/></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="C:/Users/aktur/sql/02_activities/assignments/assignment1.sql">-- Reference to file &quot;C:/Users/aktur/sql/02_activities/assignments/assignment1.sql&quot; (not supported by this version) --</sql><current_tab id="0"/></tab_sql></sqlb_project>
Binary file added 05_src/sql/farmersmarket.db-journal
Binary file not shown.