Skip to content

Integration test: arbitrary SQL column names and reviewer fixes - #1

Open
danielrazavi wants to merge 22 commits into
mainfrom
assignment-two
Open

danielrazavi wants to merge 22 commits into
mainfrom
assignment-two

Conversation

@danielrazavi

@danielrazavi danielrazavi commented Oct 9, 2026 •

Copy link
Copy Markdown
Owner

Test submission for the fixes in course PR #484, based on Sergii's and Niyaz's feedback. For testing only; don't merge these answers into the course repo.

Results

Test What happened
Rename and reorder every output column in Q1-8 All 12 answers passed. Feedback · Run
Deliberately attach Q2's dates to the wrong rows Only Q2 failed, as expected. Feedback · Run
All answers deliberately wrong Q1-9 incorrect; Q10-12 blocked by Q9. Feedback · Run

The current submission is intentionally all wrong. Q1-9 should be incorrect; Q10-12 should be blocked because Q9 did not create a valid starting table. Incorrect answers stay expanded; correct answers in the earlier runs are collapsed. Each push automatically regrades the submission.

Other Cases Checked

  • Ordinary -- query comments.
  • Q3 uses ranking without displaying it.
  • Q4 leaves out vendor/time columns and counts by date. Earlier test covers total counts too.
  • Q8 renames revenue and puts extra columns after it.
  • Q9 creates a temporary table that Q10-12 successfully use.

@danielrazavi danielrazavi reopened this Oct 9, 2026
@github-actions

github-actions Bot commented Oct 9, 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.

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

Query 2: ✅ Correct

Visit dates are numbered correctly for each customer.

SELECT 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';
[
  {
    "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, 99 AS visit_number FROM customer_purchases;
[
  {
    "customer_id": 14,
    "market_date": "2022-07-05",
    "visit_number": 99
  },
  {
    "customer_id": 14,
    "market_date": "2022-07-05",
    "visit_number": 99
  },
  {
    "customer_id": 15,
    "market_date": "2022-07-05",
    "visit_number": 99
  }
]

Query 4: ✅ Correct

The purchase count is correct for every customer and product.

SELECT cp.*, COUNT(*) OVER (PARTITION BY customer_id, product_id) AS product_purchase_count FROM customer_purchases cp 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",
    "product_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",
    "product_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",
    "product_purchase_count": 6
  }
]

Query 5: ❌ Incorrect

The descriptions do not match the text after each product-name hyphen.

SELECT product_name, NULL AS product_description FROM product;
[
  {
    "product_name": "Habanero Peppers - Organic",
    "product_description": null
  },
  {
    "product_name": "Jalapeno Peppers - Organic",
    "product_description": null
  },
  {
    "product_name": "Poblano Peppers - Organic",
    "product_description": null
  }
]

Query 6: ✅ Correct

Only product sizes containing a number are returned.

SELECT product_size FROM product WHERE product_size REGEXP '[0-9]';
[
  {
    "product_size": "8 oz"
  },
  {
    "product_size": "1.5 lbs"
  },
  {
    "product_size": "10\""
  }
]

Query 7: ❌ Incorrect

The result does not match the highest and lowest sales dates.

SELECT market_date, 0 AS total_sales FROM customer_purchases GROUP BY market_date LIMIT 2;
[
  {
    "market_date": "2022-04-05",
    "total_sales": 0
  },
  {
    "market_date": "2022-04-08",
    "total_sales": 0
  }
]

Query 8: ❌ Incorrect

The possible revenue does not match five items per customer.

SELECT vendor_name, product_name, 0 AS possible_revenue FROM vendor CROSS JOIN product;
[
  {
    "vendor_name": "Annie's Pies",
    "product_name": "Habanero Peppers - Organic",
    "possible_revenue": 0
  },
  {
    "vendor_name": "Annie's Pies",
    "product_name": "Jalapeno Peppers - Organic",
    "possible_revenue": 0
  },
  {
    "vendor_name": "Annie's Pies",
    "product_name": "Poblano Peppers - Organic",
    "possible_revenue": 0
  }
]

Query 9: ✅ Correct

Unit products and current snapshot timestamps are correct.

CREATE TABLE product_units AS SELECT product.*, CURRENT_TIMESTAMP AS snapshot_timestamp FROM product WHERE product_qty_type = 'unit';

Query 10: ✅ Correct

One product with a current timestamp was inserted.

INSERT INTO product_units SELECT product.*, CURRENT_TIMESTAMP AS snapshot_timestamp FROM product WHERE product_id = (SELECT MIN(product_id) FROM product WHERE product_qty_type = 'unit');

Query 11: ✅ Correct

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

DELETE FROM product_units WHERE product_id = (SELECT MIN(product_id) FROM product WHERE product_qty_type = 'unit') AND snapshot_timestamp = (SELECT MIN(snapshot_timestamp) FROM product_units WHERE product_id = (SELECT MIN(product_id) FROM product WHERE product_qty_type = 'unit'));

Query 12: ✅ Correct

All rows remain and current_quantity matches the latest inventory.

ALTER TABLE product_units ADD current_quantity INT;
UPDATE product_units SET current_quantity = COALESCE((SELECT quantity FROM vendor_inventory WHERE vendor_inventory.product_id = product_units.product_id ORDER BY market_date DESC LIMIT 1), 0);

@github-actions

github-actions Bot commented Oct 9, 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.

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

Query 2: ✅ Correct

Visit dates are numbered correctly for each customer.

SELECT 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';
[
  {
    "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, 99 AS visit_number FROM customer_purchases;
[
  {
    "customer_id": 14,
    "market_date": "2022-07-05",
    "visit_number": 99
  },
  {
    "customer_id": 14,
    "market_date": "2022-07-05",
    "visit_number": 99
  },
  {
    "customer_id": 15,
    "market_date": "2022-07-05",
    "visit_number": 99
  }
]

Query 4: ✅ Correct

The purchase count is correct for every customer and product.

SELECT cp.*, COUNT(*) OVER (PARTITION BY customer_id, product_id) AS product_purchase_count FROM customer_purchases cp 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",
    "product_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",
    "product_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",
    "product_purchase_count": 6
  }
]

Query 5: ✅ Correct

Product descriptions are extracted and trimmed correctly.

SELECT product_name,
       CASE WHEN INSTR(product_name, '-') > 0
            THEN TRIM(SUBSTR(product_name, INSTR(product_name, '-') + 1))
            ELSE NULL END AS product_description
