Repository navigation
Assignment two - #2
Xinyu-Liao wants to merge 3 commits into
Conversation
SQL Assignment 2 feedbackSQL checks only. ERDs, written responses, and rubric point totals require instructor marking. Query 1: ✅ Correct All product descriptions and NULL replacements are correct. SQL and outputSELECT
product_name || ', ' ||
COALESCE(product_size, '') ||
' (' || COALESCE(product_qty_type, 'unit') || ')'
FROM product;[
{
"product_name || ', ' ||\n COALESCE(product_size, '') ||\n ' (' || COALESCE(product_qty_type, 'unit') || ')'": "Habanero Peppers - Organic, medium (lbs)"
},
{
"product_name || ', ' ||\n COALESCE(product_size, '') ||\n ' (' || COALESCE(product_qty_type, 'unit') || ')'": "Jalapeno Peppers - Organic, small (lbs)"
},
{
"product_name || ', ' ||\n COALESCE(product_size, '') ||\n ' (' || COALESCE(product_qty_type, 'unit') || ')'": "Poblano Peppers - Organic, large (unit)"
}
]Query 2: ✅ Correct Visit dates are numbered correctly for each customer. SQL and outputSELECT
customer_id,
market_date,
ROW_NUMBER () OVER (PARTITION BY customer_id ORDER BY market_date) AS visit_number
FROM (
SELECT DISTINCT
customer_id,
market_date
FROM customer_purchases
WHERE market_date < '2022-04-29');[
{
"customer_id": 1,
"market_date": "2022-04-08",
"visit_number": 1
},
{
"customer_id": 1,
"market_date": "2022-04-15",
"visit_number": 2
},
{
"customer_id": 1,
"market_date": "2022-04-19",
"visit_number": 3
}
]Query 3: ✅ Correct Only each customer's most recent visit is returned. SQL and outputSELECT *
FROM (
SELECT
customer_id,
market_date,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC
) AS visit_number
FROM customer_purchases
) AS ranked_visits
WHERE visit_number = 1
ORDER BY customer_id;[
{
"customer_id": 1,
"market_date": "2023-10-13",
"visit_number": 1
},
{
"customer_id": 2,
"market_date": "2023-10-13",
"visit_number": 1
},
{
"customer_id": 3,
"market_date": "2023-10-10",
"visit_number": 1
}
]Query 4: ✅ Correct The purchase count is correct for every customer and product. SQL and outputSELECT
customer_id,
product_id,
market_date,
COUNT(*) OVER (PARTITION BY customer_id, product_id) AS purchase_count
FROM customer_purchases
WHERE market_date < '2022-04-29'
ORDER BY customer_id, product_id;[
{
"customer_id": 1,
"product_id": 4,
"market_date": "2022-04-19",
"purchase_count": 2
},
{
"customer_id": 1,
"product_id": 4,
"market_date": "2022-04-26",
"purchase_count": 2
},
{
"customer_id": 1,
"product_id": 5,
"market_date": "2022-04-08",
"purchase_count": 6
}
]Query 5: ✅ Correct Product descriptions are extracted and trimmed correctly. SQL and outputSELECT
product_name,
TRIM(SUBSTR(
product_name,
NULLIF(INSTR(product_name, '-'), 0) + 1
)
) AS description
FROM product;[
{
"product_name": "Habanero Peppers - Organic",
"description": "Organic"
},
{
"product_name": "Jalapeno Peppers - Organic",
"description": "Organic"
},
{
"product_name": "Poblano Peppers - Organic",
"description": "Organic"
}
]Query 6: ✅ Correct Only product sizes containing a number are returned. SQL and outputSELECT
product_name,
product_size
FROM product
WHERE product_size REGEXP '[0-9]';[
{
"product_name": "Banana Peppers - Jar",
"product_size": "8 oz"
},
{
"product_name": "Whole Wheat Bread",
"product_size": "1.5 lbs"
},
{
"product_name": "Apple Pie",
"product_size": "10\""
}
]Query 7: ✅ Correct The highest and lowest sales dates and totals are correct. SQL and outputWITH daily_sales AS (
SELECT
market_date,
SUM(quantity * cost_per_quantity) AS total_sales
FROM customer_purchases
GROUP BY market_date),
ranked_sales AS (
SELECT
market_date,
total_sales,
RANK() OVER (ORDER BY total_sales DESC) AS best_rank,
RANK() OVER (ORDER BY total_sales ASC) AS worst_rank
FROM daily_sales
)
SELECT
'Best Day' AS day_type,
market_date,
total_sales
FROM ranked_sales
WHERE best_rank = 1
UNION
SELECT
'Worst Day' AS day_type,
market_date,
total_sales
FROM ranked_sales
WHERE worst_rank = 1;[
{
"day_type": "Best Day",
"market_date": "2022-12-20",
"total_sales": 979
},
{
"day_type": "Worst Day",
"market_date": "2023-04-07",
"total_sales": 272.5
}
]Query 8: ✅ Correct Possible revenue is correct for every vendor and product. SQL and outputWITH vendor_products AS (
SELECT
vendor_id,
product_id,
AVG(original_price) AS price
FROM vendor_inventory
GROUP BY vendor_id, product_id
),
potential_sales AS (
SELECT
vp.vendor_id,
vp.product_id,
5 * vp.price AS sales_per_customer
FROM vendor_products AS vp
CROSS JOIN customer AS c
)
SELECT
v.vendor_name,
p.product_name,
ROUND(SUM(ps.sales_per_customer), 2) AS total_sales
FROM potential_sales AS ps
JOIN vendor AS v
ON ps.vendor_id = v.vendor_id
JOIN product AS p
ON ps.product_id = p.product_id
GROUP BY
ps.vendor_id,
ps.product_id,
v.vendor_name,
p.product_name
ORDER BY v.vendor_name, p.product_name;[
{
"vendor_name": "Annie's Pies",
"product_name": "Apple Pie",
"total_sales": 2340
},
{
"vendor_name": "Annie's Pies",
"product_name": "Cherry Pie",
"total_sales": 2340
},
{
"vendor_name": "Annie's Pies",
"product_name": "Whole Wheat Bread",
"total_sales": 845
}
]Query 9: ✅ Correct Unit products and current snapshot timestamps are correct. SQL and outputCREATE TABLE product_units AS
SELECT
*,
CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product
WHERE product_qty_type = 'unit';Query 10: ✅ Correct One product with a current timestamp was inserted. SQL and outputINSERT INTO product_units
SELECT
*,
CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product
WHERE product_name = 'Apple Pie'
LIMIT 1;Query 11: ✅ Correct Only the older copy was removed; the inserted copy remains. SQL and outputDELETE FROM product_units
WHERE rowid = (
SELECT rowid
FROM product_units
WHERE product_name = 'Apple Pie'
ORDER BY snapshot_timestamp ASC, rowid ASC
LIMIT 1
);Query 12: ❌ Error The SQL could not be executed or assessed. ALTER TABLE product_units
ADD current_quantity INT;
UPDATE product_units |
What changes are you trying to make? (e.g. Adding or removing code, refactoring existing code, adding reports)
adding code
What did you learn from the changes you have made?
practice different sql queries
Was there another approach you were thinking about making? If so, what approach(es) were you thinking of?
NA
Were there any challenges? If so, what issue(s) did you face? How did you overcome it?
the final question is a bit difficult
How were these changes tested?
NA
A reference to a related issue in your repository (if applicable)
NA
Checklist