question 294 Packaging & Databases · easy
Look up a basket's products in one query
The checkout page needs the name and price of every product in the basket. Running one query per SKU is slow on a real database (the "N+1 queries" problem), so write products_by_skus(conn, skus) that asks for all of them in one SELECT with WHERE sku IN (?, ?, …), one ? per SKU.
Function
products_by_skus(conn, skus)Table
products (sku, name, category, price, stock)Returnsa dictionary
{sku: (name, price)} for the SKUs that exist; unknown SKUs are left out; {} for an empty listRulesone SELECT for the whole list; values go through placeholders, never into the SQL text