FROM product;
[
  {
    "product_name": "Habanero Peppers - Organic",
    "product_description": "Organic"
  },
  {
    "product_name": "Jalapeno Peppers - Organic",
    "product_description": "Organic"
  },
  {
    "product_name": "Poblano Peppers - Organic",
    "product_description": "Organic"
  }
]

Query 6: ✅ Correct

Only product sizes containing a number are returned.

SELECT product_size FROM product WHERE product_size REGEXP '[0-9]';
[
  {
    "product_size": "8 oz"
  },
  {
    "product_size": "1.5 lbs"
  },
  {
    "product_size": "10\""
  }
]

Query 7: ❌ Incorrect

The result does not match the highest and lowest sales dates.

SELECT market_date, 0 AS total_sales FROM customer_purchases GROUP BY market_date LIMIT 2;
[
  {
    "market_date": "2022-04-05",
    "total_sales": 0
  },
  {
    "market_date": "2022-04-08",
    "total_sales": 0
  }
]

Query 8: ❌ Incorrect

The possible revenue does not match five items per customer.

SELECT vendor_name, product_name, 0 AS possible_revenue FROM vendor CROSS JOIN product;
[
  {
    "vendor_name": "Annie's Pies",
    "product_name": "Habanero Peppers - Organic",
    "possible_revenue": 0
  },
  {
    "vendor_name": "Annie's Pies",
    "product_name": "Jalapeno Peppers - Organic",
    "possible_revenue": 0
  },
  {
    "vendor_name": "Annie's Pies",
    "product_name": "Poblano Peppers - Organic",
    "possible_revenue": 0
  }
]

Query 9: ✅ Correct

Unit products and current snapshot timestamps are correct.

CREATE TABLE product_units AS SELECT product.*, CURRENT_TIMESTAMP AS snapshot_timestamp FROM product WHERE product_qty_type = 'unit';

Query 10: ✅ Correct

One product with a current timestamp was inserted.

INSERT INTO product_units SELECT product.*, CURRENT_TIMESTAMP AS snapshot_timestamp FROM product WHERE product_id = (SELECT MIN(product_id) FROM product WHERE product_qty_type = 'unit');

Query 11: ✅ Correct

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

DELETE FROM product_units WHERE product_id = (SELECT MIN(product_id) FROM product WHERE product_qty_type = 'unit') AND snapshot_timestamp = (SELECT MIN(snapshot_timestamp) FROM product_units WHERE product_id = (SELECT MIN(product_id) FROM product WHERE product_qty_type = 'unit'));

Query 12: ✅ Correct

All rows remain and current_quantity matches the latest inventory.

ALTER TABLE product_units ADD current_quantity INT;
UPDATE product_units SET current_quantity = COALESCE((SELECT quantity FROM vendor_inventory WHERE vendor_inventory.product_id = product_units.product_id ORDER BY market_date DESC LIMIT 1), 0);

@danielrazavi danielrazavi changed the title Test Assignment 2 automated feedback Test A2 reviewer fixes and collapsible feedback Oct 16, 2026
@github-actions

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: ❌ Incorrect

The formatted product descriptions do not match the expected results.

-- query for question 1: student answer
SELECT product_name FROM product; -- deliberately incorrect
[
  {
    "product_name": "Habanero Peppers - Organic"
  },
  {
    "product_name": "Jalapeno Peppers - Organic"
  },
  {
    "product_name": "Poblano Peppers - Organic"
  }
]

Query 2: ✅ Correct

Visit dates are numbered correctly for each customer.

SQL and output
-- query for question 2: student answer
SELECT customer_id, market_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date) AS visit_number

FROM customer_purchases
WHERE market_date < '2022-04-29'

GROUP BY customer_id, market_date
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 output
-- query for question 3: student answer
SELECT customer_id, market_date 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
    ) x WHERE visit_number = 1;
[
  {
    "customer_id": 1,
    "market_date": "2023-10-13"
  },
  {
    "customer_id": 2,
    "market_date": "2023-10-13"
  },
  {
    "customer_id": 3,
    "market_date": "2023-10-10"
  }
]

Query 4: ✅ Correct

The purchase count is correct for every customer and product.

SQL and output
-- query for question 4: student answer
SELECT customer_id, product_id, market_date,
        COUNT(*) OVER (PARTITION BY customer_id, product_id) AS cnt
        FROM customer_purchases WHERE market_date < '2022-04-29';
[
  {
    "customer_id": 1,
    "product_id": 4,
    "market_date": "2022-04-19",
    "cnt": 2
  },
  {
    "customer_id": 1,
    "product_id": 4,
    "market_date": "2022-04-26",
    "cnt": 2
  },
  {
    "customer_id": 1,
    "product_id": 5,
    "market_date": "2022-04-08",
    "cnt": 6
  }
]

Query 5: ✅ Correct

Product descriptions are extracted and trimmed correctly.

SQL and output
-- query for question 5: student answer
SELECT *, 
CASE WHEN INSTR(product_name,'-') > 0
  THEN LTRIM(RTRIM(SUBSTR(product_name,INSTR(product_name,'-')+1)))
  ELSE NULL END AS description

FROM product
[
  {
    "product_id": 1,
    "product_name": "Habanero Peppers - Organic",
    "product_size": "medium",
    "product_category_id": 1,
    "product_qty_type": "lbs",
    "description": "Organic"
  },
  {
    "product_id": 2,
    "product_name": "Jalapeno Peppers - Organic",
    "product_size": "small",
    "product_category_id": 1,
    "product_qty_type": "lbs",
    "description": "Organic"
  },
  {
    "product_id": 3,
    "product_name": "Poblano Peppers - Organic",
    "product_size": "large",
    "product_category_id": 1,
    "product_qty_type": "unit",
    "description": "Organic"
  }
]

Query 6: ✅ Correct

Only product sizes containing a number are returned.

SQL and output
-- query for question 6: student answer
SELECT *, 
CASE WHEN INSTR(product_name,'-') > 0
  THEN LTRIM(RTRIM(SUBSTR(product_name,INSTR(product_name,'-')+1)))
  ELSE NULL END AS description


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",
    "description": "Jar"
  },
  {
    "product_id": 5,
    "product_name": "Whole Wheat Bread",
    "product_size": "1.5 lbs",
    "product_category_id": 3,
    "product_qty_type": "unit",
    "description": null
  },
  {
    "product_id": 7,
    "product_name": "Apple Pie",
    "product_size": "10\"",
    "product_category_id": 3,
    "product_qty_type": "unit",
    "description": null
  }
]

Query 7: ✅ Correct

The highest and lowest sales dates and totals are correct.

