runtime
38 / 88

task Indexes · medium

A covering index for unit totals

The report “units sold per item” reads order_lines.item_id and quantity. Create an index named idx_lines_item_qty on order_lines that starts with item_id and also contains quantity, so SQLite can answer from the index alone. Then return item_id and units (sum of quantity) per item.

what to return

Columns
item_idunits in this order
Rows
12 rows · any order
Watch for
Column order in the index matters: item_id first, then quantity.
Skill
Design a composite index that covers a query. Refresher: SARGable ranges, not speed commandments →

expected result

Your query should return exactly these columns and values. Run it and compare.

expected result12 rows · any order
item_idunits
16
24
39

first 3 of 12 rows

data you can query

Sample rows from each table. Each column shows its type underneath; → marks a link to another table.

order_lines4 columns · 48 rows
order_idinteger → sales_ordersitem_idinteger → catalog_itemsquantityintegerunit_pricereal
1011139
1013219.5
1024159
1035145
10310312

showing 5 of 48 rows

solution.sql
 

Hints and the answer are always available. Using them counts the question as solved with help.