runtime
294 / 500
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.

Functionproducts_by_skus(conn, skus)
Tableproducts (sku, name, category, price, stock)
Returnsa dictionary {sku: (name, price)} for the SKUs that exist; unknown SKUs are left out; {} for an empty list
Rulesone SELECT for the whole list; values go through placeholders, never into the SQL text

Loading your progress…

Look up a basket's products in one query — Python practice · Runtime