SQL and output
-- query for question 7: student answer
;WITH sales_per_market AS (
SELECT
market_date,
ROUND(SUM(quantity * cost_per_quantity),2) AS sales

FROM customer_purchases
GROUP BY market_date
)

,market_dates_ranked_by_sales AS (
SELECT
market_date,
sales,
RANK() OVER (ORDER BY sales) AS sales_rank_asc,
RANK() OVER (ORDER BY sales DESC) AS sales_rank_desc

FROM sales_per_market
)
      
SELECT market_date, sales, sales_rank_desc AS sales_rank
FROM market_dates_ranked_by_sales
WHERE sales_rank_asc = 1

UNION

SELECT market_date, sales, sales_rank_desc AS sales_rank
FROM market_dates_ranked_by_sales
WHERE sales_rank_desc = 1
[
  {
    "market_date": "2022-12-20",
    "sales": 979,
    "sales_rank": 1
  },
  {
    "market_date": "2023-04-07",
    "sales": 272.5,
    "sales_rank": 142
  }
]

Query 8: ✅ Correct

Possible revenue is correct for every vendor and product.

SQL and output
-- query for question 8: student answer
SELECT vendor_name, "sum(price)" AS gross_income, product_name, 0 AS extra FROM (SELECT vendor_name, product_name, sum(price)
FROM (
  SELECT DISTINCT
  vendor_name,
  product_name,
  5 * original_price as price

  FROM vendor_inventory vi 
  JOIN vendor v on v.vendor_id = vi.vendor_id
  JOIN product p on p.product_id = vi.product_id
  ) x
CROSS JOIN 
  (SELECT DISTINCT 
  customer_id
  FROM customer)
GROUP BY vendor_name, product_name);
[
  {
    "vendor_name": "Annie's Pies",
    "gross_income": 2340,
    "product_name": "Apple Pie",
    "extra": 0
  },
  {
    "vendor_name": "Annie's Pies",
    "gross_income": 2340,
    "product_name": "Cherry Pie",
    "extra": 0
  },
  {
    "vendor_name": "Annie's Pies",
    "gross_income": 845,
    "product_name": "Whole Wheat Bread",
    "extra": 0
  }
]

Query 9: ✅ Correct

Unit products and current snapshot timestamps are correct.

SQL and output
-- query for question 9: student answer
CREATE TEMP TABLE product_units AS

 SELECT p.*,
   CURRENT_TIMESTAMP AS snapshot_timestamp
 FROM product AS p
 WHERE product_qty_type = "unit"

Query 10: ✅ Correct

One product with a current timestamp was inserted.

SQL and output
-- query for question 10: student answer
INSERT INTO product_units (product_id, product_name,
  product_size, product_category_id, product_qty_type, snapshot_timestamp)
      SELECT
          product_id,
          product_name,
          product_size,
          product_category_id,
          product_qty_type,
          CURRENT_TIMESTAMP
      FROM product AS p
      WHERE product_id = 7

Query 11: ✅ Correct

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

SQL and output
-- query for question 11: student answer
WITH older_record AS
(SELECT product_id
    ,MIN(snapshot_timestamp) AS snapshot_timestamp
    FROM product_units 
    WHERE product_id = 7)

DELETE FROM product_units
WHERE (product_id = 
  (SELECT  product_id 
  FROM older_record
    )
AND snapshot_timestamp =  
  (SELECT  snapshot_timestamp 
  FROM older_record
    ))

Query 12: ✅ Correct

All rows remain and current_quantity matches the latest inventory.

SQL and output
-- query for question 12: student answer
ALTER TABLE product_units
ADD current_quantity INT;

UPDATE product_units
SET current_quantity = (
  SELECT current_quantity 
  FROM (
    SELECT  p.product_id, COALESCE(quantity,0) as current_quantity

    FROM product_units p
    LEFT JOIN (
      SELECT *
      ,ROW_NUMBER() OVER( PARTITION BY vi.product_id ORDER BY market_date DESC) AS rn
      FROM vendor_inventory vi 
    ) vi ON p.product_id  = vi.product_id

    WHERE rn = 1 
    OR rn IS NULL
  ) p
WHERE product_units.product_id = p.product_id);

@github-actions

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: ❌ Incorrect

The formatted product descriptions do not match the expected results.

-- query for question 1: student answer
SELECT product_name FROM product; -- deliberately incorrect
[
  {
    "product_name": "Habanero Peppers - Organic"
  },
  {
    "product_name": "Jalapeno Peppers - Organic"
  },
  {
    "product_name": "Poblano Peppers - Organic"
  }
]

Query 2: ✅ Correct

Visit dates are numbered correctly for each customer.

SQL and output
-- query for question 2: student answer
SELECT customer_id, market_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date) AS visit_number

FROM customer_purchases
WHERE market_date < '2022-04-29'

GROUP BY customer_id, market_date
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 output
-- query for question 3: student answer
SELECT customer_id, market_date 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
    ) x WHERE visit_number = 1;
[
  {
    "customer_id": 1,
    "market_date": "2023-10-13"
  },
  {
    "customer_id": 2,
    "market_date": "2023-10-13"
  },
  {
    "customer_id": 3,
    "market_date": "2023-10-10"
  }
]

Query 4: ✅ Correct

The purchase count is correct for every customer and product.

SQL and output
-- query for question 4: student answer
SELECT customer_id, product_id, market_date,
        COUNT(*) OVER (PARTITION BY customer_id, product_id ORDER BY market_date) AS cnt
        FROM customer_purchases WHERE market_date < '2022-04-29';
[
  {
    "customer_id": 1,
    "product_id": 4,
    "market_date": "2022-04-19",
    "cnt": 1
  },
  {
    "customer_id": 1,
    "product_id": 4,
    "market_date": "2022-04-26",
    "cnt": 2
  },
  {
    "customer_id": 1,
    "product_id": 5,
    "market_date": "2022-04-08",
    "cnt": 1
  }
]

Query 5: ✅ Correct

Product descriptions are extracted and trimmed correctly.

SQL and output
-- query for question 5: student answer
SELECT *, 
CASE WHEN INSTR(product_name,'-') > 0
  THEN LTRIM(RTRIM(SUBSTR(product_name,INSTR(product_name,'-')+1)))
  ELSE NULL END AS description

