Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

Given index idx ON orders(customer_id, status, created_at), which of these queries can use it efficiently and how? (a) WHERE customer_id = 7 (b) WHERE status = 'NEW' (c) WHERE customer_id = 7 AND created_at > '2026-01-01' (d) WHERE customer_id = 7 AND status = 'NEW' ORDER BY created_at DESC LIMIT 20

With idx on (customer_id, status, created_at): (a) uses it well, because customer_id is the leading column; (b) cannot use it efficiently, because the index is not ordered by status alone, though skip scan can help when customer_id has few distinct values; (c) uses it partly, seeking on customer_id but only filtering created_at on the index entries, because status sits in between; and (d) is the best case, with equality on the first two columns and entries already ordered by created_at, so a backward scan returns 20 rows with no sort.

Background to this question

Composite indexes are where most indexing mistakes happen, because the order of columns decides which queries the index can serve, and the rule is easy to state but easy to misapply. This question gives four queries against one index so we can show that we can predict the plan for each. In this answer, we will picture how the index entries are sorted, walk through each query and what the database does with it, look at the plans, and end with the rule of thumb and how to change the index if (c) matters most.

1Picturing the Sorted Entries

A composite B-tree index sorts its entries by the first column, then by the second within equal first values, then by the third, exactly like a phone book sorted by last name, then first name. So all entries for customer 7 sit together; inside them, all NEW entries sit together, sorted by created_at; then all PAID entries, sorted by created_at again, and so on.

Everything about which query can use the index follows from this picture. The database can jump to any point in the sort order and read forwards or backwards. It can only jump efficiently to a place defined by a leading part of the key.

Entries in idx (customer_id, status, created_at)
customer_idstatuscreated_atNote
6PAID2026-03-02previous customer
7NEW2025-11-20customer 7 starts here
7NEW2026-02-14
7NEW2026-04-01(d) starts here and reads backwards
7PAID2025-12-01dates start over for each status
7PAID2026-03-09
7SHIPPED2026-01-05customer 7 ends after this status
8NEW2026-01-11next customer

2Query (a): WHERE customer_id = 7

customer_id is the leading column, so all its entries are contiguous. The database descends the tree to the first entry for 7 and reads until the first entry for 8. This is a range on the index prefix, as efficient as an index on customer_id alone.

SQL
EXPLAIN SELECT * FROM orders WHERE customer_id = 7;
-- Index Scan using idx on orders
-- Index Cond: (customer_id = 7)

3Query (b): WHERE status = 'NEW'

Now look at the picture again. NEW entries appear inside every customer's block, scattered through the whole index. There is no single place to jump to. Usually the database either scans the table or reads the entire index and filters it, which only pays off when the index is much narrower than the table.

There is one exception. A skip scan treats the missing leading column as a list of values and does one small seek per value: customer 1 and NEW, customer 2 and NEW, and so on. MySQL 8 can do this when the leading column has few distinct values, and PostgreSQL 18 added skip scan to B-tree indexes too; check your version. With thousands of customers it is no help, so (b) needs its own index if it is frequent, perhaps a partial index on rows where status = 'NEW'.

SQL
EXPLAIN SELECT * FROM orders WHERE status = 'NEW';
-- Seq Scan on orders Filter: (status = 'NEW') -- typical with many customers
-- MySQL with few customers: type range, Extra: Using index for skip scan

4Query (c): customer_id = 7 AND created_at > '2026-01-01'

customer_id = 7 narrows the scan to customer 7's block. But inside that block the entries are ordered by status first, and created_at only within each status. So the dates do not form one contiguous range: there is a run of NEW dates, then a run of PAID dates starting again from the oldest. The database cannot jump to '2026-01-01' once. It reads all of customer 7's entries and checks created_at on each entry in the index, discarding the old ones before visiting the table.

So the index is used, but the seek boundary is only customer_id; created_at is applied as a filter on the index entries. For a customer with 50 orders that is nothing; for a customer with 2 million orders it is a lot of wasted reading. With only a handful of statuses, the skip scan in PostgreSQL 18 can also help here, seeking once per status value; check your version.

