SQL Challenger
0 of 250 complete

Lessons

0 of 250 complete

Saved in this browser

Lesson 183 of 250

Easy

Read the receipt date index’s key columns

Your task

Return seqno and name for the key columns in idx_receipts_sold_on. Sort by sequence number.

Index metadata shows the order and name of each indexed column.

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

seqnoname
0sold_on
Show a hint

Select the two columns from pragma_index_info('idx_receipts_sold_on').

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…