FROM product
[
  {
    "product_id": 1,
    "product_name": "Habanero Peppers - Organic",
    "product_size": "medium",
    "product_category_id": 1,
    "product_qty_type": "lbs",
    "description": "Organic"
  },
  {
    "product_id": 2,
    "product_name": "Jalapeno Peppers - Organic",
    "product_size": "small",
    "product_category_id": 1,
    "product_qty_type": "lbs",
    "description": "Organic"
  },
  {
    "product_id": 3,
    "product_name": "Poblano Peppers - Organic",
    "product_size": "large",
    "product_category_id": 1,
    "product_qty_type": "unit",
    "description": "Organic"
  }
]

Query 6: ✅ Correct

Only product sizes containing a number are returned.

SQL and output
-- query for question 6: student answer
SELECT *, 
CASE WHEN INSTR(product_name,'-') > 0
  THEN LTRIM(RTRIM(SUBSTR(product_name,INSTR(product_name,'-')+1)))
  ELSE NULL END AS description


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",
    "description": "Jar"
  },
  {
    "product_id": 5,
    "product_name": "Whole Wheat Bread",
    "product_size": "1.5 lbs",
    "product_category_id": 3,
    "product_qty_type": "unit",
    "description": null
  },
  {
    "product_id": 7,
    "product_name": "Apple Pie",
    "product_size": "10\"",
    "product_category_id": 3,
    "product_qty_type": "unit",
    "description": null
  }
]

Query 7: ✅ Correct

The highest and lowest sales dates and totals are correct.

SQL and output
-- query for question 7: student answer
;WITH sales_per_market AS (
SELECT
market_date,
ROUND(SUM(quantity * cost_per_quantity),2) AS sales

FROM customer_purchases
GROUP BY market_date
)

,market_dates_ranked_by_sales AS (
SELECT
market_date,
sales,
RANK() OVER (ORDER BY sales) AS sales_rank_asc,
RANK() OVER (ORDER BY sales DESC) AS sales_rank_desc

FROM sales_per_market
)
      
SELECT market_date, sales, sales_rank_desc AS sales_rank
FROM market_dates_ranked_by_sales
WHERE sales_rank_asc = 1

UNION

SELECT market_date, sales, sales_rank_desc AS sales_rank
FROM market_dates_ranked_by_sales
WHERE sales_rank_desc = 1
[
  {
    "market_date": "2022-12-20",
    "sales": 979,
    "sales_rank": 1
  },
  {
    "market_date": "2023-04-07",
    "sales": 272.5,
    "sales_rank": 142
  }
]

Query 8: ✅ Correct

Possible revenue is correct for every vendor and product.

SQL and output
-- query for question 8: student answer
SELECT vendor_name, "sum(price)" AS gross_income, product_name, 0 AS extra FROM (SELECT vendor_name, product_name, sum(price)
FROM (
  SELECT DISTINCT
  vendor_name,
  product_name,
  5 * original_price as price

  FROM vendor_inventory vi 
  JOIN vendor v on v.vendor_id = vi.vendor_id
  JOIN product p on p.product_id = vi.product_id
  ) x
CROSS JOIN 
  (SELECT DISTINCT 
  customer_id
  FROM customer)
GROUP BY vendor_name, product_name);
[
  {
    "vendor_name": "Annie's Pies",
    "gross_income": 2340,
    "product_name": "Apple Pie",
    "extra": 0
  },
  {
    "vendor_name": "Annie's Pies",
    "gross_income": 2340,
    "product_name": "Cherry Pie",
    "extra": 0
  },
  {
    "vendor_name": "Annie's Pies",
    "gross_income": 845,
    "product_name": "Whole Wheat Bread",
    "extra": 0
  }
]

Query 9: ✅ Correct

Unit products and current snapshot timestamps are correct.

SQL and output
-- query for question 9: student answer
CREATE TEMP TABLE product_units AS

 SELECT p.*,
   CURRENT_TIMESTAMP AS snapshot_timestamp
 FROM product AS p
 WHERE product_qty_type = "unit"

Query 10: ✅ Correct

One product with a current timestamp was inserted.

SQL and output
-- query for question 10: student answer
INSERT INTO product_units (product_id, product_name,
  product_size, product_category_id, product_qty_type, snapshot_timestamp)
      SELECT
          product_id,
          product_name,
          product_size,
          product_category_id,
          product_qty_type,
          CURRENT_TIMESTAMP
      FROM product AS p
      WHERE product_id = 7

Query 11: ✅ Correct

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

SQL and output
-- query for question 11: student answer
WITH older_record AS
(SELECT product_id
    ,MIN(snapshot_timestamp) AS snapshot_timestamp
    FROM product_units 
    WHERE product_id = 7)

DELETE FROM product_units
WHERE (product_id = 
  (SELECT  product_id 
  FROM older_record
    )
AND snapshot_timestamp =  
  (SELECT  snapshot_timestamp 
  FROM older_record
    ))

Query 12: ✅ Correct

All rows remain and current_quantity matches the latest inventory.

SQL and output
-- query for question 12: student answer
ALTER TABLE product_units
ADD current_quantity INT;

UPDATE product_units
SET current_quantity = (
  SELECT current_quantity 
  FROM (
    SELECT  p.product_id, COALESCE(quantity,0) as current_quantity

    FROM product_units p
    LEFT JOIN (
      SELECT *
      ,ROW_NUMBER() OVER( PARTITION BY vi.product_id ORDER BY market_date DESC) AS rn
      FROM vendor_inventory vi 
    ) vi ON p.product_id  = vi.product_id

    WHERE rn = 1 
    OR rn IS NULL
  ) p
WHERE product_units.product_id = p.product_id);

@danielrazavi danielrazavi changed the title Test A2 reviewer fixes and collapsible feedback Integration test: arbitrary SQL column names and reviewer fixes Oct 20, 2026
@github-actions

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
-- query 1: arbitrary aliases and reordered output
SELECT "product_name || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')'" AS "q1_field_0" FROM (SELECT 
product_name || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')'
FROM product);
[
  {
    "q1_field_0": "Habanero Peppers - Organic, medium (lbs)"
  },
  {
    "q1_field_0": "Jalapeno Peppers - Organic, small (lbs)"
  },
  {
    "q1_field_0": "Poblano Peppers - Organic, large (unit)"
  }
]

Query 2: ✅ Correct

Visit dates are numbered correctly for each customer.

SQL and output
-- query 2: arbitrary aliases and reordered output
SELECT "visit_number" AS "q2_field_2", "market_date" AS "q2_field_1", "customer_id" AS "q2_field_0" FROM (-- query for question 2: student answer
SELECT customer_id, market_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date) AS visit_number

FROM customer_purchases
WHERE market_date < '2022-04-29'

GROUP BY customer_id, market_date
ORDER BY customer_id, market_date);
[
  {
    "q2_field_2": 1,
    "q2_field_1": "2022-04-08",
    "q2_field_0": 1
  },
  {
    "q2_field_2": 2,
    "q2_field_1": "2022-04-15",
    "q2_field_0": 1
  },
  {
    "q2_field_2": 3,
    "q2_field_1": "2022-04-19",
    "q2_field_0": 1
  }
]

Query 3: ✅ Correct

Only each customer's most recent visit is returned.

SQL and output
-- query 3: arbitrary aliases and reordered output
SELECT "market_date" AS "q3_field_1", "customer_id" AS "q3_field_0" FROM (-- query for question 3: student answer
SELECT customer_id, market_date 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
    ) x WHERE visit_number = 1);
[
  {
    "q3_field_1": "2023-10-13",
    "q3_field_0": 1
  },
  {
    "q3_field_1": "2023-10-13",
    "q3_field_0": 2
  },
  {
    "q3_field_1": "2023-10-10",
    "q3_field_0": 3
  }
]

Query 4: ✅ Correct

The purchase count is correct for every customer and product.

SQL and output
-- query 4: arbitrary aliases and reordered output
SELECT "cnt" AS "q4_field_3", "market_date" AS "q4_field_2", "product_id" AS "q4_field_1", "customer_id" AS "q4_field_0" FROM (-- query for question 4: student answer
SELECT customer_id, product_id, market_date,
        COUNT(*) OVER (PARTITION BY customer_id, product_id ORDER BY market_date) AS cnt
        FROM customer_purchases WHERE market_date < '2022-04-29');
[
  {
    "q4_field_3": 1,
    "q4_field_2": "2022-04-19",
    "q4_field_1": 4,
    "q4_field_0": 1
  },
  {
    "q4_field_3": 2,
    "q4_field_2": "2022-04-26",
    "q4_field_1": 4,
    "q4_field_0": 1
  },
  {
    "q4_field_3": 1,
    "q4_field_2": "2022-04-08",
    "q4_field_1": 5,
    "q4_field_0": 1
  }
]

Query 5: ✅ Correct

Product descriptions are extracted and trimmed correctly.

SQL and output
-- query 5: arbitrary aliases and reordered output
SELECT "description" AS "q5_field_5", "product_qty_type" AS "q5_field_4", "product_category_id" AS "q5_field_3", "product_size" AS "q5_field_2", "product_name" AS "q5_field_1", "product_id" AS "q5_field_0" FROM (-- query for question 5: student answer
SELECT *, 
CASE WHEN INSTR(product_name,'-') > 0
  THEN LTRIM(RTRIM(SUBSTR(product_name,INSTR(product_name,'-')+1)))
  ELSE NULL END AS description

FROM product);
[
  {
    "q5_field_5": "Organic",
    "q5_field_4": "lbs",
    "q5_field_3": 1,
    "q5_field_2": "medium",
    "q5_field_1": "Habanero Peppers - Organic",
    "q5_field_0": 1
  },
  {
    "q5_field_5": "Organic",
    "q5_field_4": "lbs",
    "q5_field_3": 1,
    "q5_field_2": "small",
    "q5_field_1": "Jalapeno Peppers - Organic",
    "q5_field_0": 2
  },
  {
    "q5_field_5": "Organic",
    "q5_field_4": "unit",
    "q5_field_3": 1,
    "q5_field_2": "large",
    "q5_field_1": "Poblano Peppers - Organic",
    "q5_field_0": 3
  }
]

Query 6: ✅ Correct

Only product sizes containing a number are returned.

SQL and output
-- query 6: arbitrary aliases and reordered output
SELECT "description" AS "q6_field_5", "product_qty_type" AS "q6_field_4", "product_category_id" AS "q6_field_3", "product_size" AS "q6_field_2", "product_name" AS "q6_field_1", "product_id" AS "q6_field_0" FROM (-- query for question 6: student answer
SELECT *, 
CASE WHEN INSTR(product_name,'-') > 0
  THEN LTRIM(RTRIM(SUBSTR(product_name,INSTR(product_name,'-')+1)))
  ELSE NULL END AS description


FROM product
WHERE product_size REGEXP( '[0-9]'));
[
  {
    "q6_field_5": "Jar",
    "q6_field_4": "unit",
    "q6_field_3": 3,
    "q6_field_2": "8 oz",
    "q6_field_1": "Banana Peppers - Jar",
    "q6_field_0": 4
  },
  {
    "q6_field_5": null,
    "q6_field_4": "unit",
    "q6_field_3": 3,
    "q6_field_2": "1.5 lbs",
    "q6_field_1": "Whole Wheat Bread",
    "q6_field_0": 5
  },
  {
    "q6_field_5": null,
    "q6_field_4": "unit",
    "q6_field_3": 3,
    "q6_field_2": "10\"",
    "q6_field_1": "Apple Pie",
    "q6_field_0": 7
  }
]

Query 7: ✅ Correct

The highest and lowest sales dates and totals are correct.

SQL and output
-- query 7: arbitrary aliases and reordered output
SELECT "sales_rank" AS "q7_field_2", "sales" AS "q7_field_1", "market_date" AS "q7_field_0" FROM (-- query for question 7: student answer
WITH sales_per_market AS (
SELECT
market_date,
ROUND(SUM(quantity * cost_per_quantity),2) AS sales

FROM customer_purchases
GROUP BY market_date
)

,market_dates_ranked_by_sales AS (
SELECT
market_date,
sales,
RANK() OVER (ORDER BY sales) AS sales_rank_asc,
RANK() OVER (ORDER BY sales DESC) AS sales_rank_desc

FROM sales_per_market
)
      
SELECT market_date, sales, sales_rank_desc AS sales_rank
FROM market_dates_ranked_by_sales
WHERE sales_rank_asc = 1

UNION

SELECT market_date, sales, sales_rank_desc AS sales_rank
FROM market_dates_ranked_by_sales
WHERE sales_rank_desc = 1);
[
  {
    "q7_field_2": 1,
    "q7_field_1": 979,
    "q7_field_0": "2022-12-20"
  },
  {
    "q7_field_2": 142,
    "q7_field_1": 272.5,
    "q7_field_0": "2023-04-07"
  }
]

Query 8: ✅ Correct

Possible revenue is correct for every vendor and product.

