Skip to content

UofT-DSI | SQL - Assignment 2 - #2

Open
mouweili wants to merge 2 commits into
mainfrom
assignment-two
Open

mouweili wants to merge 2 commits into
mainfrom
assignment-two

Conversation

@mouweili

@mouweili mouweili commented Nov 6, 2026

Copy link
Copy Markdown
Owner

What changes are you trying to make?
I completed Assignment 2: designed a bookstore logical model (with and without employee shifts), answered the Type 1 vs Type 2 slowly changing dimension prompt, wrote the SQL for Sections 2 and 3 in assignment2.sql, and wrote the Section 4 ethics response in Assignment2.md. This branch was created from assignment-one, so the Assignment 1 files also appear in the diff.

What did you learn from the changes you have made?
I learned how to use COALESCE, window functions, string functions, UNION, CROSS JOIN, and INSERT/DELETE/UPDATE, and how to design a model with a date table and slowly changing dimensions.

Was there another approach you were thinking about making?
For the most recent visit query, I considered ROW_NUMBER versus DENSE_RANK. I used DENSE_RANK so multiple purchases on the same market date share the same visit number.

Were there any challenges? If so, what issue(s) did you face? How did you overcome it?
Keeping each query separate and handling tables that already existed when re-running statements. I fixed these by testing queries one at a time.

How were these changes tested?
I ran each query in DB Browser for SQLite and checked row counts and column values.

Checklist
[x] I can confirm that my changes are working as intended

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

SQL Assignment 2 feedback

SQL checks only. ERDs, written responses, and rubric point totals require instructor marking.

02_activities/assignments/DC_Cohort/assignment2.sql

Query 1: ✅ Correct

All product descriptions and NULL replacements are correct.

SQL and output
SELECT
product_name || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')'
FROM product;
[
  {
    "product_name || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')'": "Habanero Peppers - Organic, medium (lbs)"
  },
  {
    "product_name || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')'": "Jalapeno Peppers - Organic, small (lbs)"
  },
  {
    "product_name || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')'": "Poblano Peppers - Organic, large (unit)"
  }
]

Query 2: ✅ Correct

Visit dates are numbered correctly for each customer.

SQL and output
SELECT *,
  DENSE_RANK() OVER (PARTITION BY customer_id ORDER BY market_date) AS visit_number
FROM customer_purchases
WHERE market_date < '2022-04-29';
[
  {
    "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",
    "visit_number": 1
  },
  {
    "product_id": 5,
    "vendor_id": 8,
    "market_date": "2022-04-15",
    "customer_id": 1,
    "quantity": 1,
    "cost_per_quantity": 6.5,
    "transaction_time": "9:54:00",
    "visit_number": 2
  },
  {
    "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",
    "visit_number": 3
  }
]

Query 3: ✅ Correct

Only each customer's most recent visit is returned.

SQL and output
SELECT *
FROM (
  SELECT *,
    DENSE_RANK() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS visit_number
  FROM customer_purchases
) AS t
WHERE visit_number = 1;
[
  {
    "product_id": 4,
    "vendor_id": 7,
    "market_date": "2023-10-13",
    "customer_id": 1,
    "quantity": 5,
    "cost_per_quantity": 3.5,
    "transaction_time": "9:06:00",
    "visit_number": 1
  },
  {
    "product_id": 4,
    "vendor_id": 7,
    "market_date": "2023-10-13",
    "customer_id": 1,
    "quantity": 1,
    "cost_per_quantity": 3.5,
    "transaction_time": "9:07:00",
    "visit_number": 1
  },
  {
    "product_id": 5,
    "vendor_id": 8,
    "market_date": "2023-10-13",
    "customer_id": 1,
    "quantity": 4,
    "cost_per_quantity": 6.5,
    "transaction_time": "9:07:00",
    "visit_number": 1
  }
]

Query 4: ✅ Correct

The purchase count is correct for every customer and product.

SQL and output
SELECT *,
  COUNT(*) OVER (PARTITION BY customer_id, product_id ORDER BY market_date) AS times_purchased
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",
    "times_purchased": 1
  },
  {
    "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",
    "times_purchased": 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",
    "times_purchased": 1
  }
]

Query 5: ✅ Correct

Product descriptions are extracted and trimmed correctly.

SQL and output
SELECT product_name,
  CASE WHEN INSTR(product_name, '-') > 0
       THEN TRIM(SUBSTR(product_name, INSTR(product_name, '-') + 1))
  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 output
SELECT product_name, product_size,
  CASE WHEN INSTR(product_name, '-') > 0
       THEN TRIM(SUBSTR(product_name, INSTR(product_name, '-') + 1))
  END AS description
FROM product
WHERE product_size REGEXP '[0-9]';
[
  {
    "product_name": "Banana Peppers - Jar",
    "product_size": "8 oz",
    "description": "Jar"
  },
  {
    "product_name": "Whole Wheat Bread",
    "product_size": "1.5 lbs",
    "description": null
  },
  {
    "product_name": "Apple Pie",
    "product_size": "10\"",
    "description": null
  }
]

Query 7: ✅ Correct

The highest and lowest sales dates and totals are correct.

SQL and output
WITH daily AS (
  SELECT market_date, SUM(quantity * cost_per_quantity) AS total_sales
  FROM customer_purchases
  GROUP BY market_date
),
ranked 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
)
SELECT market_date, total_sales, 'best day' AS label FROM ranked WHERE best_rank = 1
UNION
SELECT market_date, total_sales, 'worst day' FROM ranked WHERE worst_rank = 1;
[
  {
    "market_date": "2022-12-20",
    "total_sales": 979,
    "label": "best day"
  },
  {
    "market_date": "2023-04-07",
    "total_sales": 272.5,
    "label": "worst day"
  }
]

Query 8: ✅ Correct

Possible revenue is correct for every vendor and product.

SQL and output
SELECT vendor_name, product_name, SUM(5 * original_price) AS potential_revenue
FROM (
  SELECT DISTINCT v.vendor_name, p.product_name, vi.original_price
  FROM vendor_inventory AS vi
  INNER JOIN vendor AS v ON vi.vendor_id = v.vendor_id
  INNER JOIN product AS p ON vi.product_id = p.product_id
) AS vp
CROSS JOIN customer
GROUP BY vendor_name, 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 output
CREATE 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 output
INSERT INTO product_units
SELECT *, CURRENT_TIMESTAMP
FROM product
WHERE product_name = 'Apple Pie';

Query 11: ✅ Correct

Only the older copy was removed; the inserted copy remains.

SQL and output
DELETE FROM product_units
WHERE product_name = 'Apple Pie'
  AND snapshot_timestamp = (
    SELECT MIN(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 output
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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant