forked from UofT-DSI/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
assignment-one sql #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CarlaKulcsar
wants to merge
1
commit into
main
Choose a base branch
from
assignment-one
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
| --SELECT | ||
| /* 1. Write a query that returns everything in the customer table. */ | ||
| --QUERY 1 | ||
|
|
||
| SELECT * FROM customer; | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -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 | ||
|
|
||
|
|
@@ -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 | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -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; | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -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 | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -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 | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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; | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -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'; | ||
|
|
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The correct answer uses INSERT statement: |
||
|
|
||
|
|
@@ -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 | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -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; | ||
|
|
||
|
|
||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
total_spentshould beSUM(quantity * cost_to_customer_per_qty)The HAVING clause can simply use
total_spent > 2000instead of recalculating.The result set will give 19 rows instead of just 3.