SQL and output
-- query 8: arbitrary aliases and reordered output
SELECT "extra" AS "q8_field_3", "product_name" AS "q8_field_2", "gross_income" AS "q8_field_1", "vendor_name" AS "q8_field_0" FROM (-- query for question 8: student answer
SELECT vendor_name, "sum(price)" AS gross_income, product_name, 0 AS extra FROM (SELECT vendor_name, product_name, sum(price)
FROM (
  SELECT DISTINCT
  vendor_name,
  product_name,
  5 * original_price as price

  FROM vendor_inventory vi 
  JOIN vendor v on v.vendor_id = vi.vendor_id
  JOIN product p on p.product_id = vi.product_id
  ) x
CROSS JOIN 
  (SELECT DISTINCT 
  customer_id
  FROM customer)
GROUP BY vendor_name, product_name));
[
  {
    "q8_field_3": 0,
    "q8_field_2": "Apple Pie",
    "q8_field_1": 2340,
    "q8_field_0": "Annie's Pies"
  },
  {
    "q8_field_3": 0,
    "q8_field_2": "Cherry Pie",
    "q8_field_1": 2340,
    "q8_field_0": "Annie's Pies"
  },
  {
    "q8_field_3": 0,
    "q8_field_2": "Whole Wheat Bread",
    "q8_field_1": 845,
    "q8_field_0": "Annie's Pies"
  }
]

Query 9: ✅ Correct

Unit products and current snapshot timestamps are correct.

SQL and output
-- query 9: arbitrary aliases and reordered output
-- query for question 9: student answer
CREATE TEMP TABLE product_units AS

 SELECT p.*,
   CURRENT_TIMESTAMP AS snapshot_timestamp
 FROM product AS p
 WHERE product_qty_type = "unit"

Query 10: ✅ Correct

One product with a current timestamp was inserted.

SQL and output
-- query 10: arbitrary aliases and reordered output
-- query for question 10: student answer
INSERT INTO product_units (product_id, product_name,
  product_size, product_category_id, product_qty_type, snapshot_timestamp)
      SELECT
          product_id,
          product_name,
          product_size,
          product_category_id,
          product_qty_type,
          CURRENT_TIMESTAMP
      FROM product AS p
      WHERE product_id = 7

Query 11: ✅ Correct

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

SQL and output
-- query 11: arbitrary aliases and reordered output
-- query for question 11: student answer
WITH older_record AS
(SELECT product_id
    ,MIN(snapshot_timestamp) AS snapshot_timestamp
    FROM product_units 
    WHERE product_id = 7)

DELETE FROM product_units
WHERE (product_id = 
  (SELECT  product_id 
  FROM older_record
    )
AND snapshot_timestamp =  
  (SELECT  snapshot_timestamp 
  FROM older_record
    ))

Query 12: ✅ Correct

All rows remain and current_quantity matches the latest inventory.

SQL and output
-- query 12: arbitrary aliases and reordered output
-- query for question 12: student answer
ALTER TABLE product_units
ADD current_quantity INT;

UPDATE product_units
SET current_quantity = (
  SELECT current_quantity 
  FROM (
    SELECT  p.product_id, COALESCE(quantity,0) as current_quantity

    FROM product_units p
    LEFT JOIN (
      SELECT *
      ,ROW_NUMBER() OVER( PARTITION BY vi.product_id ORDER BY market_date DESC) AS rn
      FROM vendor_inventory vi 
    ) vi ON p.product_id  = vi.product_id

    WHERE rn = 1 
    OR rn IS NULL
  ) p
WHERE product_units.product_id = p.product_id);

@github-actions

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
-- query 1: arbitrary aliases and reordered output
SELECT "product_name || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')'" AS "q1_field_0" FROM (SELECT 
product_name || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')'
FROM product);
[
  {
    "q1_field_0": "Habanero Peppers - Organic, medium (lbs)"
  },
  {
    "q1_field_0": "Jalapeno Peppers - Organic, small (lbs)"
  },
  {
    "q1_field_0": "Poblano Peppers - Organic, large (unit)"
  }
]

Query 2: ❌ Incorrect

The visit numbers or returned dates are incorrect.

-- query 2: arbitrary aliases and reordered output
SELECT "q2_field_2",
        COALESCE(LEAD("q2_field_1") OVER (ORDER BY "q2_field_0", "q2_field_1"),
                 FIRST_VALUE("q2_field_1") OVER (ORDER BY "q2_field_0", "q2_field_1")) AS "q2_field_1",
        "q2_field_0" FROM (SELECT "visit_number" AS "q2_field_2", "market_date" AS "q2_field_1", "customer_id" AS "q2_field_0" FROM (-- query for question 2: student answer
SELECT customer_id, market_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date) AS visit_number

FROM customer_purchases
WHERE market_date < '2022-04-29'

GROUP BY customer_id, market_date
ORDER BY customer_id, market_date));
[
  {
    "q2_field_2": 1,
    "q2_field_1": "2022-04-15",
    "q2_field_0": 1
  },
  {
    "q2_field_2": 2,
    "q2_field_1": "2022-04-19",
    "q2_field_0": 1
  },
  {
    "q2_field_2": 3,
    "q2_field_1": "2022-04-22",
    "q2_field_0": 1
  }
]

Query 3: ✅ Correct

Only each customer's most recent visit is returned.

SQL and output
-- query 3: arbitrary aliases and reordered output
SELECT "market_date" AS "q3_field_1", "customer_id" AS "q3_field_0" FROM (-- query for question 3: student answer
SELECT customer_id, market_date 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
    ) x WHERE visit_number = 1);
[
  {
    "q3_field_1": "2023-10-13",
    "q3_field_0": 1
  },
  {
    "q3_field_1": "2023-10-13",
    "q3_field_0": 2
  },
  {
    "q3_field_1": "2023-10-10",
    "q3_field_0": 3
  }
]

Query 4: ✅ Correct

The purchase count is correct for every customer and product.

SQL and output
-- query 4: arbitrary aliases and reordered output
SELECT "cnt" AS "q4_field_3", "market_date" AS "q4_field_2", "product_id" AS "q4_field_1", "customer_id" AS "q4_field_0" FROM (-- query for question 4: student answer
SELECT customer_id, product_id, market_date,
        COUNT(*) OVER (PARTITION BY customer_id, product_id ORDER BY market_date) AS cnt
        FROM customer_purchases WHERE market_date < '2022-04-29');
[
  {
    "q4_field_3": 1,
    "q4_field_2": "2022-04-19",
    "q4_field_1": 4,
    "q4_field_0": 1
  },
  {
    "q4_field_3": 2,
    "q4_field_2": "2022-04-26",
    "q4_field_1": 4,
    "q4_field_0": 1
  },
  {
    "q4_field_3": 1,
    "q4_field_2": "2022-04-08",
    "q4_field_1": 5,
    "q4_field_0": 1
  }
]

