SQL Challenger
0 of 250 complete

Lessons

0 of 250 complete

Saved in this browser

Lesson 184 of 250

Easy

Inspect a receipt date equality lookup

Your task

Show the query plan for selecting amount from receipts where sold_on equals '2025-01-16'.

An equality comparison against an indexed column can let SQLite search directly for a matching key.

Sample data

Table:receipts
idsold_onamountchannelmetadata
12025-01-0136 ONLINE {"currency":"EUR","tags":["gift","jazz"]}
22025-01-1622Store{"currency":"EUR","tags":["indie"]}
32025-01-3154 online{"currency":"EUR","tags":["returning","bundle"]}
42025-03-0438STORE {"currency":"EUR","tags":[]}
52025-04-0944Online{"currency":"EUR","tags":["indie"]}

Expected output

idparentnotuseddetail
3062SEARCH receipts USING INDEX idx_receipts_sold_on (sold_on=?)
Show a hint

Prefix the SELECT with EXPLAIN QUERY PLAN and compare sold_on directly.

Indexes and query plans guide

Read SCAN and SEARCH

EXPLAIN QUERY PLAN describes access paths. SCAN indicates a scan of a table or index. SEARCH indicates that SQLite can use constraints to narrow the rows it visits. The exact plan depends on indexes, the query, and the database contents.

EXPLAIN QUERY PLAN
SELECT title FROM catalog WHERE stock = 3;

EXPLAIN QUERY PLAN
SELECT title FROM catalog WHERE genre = 'Jazz' AND price = 25;

Use a composite index from the leading column

The catalog index orders entries by genre and then by price. Filtering by genre and a price range lets SQLite narrow the search and read prices in index order. Selecting only genre and price can make this a covering lookup, which avoids reading extra columns from the table.

EXPLAIN QUERY PLAN
SELECT genre, price
FROM catalog
WHERE genre = 'Jazz' AND price >= 20
ORDER BY price;

Look for a separate sort

The plan may report a temporary B-tree when no available access path provides the requested order. A scan can still use an index, so look at the whole plan. Indexes trade read improvements for extra storage and work on writes.

EXPLAIN QUERY PLAN
SELECT title FROM catalog ORDER BY title;
SQLiteCtrl / ⌘ + Enter

Press Control+Space for SQL suggestions. Use the arrow keys to select an option and Enter to insert it.

Results

Loading SQLite…