SQL
EXPLAIN SELECT * FROM orders WHERE customer_id = 7 AND created_at > '2026-01-01';
-- PostgreSQL: Index Scan using idx
-- Index Cond: ((customer_id = 7) AND (created_at > '2026-01-01')) -- seek on customer_id only
-- MySQL: type ref on customer_id, Extra: Using index condition -- created_at pushed down

Notice that PostgreSQL still prints created_at inside Index Cond, which can mislead us. The condition is checked inside the index, but only the leading equality defines where the scan starts and stops. MySQL's 'Using index condition' means the same thing: index condition pushdown filters entries before reading rows.

5Query (d): customer_id = 7 AND status = 'NEW' ORDER BY created_at DESC LIMIT 20

This is the query the index was designed for. Equality on customer_id and status pins the scan to one contiguous run, customer 7's NEW entries, and within that run the entries are already sorted by created_at. The database jumps to the end of the run and reads backwards, and after 20 entries it stops. No sort, no wasted entries.

Java
EXPLAIN SELECT * FROM orders WHERE customer_id = 7 AND status = 'NEW'
ORDER BY created_at DESC LIMIT 20;
-- Limit
-- -> Index Scan Backward using idx on orders
-- Index Cond: ((customer_id = 7) AND (status = 'NEW')) -- no Sort node

6The Rule of Thumb

Put the columns we compare with equality first, then the one column we use for a range or for sorting. The reason is that equality on leading columns keeps the remaining entries in one contiguous, sorted run, and a range or sort on the next column can use that order. Once a column is used as a range, the columns after it are no longer sorted within the range, so they can only be filtered. In short, the range column ends the usable prefix.

Applied here: (a) and (d) follow the rule, (c) skips a column between the equality and the range, and (b) skips the leading column entirely.

7Follow-up questions

How would you change the index if (c) is the most common query?

Put created_at directly after customer_id, so the dates for one customer form a single sorted run: an index on (customer_id, created_at) seeks straight to the first date and reads only matching entries. If (d) is still important, we can keep the original index for it as well, or use (customer_id, created_at, status) for both, where (d) reads customer 7's entries newest first and filters status inside the index until it has 20 NEW rows, which is cheap when NEW orders are common and expensive when they are rare. We choose by how frequent each query is and measure with EXPLAIN ANALYZE.

Does the order of conditions in the WHERE clause matter?

No. The planner treats WHERE status = 'NEW' AND customer_id = 7 exactly like the reverse. Only the order of columns in the index matters.

Would (d) still avoid a sort with ORDER BY created_at ASC?

Yes. A B-tree can be read forwards as easily as backwards, so ascending order is a forward scan of the same run. Mixed directions across several sort columns are different and may need an index declared with matching DESC columns.

8Conclusion

In this answer, we saw how a composite index on (customer_id, status, created_at) sorts its entries and what that means for four queries: (a) uses the leading column as a range, (b) cannot seek without the leading column except through a skip scan when it has few values, (c) seeks on customer_id but only filters created_at because status sits between, and (d) uses equality on the first two columns and reads entries already in created_at order, so a backward scan returns 20 rows without sorting. The rule is equality columns first, then the range or sort column, which ends the usable prefix.

What to say in the interviewAbout 90 seconds aloud

Open with

I picture the index as sorted by customer_id, then status, then created_at. For (a), customer_id is the leading column, so all of customer 7's entries are together and the database does a range scan on that prefix, which is efficient.

Then, if they want more

For (b), status alone is not ordered across the index, NEW entries are scattered inside every customer's block, so normally it is a sequential scan or a full index scan. The exception is a skip scan, which MySQL 8 and now PostgreSQL 18 can do when customer_id has few distinct values; otherwise (b) needs its own index.

For (c), customer_id narrows the scan to customer 7, but because status sits between customer_id and created_at, the dates are sorted only within each status. So created_at cannot be a seek boundary; it is checked on each index entry as a filter, in MySQL shown as Using index condition.

For (d), equality on customer_id and status gives one contiguous run that is already sorted by created_at, so the database jumps to its end, reads backwards and stops after 20 rows with no sort. The rule I use is equality columns first, then the range or sort column, because the range column ends the usable prefix.

If (c) were the most common query, I would add or switch to an index on customer_id and created_at.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 18, MySQL 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Given index idx ON orders(customer_id, status, created_at), which of these queries can...": https://scrivio-interview-library.netlify.app/databases/indexing/d0922c3b92/