Query 5: ✅ Correct

Product descriptions are extracted and trimmed correctly.

SQL and output
-- query 5: arbitrary aliases and reordered output
SELECT "description" AS "q5_field_5", "product_qty_type" AS "q5_field_4", "product_category_id" AS "q5_field_3", "product_size" AS "q5_field_2", "product_name" AS "q5_field_1", "product_id" AS "q5_field_0" FROM (-- query for question 5: student answer
SELECT *, 
CASE WHEN INSTR(product_name,'-') > 0
  THEN LTRIM(RTRIM(SUBSTR(product_name,INSTR(product_name,'-')+1)))
  ELSE NULL END AS description

FROM product);
[
  {
    "q5_field_5": "Organic",
    "q5_field_4": "lbs",
    "q5_field_3": 1,
    "q5_field_2": "medium",
    "q5_field_1": "Habanero Peppers - Organic",
    "q5_field_0": 1
  },
  {
    "q5_field_5": "Organic",
    "q5_field_4": "lbs",
    "q5_field_3": 1,
    "q5_field_2": "small",
    "q5_field_1": "Jalapeno Peppers - Organic",
    "q5_field_0": 2
  },
  {
    "q5_field_5": "Organic",
    "q5_field_4": "unit",
    "q5_field_3": 1,
    "q5_field_2": "large",
    "q5_field_1": "Poblano Peppers - Organic",
    "q5_field_0": 3
  }
]

Query 6: ✅ Correct

Only product sizes containing a number are returned.

SQL and output
-- query 6: arbitrary aliases and reordered output
SELECT "description" AS "q6_field_5", "product_qty_type" AS "q6_field_4", "product_category_id" AS "q6_field_3", "product_size" AS "q6_field_2", "product_name" AS "q6_field_1", "product_id" AS "q6_field_0" FROM (-- query for question 6: student answer
SELECT *, 
CASE WHEN INSTR(product_name,'-') > 0
  THEN LTRIM(RTRIM(SUBSTR(product_name,INSTR(product_name,'-')+1)))
  ELSE NULL END AS description


FROM product
WHERE product_size REGEXP( '[0-9]'));
[
  {
    "q6_field_5": "Jar",
    "q6_field_4": "unit",
    "q6_field_3": 3,
    "q6_field_2": "8 oz",
    "q6_field_1": "Banana Peppers - Jar",
    "q6_field_0": 4
  },
  {
    "q6_field_5": null,
    "q6_field_4": "unit",
    "q6_field_3": 3,
    "q6_field_2": "1.5 lbs",
    "q6_field_1": "Whole Wheat Bread",
    "q6_field_0": 5
  },
  {
    "q6_field_5": null,
    "q6_field_4": "unit",
    "q6_field_3": 3,
    "q6_field_2": "10\"",
    "q6_field_1": "Apple Pie",
    "q6_field_0": 7
  }
]

Query 7: ✅ Correct

The highest and lowest sales dates and totals are correct.

SQL and output
-- query 7: arbitrary aliases and reordered output
SELECT "sales_rank" AS "q7_field_2", "sales" AS "q7_field_1", "market_date" AS "q7_field_0" FROM (-- query for question 7: student answer
WITH sales_per_market AS (
SELECT
market_date,
ROUND(SUM(quantity * cost_per_quantity),2) AS sales

FROM customer_purchases
GROUP BY market_date
)

,market_dates_ranked_by_sales AS (
SELECT
market_date,
sales,
RANK() OVER (ORDER BY sales) AS sales_rank_asc,
RANK() OVER (ORDER BY sales DESC) AS sales_rank_desc

FROM sales_per_market
)
      
SELECT market_date, sales, sales_rank_desc AS sales_rank
FROM market_dates_ranked_by_sales
WHERE sales_rank_asc = 1

UNION

SELECT market_date, sales, sales_rank_desc AS sales_rank
FROM market_dates_ranked_by_sales
WHERE sales_rank_desc = 1);
[
  {
    "q7_field_2": 1,
    "q7_field_1": 979,
    "q7_field_0": "2022-12-20"
  },
  {
    "q7_field_2": 142,
    "q7_field_1": 272.5,
    "q7_field_0": "2023-04-07"
  }
]

Query 8: ✅ Correct

Possible revenue is correct for every vendor and product.

SQL and output
-- query 8: arbitrary aliases and reordered output
SELECT "extra" AS "q8_field_3", "product_name" AS "q8_field_2", "gross_income" AS "q8_field_1", "vendor_name" AS "q8_field_0" FROM (-- query for question 8: student answer
SELECT vendor_name, "sum(price)" AS gross_income, product_name, 0 AS extra FROM (SELECT vendor_name, product_name, sum(price)
FROM (
  SELECT DISTINCT
  vendor_name,
  product_name,
  5 * original_price as price

  FROM vendor_inventory vi 
  JOIN vendor v on v.vendor_id = vi.vendor_id
  JOIN product p on p.product_id = vi.product_id
  ) x
CROSS JOIN 
  (SELECT DISTINCT 
  customer_id
  FROM customer)
GROUP BY vendor_name, product_name));
[
  {
    "q8_field_3": 0,
    "q8_field_2": "Apple Pie",
    "q8_field_1": 2340,
    "q8_field_0": "Annie's Pies"
  },
  {
    "q8_field_3": 0,
    "q8_field_2": "Cherry Pie",
    "q8_field_1": 2340,
    "q8_field_0": "Annie's Pies"
  },
  {
    "q8_field_3": 0,
    "q8_field_2": "Whole Wheat Bread",
    "q8_field_1": 845,
    "q8_field_0": "Annie's Pies"
  }
]

Query 9: ✅ Correct

Unit products and current snapshot timestamps are correct.

SQL and output
-- query 9: arbitrary aliases and reordered output
-- query for question 9: student answer
CREATE TEMP TABLE product_units AS

 SELECT p.*,
   CURRENT_TIMESTAMP AS snapshot_timestamp
 FROM product AS p
 WHERE product_qty_type = "unit"

Query 10: ✅ Correct

One product with a current timestamp was inserted.

SQL and output
-- query 10: arbitrary aliases and reordered output
-- query for question 10: student answer
INSERT INTO product_units (product_id, product_name,
  product_size, product_category_id, product_qty_type, snapshot_timestamp)
      SELECT
          product_id,
          product_name,
          product_size,
          product_category_id,
          product_qty_type,
          CURRENT_TIMESTAMP
      FROM product AS p
      WHERE product_id = 7

