Repository navigation
Complete SQL assignment 2 - #2
mohammadgoudarzirad 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 || ', ' ||\nCOALESCE(product_size, '') ||\n' (' ||\nCOALESCE(product_qty_type, 'unit') ||\n')'": "Habanero Peppers - Organic, medium (lbs)"
},
{
"product_name || ', ' ||\nCOALESCE(product_size, '') ||\n' (' ||\nCOALESCE(product_qty_type, 'unit') ||\n')'": "Jalapeno Peppers - Organic, small (lbs)"
},
{
"product_name || ', ' ||\nCOALESCE(product_size, '') ||\n' (' ||\nCOALESCE(product_qty_type, 'unit') ||\n')'": "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: ✅ Correct Only each customer's most recent visit is returned. SQL and outputSELECT DISTINCT
customer_id,
market_date,
visit_number
FROM (
SELECT
customer_id,
market_date,
DENSE_RANK() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC
) AS visit_number
FROM customer_purchases
) AS ranked_visits
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
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, market_date;[
{
"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,
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
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
market_date,
total_sales
FROM ranked_sales
WHERE best_rank = 1
UNION
SELECT
market_date,
total_sales
FROM ranked_sales
WHERE worst_rank = 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,
SUM(5 * vi.original_price) AS potential_revenue
FROM (
SELECT DISTINCT
vendor_id,
product_id,
original_price
FROM vendor_inventory
) AS vi
JOIN vendor AS v
ON vi.vendor_id = v.vendor_id
JOIN product AS p
ON vi.product_id = p.product_id
CROSS JOIN customer AS c
GROUP BY
v.vendor_name,
p.product_name;[
{
"vendor_name": "Annie's Pies",
"product_name": "Apple Pie",
"potential_revenue": 2340
},
{
"vendor_name": "Annie's Pies",
"product_name": "Cherry Pie",
"potential_revenue": 2340
},
{
"vendor_name": "Annie's Pies",
"product_name": "Whole Wheat Bread",
"potential_revenue": 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 outputSELECT *
FROM product_units
WHERE product_name = 'Apple Pie';
INSERT INTO product_units (
product_id,
product_name,
product_size,
product_category_id,
product_qty_type,
snapshot_timestamp
)
VALUES (
7,
'Apple Pie',
'10"',
3,
'unit',
CURRENT_TIMESTAMP
);
SELECT *
FROM product_units
WHERE product_name = 'Apple Pie'
ORDER BY snapshot_timestamp;[
{
"product_id": 7,
"product_name": "Apple Pie",
"product_size": "10\"",
"product_category_id": 3,
"product_qty_type": "unit",
"snapshot_timestamp": "2026-11-07 02:39:54"
},
{
"product_id": 7,
"product_name": "Apple Pie",
"product_size": "10\"",
"product_category_id": 3,
"product_qty_type": "unit",
"snapshot_timestamp": "2026-11-07 02:39:55"
}
]Query 11: ✅ Correct Only the older copy was removed; the inserted copy remains. SQL and outputDELETE FROM product_units
WHERE product_id = 7
AND snapshot_timestamp = (
SELECT MIN(snapshot_timestamp)
FROM product_units
WHERE product_id = 7
);Query 12: ❌ Error The SQL could not be executed or assessed. SELECT
pu.product_id,
pu.product_name,
COALESCE(
(
SELECT vi.quantity
FROM vendor_inventory AS vi
WHERE vi.product_id = pu.product_id
ORDER BY vi.market_date DESC
LIMIT 1
),
0
) AS latest_quantity
FROM product_units AS pu;
UPDATE product_units
SET current_quantity = COALESCE(
(
SELECT vi.quantity
FROM vendor_inventory AS vi
WHERE vi.product_id = product_units.product_id
ORDER BY vi.market_date DESC
LIMIT 1
),
0
);
SELECT
product_id,
product_name,
current_quantity
FROM product_units; |
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, '') ||\n' (' ||\nCOALESCE(product_qty_type, 'unit') ||\n')'": "Habanero Peppers - Organic, medium (lbs)"
},
{
"product_name || ', ' ||\nCOALESCE(product_size, '') ||\n' (' ||\nCOALESCE(product_qty_type, 'unit') ||\n')'": "Jalapeno Peppers - Organic, small (lbs)"
},
{
"product_name || ', ' ||\nCOALESCE(product_size, '') ||\n' (' ||\nCOALESCE(product_qty_type, 'unit') ||\n')'": "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: ✅ Correct Only each customer's most recent visit is returned. SQL and outputSELECT DISTINCT
customer_id,
market_date,
visit_number
FROM (
SELECT
customer_id,
market_date,
DENSE_RANK() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC
) AS visit_number
FROM customer_purchases
) AS ranked_visits
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
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, market_date;[
{
"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,
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
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
market_date,
total_sales
FROM ranked_sales
WHERE best_rank = 1
UNION
SELECT
market_date,
total_sales
FROM ranked_sales
WHERE worst_rank = 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,
SUM(5 * vi.original_price) AS potential_revenue
FROM (
SELECT DISTINCT
vendor_id,
product_id,
original_price
FROM vendor_inventory
) AS vi
JOIN vendor AS v
ON vi.vendor_id = v.vendor_id
JOIN product AS p
ON vi.product_id = p.product_id
CROSS JOIN customer AS c
GROUP BY
v.vendor_name,
p.product_name;[
{
"vendor_name": "Annie's Pies",
"product_name": "Apple Pie",
"potential_revenue": 2340
},
{
"vendor_name": "Annie's Pies",
"product_name": "Cherry Pie",
"potential_revenue": 2340
},
{
"vendor_name": "Annie's Pies",
"product_name": "Whole Wheat Bread",
"potential_revenue": 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 outputSELECT *
FROM product_units
WHERE product_name = 'Apple Pie';
INSERT INTO product_units (
product_id,
product_name,
product_size,
product_category_id,
product_qty_type,
snapshot_timestamp
)
VALUES (
7,
'Apple Pie',
'10"',
3,
'unit',
CURRENT_TIMESTAMP
);
SELECT *
FROM product_units
WHERE product_name = 'Apple Pie'
ORDER BY snapshot_timestamp;[
{
"product_id": 7,
"product_name": "Apple Pie",
"product_size": "10\"",
"product_category_id": 3,
"product_qty_type": "unit",
"snapshot_timestamp": "2026-11-07 02:52:01"
},
{
"product_id": 7,
"product_name": "Apple Pie",
"product_size": "10\"",
"product_category_id": 3,
"product_qty_type": "unit",
"snapshot_timestamp": "2026-11-07 02:52:02"
}
]Query 11: ✅ Correct Only the older copy was removed; the inserted copy remains. SQL and outputDELETE FROM product_units
WHERE product_id = 7
AND snapshot_timestamp = (
SELECT MIN(snapshot_timestamp)
FROM product_units
WHERE product_id = 7
);Query 12: ❌ Error The SQL could not be executed or assessed. SELECT
ALTER TABLE product_units
ADD current_quantity INT;
UPDATE product_units
SET current_quantity = COALESCE(
(
SELECT vi.quantity
FROM vendor_inventory AS vi
WHERE vi.product_id = product_units.product_id
ORDER BY vi.market_date DESC
LIMIT 1
),
0
); |
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, '') ||\n' (' ||\nCOALESCE(product_qty_type, 'unit') ||\n')'": "Habanero Peppers - Organic, medium (lbs)"
},
{
"product_name || ', ' ||\nCOALESCE(product_size, '') ||\n' (' ||\nCOALESCE(product_qty_type, 'unit') ||\n')'": "Jalapeno Peppers - Organic, small (lbs)"
},
{
"product_name || ', ' ||\nCOALESCE(product_size, '') ||\n' (' ||\nCOALESCE(product_qty_type, 'unit') ||\n')'": "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: ✅ Correct Only each customer's most recent visit is returned. SQL and outputSELECT DISTINCT
customer_id,
market_date,
visit_number
FROM (
SELECT
customer_id,
market_date,
DENSE_RANK() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC
) AS visit_number
FROM customer_purchases
) AS ranked_visits
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
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, market_date;[
{
"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,
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
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
market_date,
total_sales
FROM ranked_sales
WHERE best_rank = 1
UNION
SELECT
market_date,
total_sales
FROM ranked_sales
WHERE worst_rank = 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,
SUM(5 * vi.original_price) AS potential_revenue
FROM (
SELECT DISTINCT
vendor_id,
product_id,
original_price
FROM vendor_inventory
) AS vi
JOIN vendor AS v
ON vi.vendor_id = v.vendor_id
JOIN product AS p
ON vi.product_id = p.product_id
CROSS JOIN customer AS c
GROUP BY
v.vendor_name,
p.product_name;[
{
"vendor_name": "Annie's Pies",
"product_name": "Apple Pie",
"potential_revenue": 2340
},
{
"vendor_name": "Annie's Pies",
"product_name": "Cherry Pie",
"potential_revenue": 2340
},
{
"vendor_name": "Annie's Pies",
"product_name": "Whole Wheat Bread",
"potential_revenue": 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 outputSELECT *
FROM product_units
WHERE product_name = 'Apple Pie';
INSERT INTO product_units (
product_id,
product_name,
product_size,
product_category_id,
product_qty_type,
snapshot_timestamp
)
VALUES (
7,
'Apple Pie',
'10"',
3,
'unit',
CURRENT_TIMESTAMP
);
SELECT *
FROM product_units
WHERE product_name = 'Apple Pie'
ORDER BY snapshot_timestamp;[
{
"product_id": 7,
"product_name": "Apple Pie",
"product_size": "10\"",
"product_category_id": 3,
"product_qty_type": "unit",
"snapshot_timestamp": "2026-11-07 02:56:20"
},
{
"product_id": 7,
"product_name": "Apple Pie",
"product_size": "10\"",
"product_category_id": 3,
"product_qty_type": "unit",
"snapshot_timestamp": "2026-11-07 02:56:21"
}
]Query 11: ✅ Correct Only the older copy was removed; the inserted copy remains. SQL and outputDELETE FROM product_units
WHERE product_id = 7
AND snapshot_timestamp = (
SELECT MIN(snapshot_timestamp)
FROM product_units
WHERE product_id = 7
);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 vi.quantity
FROM vendor_inventory AS 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)
Assignment 2 SQL queries
What did you learn from the changes you have made?
SQL queries, joins, window functions, and updating tables
Was there another approach you were thinking about making? If so, what approach(es) were you thinking of?
N/A
Were there any challenges? If so, what issue(s) did you face? How did you overcome it?
I tested them step by step and fixed errors
How were these changes tested?
I ran the SQL queries in DB Browser for SQLite and checked the results
A reference to a related issue in your repository (if applicable)
N/A
Checklist