Repository navigation
Assignment two - #2
tabassumaniqa wants to merge 7 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\tCOALESCE(product_size, '') || ' (' || \n\tCOALESCE(product_qty_type, 'unit') || ')'": "Habanero Peppers - Organic, medium (lbs)"
},
{
"product_name || ', ' || \n\tCOALESCE(product_size, '') || ' (' || \n\tCOALESCE(product_qty_type, 'unit') || ')'": "Jalapeno Peppers - Organic, small (lbs)"
},
{
"product_name || ', ' || \n\tCOALESCE(product_size, '') || ' (' || \n\tCOALESCE(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 ASC
) AS visit_number
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 DISTINCT
customer_id,
market_date,
DENSE_RANK() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC
) AS visit_number
FROM customer_purchases
)
WHERE visit_number = 1;[
{
"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 *,
COUNT(*) OVER (
PARTITION BY customer_id, product_id
) AS purchase_count
FROM customer_purchases
WHERE market_date < '2022-04-29';[
{
"product_id": 4,
"vendor_id": 7,
"market_date": "2022-04-19",
"customer_id": 1,
"quantity": 5,
"cost_per_quantity": 3.5,
"transaction_time": "16:09:00",
"purchase_count": 2
},
{
"product_id": 4,
"vendor_id": 7,
"market_date": "2022-04-26",
"customer_id": 1,
"quantity": 3,
"cost_per_quantity": 4,
"transaction_time": "18:22:00",
"purchase_count": 2
},
{
"product_id": 5,
"vendor_id": 8,
"market_date": "2022-04-08",
"customer_id": 1,
"quantity": 1,
"cost_per_quantity": 6.5,
"transaction_time": "13:10:00",
"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 *
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 day_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_day,
RANK() OVER (ORDER BY total_sales ASC) AS worst_day
FROM day_sales
)
SELECT market_date, total_sales
FROM ranked_sales
WHERE best_day = 1
UNION
SELECT market_date, total_sales
FROM ranked_sales
WHERE worst_day = 1;[
{
"market_date": "2022-12-20",
"total_sales": 979
},
{
"market_date": "2023-04-07",
"total_sales": 272.5
}
]Query 8: ✅ Correct Possible revenue is correct for every vendor and product. SQL and outputSELECT
v.vendor_name,
p.product_name,
COUNT(*) * 5 * vinv.original_price AS total_revenue
FROM (
SELECT DISTINCT
vendor_id,
product_id,
original_price
FROM vendor_inventory
) AS vinv
INNER JOIN vendor as v
ON vinv.vendor_id = v.vendor_id
INNER JOIN product AS p
ON vinv.product_id = p.product_id
CROSS JOIN customer as c
GROUP BY
v.vendor_id,
p.product_id,
vinv.original_price;[
{
"vendor_name": "Fields of Corn",
"product_name": "Sweet Corn",
"total_revenue": 65
},
{
"vendor_name": "Marco's Peppers",
"product_name": "Habanero Peppers - Organic",
"total_revenue": 908.7
},
{
"vendor_name": "Marco's Peppers",
"product_name": "Jalapeno Peppers - Organic",
"total_revenue": 453.70000000000005
}
]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';
/*
to check new table:
SELECT *
FROM product_units;
/*Query 10: ✅ Correct One product with a current timestamp was inserted. SQL and outputINSERT INTO product_units
SELECT
product.*,
CURRENT_TIMESTAMP
FROM product
WHERE product_name = 'Apple Pie';
SELECT *
FROM product_units
WHERE product_name = 'Apple Pie';[
{
"product_id": 7,
"product_name": "Apple Pie",
"product_size": "10\"",
"product_category_id": 3,
"product_qty_type": "unit",
"snapshot_timestamp": "2026-11-09 01:14:00"
},
{
"product_id": 7,
"product_name": "Apple Pie",
"product_size": "10\"",
"product_category_id": 3,
"product_qty_type": "unit",
"snapshot_timestamp": "2026-11-09 01:14:01"
}
]Query 11: ✅ Correct Only the older copy was removed; the inserted copy remains. SQL and outputDELETE FROM product_units
WHERE product_name = 'Apple Pie'
AND snapshot_timestamp = (
SELECT MIN(snapshot_timestamp)
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: ✅ Correct All rows remain and current_quantity matches the latest inventory. SQL and outputALTER TABLE product_units
ADD current_quantity INT;
UPDATE product_units
SET current_quantity = COALESCE((
SELECT vinv.quantity
FROM vendor_inventory AS vinv
WHERE vinv.product_id = product_units.product_id
ORDER BY vinv.market_date DESC
LIMIT 1
), 0);
/*
-- check query
SELECT
product_id,
product_name,
current_quantity
FROM product_units;
/* |
What changes are you trying to make? (e.g. Adding or removing code, refactoring existing code, adding reports)
Adding assignment 2 responses and queries
What did you learn from the changes you have made?
More advanced SQL queries
Was there another approach you were thinking about making? If so, what approach(es) were you thinking of?
no
Were there any challenges? If so, what issue(s) did you face? How did you overcome it?
It was a lot to learn over a short period of time, I had to split the learning into smaller sessions and practice newly learned queries using the self assessments to ensure i retained the info
How were these changes tested?
Made sure all the queries run without errors on DB browser for SQLite
A reference to a related issue in your repository (if applicable)
n/a
Checklist