Query 11: ✅ Correct

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

SQL and output
-- query 11: arbitrary aliases and reordered output
-- query for question 11: student answer
WITH older_record AS
(SELECT product_id
    ,MIN(snapshot_timestamp) AS snapshot_timestamp
    FROM product_units 
    WHERE product_id = 7)

DELETE FROM product_units
WHERE (product_id = 
  (SELECT  product_id 
  FROM older_record
    )
AND snapshot_timestamp =  
  (SELECT  snapshot_timestamp 
  FROM older_record
    ))

Query 12: ✅ Correct

All rows remain and current_quantity matches the latest inventory.

SQL and output
-- query 12: arbitrary aliases and reordered output
-- query for question 12: student answer
ALTER TABLE product_units
ADD current_quantity INT;

UPDATE product_units
SET current_quantity = (
  SELECT current_quantity 
  FROM (
    SELECT  p.product_id, COALESCE(quantity,0) as current_quantity

    FROM product_units p
    LEFT JOIN (
      SELECT *
      ,ROW_NUMBER() OVER( PARTITION BY vi.product_id ORDER BY market_date DESC) AS rn
      FROM vendor_inventory vi 
    ) vi ON p.product_id  = vi.product_id

    WHERE rn = 1 
    OR rn IS NULL
  ) p
WHERE product_units.product_id = p.product_id);

@github-actions

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: ❌ Incorrect

The formatted product descriptions do not match the expected results.

SELECT product_name FROM product;
[
  {
    "product_name": "Habanero Peppers - Organic"
  },
  {
    "product_name": "Jalapeno Peppers - Organic"
  },
  {
    "product_name": "Poblano Peppers - Organic"
  }
]

Query 2: ❌ Incorrect

The visit numbers or returned dates are incorrect.

SELECT customer_id, market_date, 99 AS visit_number FROM customer_purchases;
[
  {
    "customer_id": 14,
    "market_date": "2022-07-05",
    "visit_number": 99
  },
  {
    "customer_id": 14,
    "market_date": "2022-07-05",
    "visit_number": 99
  },
  {
    "customer_id": 15,
    "market_date": "2022-07-05",
    "visit_number": 99
  }
]

Query 3: ❌ Incorrect

The result does not contain only each customer's most recent visit.

SELECT customer_id, market_date, 99 AS visit_number FROM customer_purchases;
[
  {
    "customer_id": 14,
    "market_date": "2022-07-05",
    "visit_number": 99
  },
  {
    "customer_id": 14,
    "market_date": "2022-07-05",
    "visit_number": 99
  },
  {
    "customer_id": 15,
    "market_date": "2022-07-05",
    "visit_number": 99
  }
]

Query 4: ❌ Incorrect

Include the customer ID, product ID, market date, and a purchase count for every purchase row. Check the counts and the filter to dates before April 29, 2022; vendor_id and transaction_time are optional.

SELECT *, 99 AS product_purchase_count FROM customer_purchases;
[
  {
    "product_id": 1,
    "vendor_id": 7,
    "market_date": "2022-07-05",
    "customer_id": 14,
    "quantity": 0.99,
    "cost_per_quantity": 6.99,
    "transaction_time": "17:32:00",
    "product_purchase_count": 99
  },
  {
    "product_id": 1,
    "vendor_id": 7,
    "market_date": "2022-07-05",
    "customer_id": 14,
    "quantity": 2.18,
    "cost_per_quantity": 6.99,
    "transaction_time": "18:23:00",
    "product_purchase_count": 99
  },
  {
    "product_id": 1,
    "vendor_id": 7,
    "market_date": "2022-07-05",
    "customer_id": 15,
    "quantity": 1.53,
    "cost_per_quantity": 6.99,
    "transaction_time": "18:41:00",
    "product_purchase_count": 99
  }
]

Query 5: ❌ Incorrect

The descriptions do not match the text after each product-name hyphen.

SELECT product_name, NULL AS description FROM product;
[
  {
    "product_name": "Habanero Peppers - Organic",
    "description": null
  },
  {
    "product_name": "Jalapeno Peppers - Organic",
    "description": null
  },
  {
    "product_name": "Poblano Peppers - Organic",
    "description": null
  }
]

Query 6: ❌ Incorrect

The result does not match all product sizes containing a number.

SELECT * FROM product;
[
  {
    "product_id": 1,
    "product_name": "Habanero Peppers - Organic",
    "product_size": "medium",
    "product_category_id": 1,
    "product_qty_type": "lbs"
  },
  {
    "product_id": 2,
    "product_name": "Jalapeno Peppers - Organic",
    "product_size": "small",
    "product_category_id": 1,
    "product_qty_type": "lbs"
  },
  {
    "product_id": 3,
    "product_name": "Poblano Peppers - Organic",
    "product_size": "large",
    "product_category_id": 1,
    "product_qty_type": "unit"
  }
]

Query 7: ❌ Incorrect

The result does not match the highest and lowest sales dates.

SELECT market_date, 0 AS sales FROM customer_purchases GROUP BY market_date LIMIT 2;
[
  {
    "market_date": "2022-04-05",
    "sales": 0
  },
  {
    "market_date": "2022-04-08",
    "sales": 0
  }
]

Query 8: ❌ Incorrect

The possible revenue does not match five items per customer.

SELECT vendor_name, product_name, 0 AS revenue FROM vendor CROSS JOIN product;
[
  {
    "vendor_name": "Annie's Pies",
    "product_name": "Habanero Peppers - Organic",
    "revenue": 0
  },
  {
    "vendor_name": "Annie's Pies",
    "product_name": "Jalapeno Peppers - Organic",
    "revenue": 0
  },
  {
    "vendor_name": "Annie's Pies",
    "product_name": "Poblano Peppers - Organic",
    "revenue": 0
  }
]

Query 9: ❌ Incorrect

Check the columns, unit products, and current snapshot timestamps.

CREATE TEMP TABLE product_units AS SELECT * FROM product;

Query 10: ⚪ Cannot assess: prerequisite failed

Question 9 must create a valid snapshot first.

SELECT 1;

Query 11: ⚪ Cannot assess: prerequisite failed

Question 9 must create a valid snapshot first.

DELETE FROM product_units WHERE 1 = 0;

Query 12: ⚪ Cannot assess: prerequisite failed

Question 9 must create a valid snapshot first.

ALTER TABLE product_units ADD current_quantity INT;
UPDATE product_units SET current_quantity = -1;

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.

2 participants