task Constraints · hard
Enforce the reservation boundary
Create safe_reservations with shopper_id, item_id and quantity. Load the latest valid feed row for each shopper/item pair, then return the loaded rows. Valid input has existing shopper and item keys and an integer-stored quantity from 1 through 5.
what to return
- Columns
shopper_iditem_idquantityin this order- Rows
- 3 rows · in the order asked
- Watch for
- Filter invalid feed rows before choosing greatest feed id per pair. A later invalid row must not suppress an earlier valid one.
- The target must reject NULL keys, orphan keys, duplicate pairs, NULL quantity, fractional quantity and quantities outside 1–5; valid new rows must remain insertable.
- Use a composite primary key, NOT NULL, foreign keys and a quantity CHECK including typeof(quantity). Order by shopper_id,item_id.
- Skill
- Separate staging cleanup from durable relational and domain constraints. Refresher: Stored procedures: parameters & contracts →
expected result
Your query should return exactly these columns and values. Run it and compare.
expected result3 rows · in this order
| shopper_id | item_id | quantity |
|---|---|---|
| 1 | 1 | 4 |
| 2 | 4 | 1 |
| 7 | 4 | 5 |
all 3 rows
data you can query
Sample rows from each table. Each column shows its type underneath; → marks a link to another table.
market_reservation_feed4 columns · 11 rows
| idinteger · key | shopper_idinteger | item_idinteger | quantityblob |
|---|---|---|---|
| 1 | 1 | 1 | 2 |
| 2 | 1 | 1 | 4 |
| 3 | 2 | 4 | 1 |
| 4 | 2 | 4 | 9 |
| 5 | 3 | 7 | 0 |
showing 5 of 11 rows
shoppers5 columns · 17 rows
| idinteger · key | nametext | emailtext | regiontext | signup_datetext |
|---|---|---|---|---|
| 1 | Ana Ruiz | ana@harbor.test | West | 2025-01-03 |
| 2 | Ben Okafor | ben@harbor.test | East | 2025-01-05 |
| 3 | Chloe Park | chloe@harbor.test | West | 2025-01-09 |
| 4 | Dmitri Volkov | dmitri@harbor.test | North | 2025-01-14 |
| 5 | Esi Mensah | NULL | South | 2025-01-20 |
showing 5 of 17 rows
catalog_items4 columns · 13 rows
| idinteger · key | nametext | categorytext | pricereal |
|---|---|---|---|
| 1 | Desk Lamp | Lighting | 39 |
| 2 | Floor lamp | Lighting | 89 |
| 3 | String Lights | Lighting | 19.5 |
| 4 | Chef Knife | Kitchen | 59 |
| 5 | Cast Iron Pan | Kitchen | 45 |
showing 5 of 13 rows
solution.sql
Hints and the answer are always available. Using them counts the question as solved with help.