Repository navigation
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 || ', ' ||\nCOALESCE(product_size, '') || ' (' ||\nCOALESCE(product_qty_type, 'unit') || ')'": "Habanero Peppers - Organic, medium (lbs)"
},
{
"product_name || ', ' ||\nCOALESCE(product_size, '') || ' (' ||\nCOALESCE(product_qty_type, 'unit') || ')'": "Jalapeno Peppers - Organic, small (lbs)"
},
{
"product_name || ', ' ||\nCOALESCE(product_size, '') || ' (' ||\nCOALESCE(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,
DENSE_RANK() OVER (
PARTITION BY customer_id
ORDER BY market_date) AS visit_number
FROM customer_purchases
WHERE market_date < '2022-04-29'
ORDER BY customer_id, market_date;[
{
"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: ❌ Incorrect The result does not contain only each customer's most recent visit. SELECT
customer_id,
market_date,
DENSE_RANK() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC) AS visit_number
FROM customer_purchases;[
{
"customer_id": 1,
"market_date": "2023-10-13",
"visit_number": 1
},
{
"customer_id": 1,
"market_date": "2023-10-13",
"visit_number": 1
},
{
"customer_id": 1,
"market_date": "2023-10-13",
"visit_number": 1
}
]Query 4: ✅ Correct The purchase count is correct for every customer and product. SQL and outputSELECT
customer_id,
market_date,
product_id,
quantity,
COUNT(*) OVER (
PARTITION BY customer_id, product_id) AS times_purchased
FROM customer_purchases
WHERE market_date < '2022-04-29'
ORDER BY customer_id, product_id, market_date;[
{
"customer_id": 1,
"market_date": "2022-04-19",
"product_id": 4,
"quantity": 5,
"times_purchased": 2
},
{
"customer_id": 1,
"market_date": "2022-04-26",
"product_id": 4,
"quantity": 3,
"times_purchased": 2
},
{
"customer_id": 1,
"market_date": "2022-04-08",
"product_id": 5,
"quantity": 1,
"times_purchased": 6
}
]Query 5: ✅ Correct Product descriptions are extracted and trimmed correctly. SQL and outputSELECT
product_name,
CASE
WHEN INSTR(product_name, '-') > 0
THEN TRIM(SUBSTR(product_name, INSTR(product_name, '-') + 1))
ELSE NULL
END 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 *
FROM product
WHERE product_size REGEXP '[0-9]';[
{
"product_id": 4,
"product_name": "Banana Peppers - Jar",
"product_size": "8 oz",
"product_category_id": 3,
"product_qty_type": "unit"
},
{
"product_id": 5,
"product_name": "Whole Wheat Bread",
"product_size": "1.5 lbs",
"product_category_id": 3,
"product_qty_type": "unit"
},
{
"product_id": 7,
"product_name": "Apple Pie",
"product_size": "10\"",
"product_category_id": 3,
"product_qty_type": "unit"
}
]Query 7: ✅ Correct The highest and lowest sales dates and totals are correct. SQL and outputWITH sales_by_date 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 sales_by_date
)
SELECT
market_date,
total_sales,
'Highest Sales' AS sales_day
FROM ranked_sales
WHERE best_rank = 1
UNION
SELECT
market_date,
total_sales,
'Lowest Sales' AS sales_day
FROM ranked_sales
WHERE worst_rank = 1;[
{
"market_date": "2022-12-20",
"total_sales": 979,
"sales_day": "Highest Sales"
},
{
"market_date": "2023-04-07",
"total_sales": 272.5,
"sales_day": "Lowest Sales"
}
]Query 8: ❌ Incorrect The possible revenue does not match five items per customer. SELECT
v.vendor_name,
p.product_name,
COUNT(*) * 5 * vi.original_price AS total_revenue
FROM vendor_inventory vi
CROSS JOIN customer c
JOIN vendor v
ON vi.vendor_id = v.vendor_id
JOIN product p
ON vi.product_id = p.product_id
GROUP BY
v.vendor_name,
p.product_name,
vi.original_price
ORDER BY
v.vendor_name,
p.product_name;[
{
"vendor_name": "Annie's Pies",
"product_name": "Apple Pie",
"total_revenue": 332280
},
{
"vendor_name": "Annie's Pies",
"product_name": "Cherry Pie",
"total_revenue": 332280
},
{
"vendor_name": "Annie's Pies",
"product_name": "Whole Wheat Bread",
"total_revenue": 119990
}
]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: ❌ Incorrect Check the inserted product fields and updated timestamp. INSERT INTO product_units (
product_id,
product_name,
product_size,
product_qty_type,
snapshot_timestamp
)
VALUES (
999,
'Apple Pie',
'1',
'unit',
CURRENT_TIMESTAMP);Query 11: ⚪ Cannot assess: prerequisite failed Question 10 must insert a valid product first. DELETE FROM product_units
WHERE product_name = 'Apple Pie'
AND snapshot_timestamp <> (
SELECT MAX(snapshot_timestamp)
FROM product_units
WHERE product_name = 'Apple Pie');Query 12: ⚪ Cannot assess: prerequisite failed Question 11 must preserve the intended product rows first. ALTER TABLE product_units
ADD current_quantity INT;
UPDATE product_units
SET current_quantity =
COALESCE(
(SELECT vi.quantity
FROM vendor_inventory vi
WHERE vi.product_id = product_units.product_id
ORDER BY vi.market_date DESC
LIMIT 1),0); |
What changes are you trying to make? (e.g. Adding or removing code, refactoring existing code, adding reports)
What did you learn from the changes you have made?
Was there another approach you were thinking about making? If so, what approach(es) were you thinking of?
Were there any challenges? If so, what issue(s) did you face? How did you overcome it?
How were these changes tested?
A reference to a related issue in your repository (if applicable)
Checklist