Databases & SQL Interview Questions: Indexes

Reviewed by Mark Dickie · Last updated

15 questions on this page you can answer and see marked on the spot. Go to the first one

A database index is a secondary data structure that lets the query engine find rows without scanning the entire table. For a SQL interview focused on indexes, you should know the trade-off between read speed and write cost, the difference between clustered and non-clustered indexes, and when a covering index lets the engine skip the table lookup entirely. Interviewers also probe whether you can read an execution plan and explain why a query is doing a full scan instead of using the index you expected.

Here's a quick map of the index types you'll be asked about:

Index typeWhat it storesTypical interview question
B-tree (clustered)Table rows sorted by keyWhy can a table have only one clustered index?
B-tree (non-clustered)Key + pointer to rowWhen does the optimizer choose a scan over a seek?
Covering indexAll columns the query needsHow does INCLUDE differ from KEY columns?
Hash (memory-optimized)Hash bucket of keyWhen does a hash index beat a B-tree?
BitmapBitmap of row IDs per valueWhy are bitmap indexes rare in OLTP engines?

What does an index cost, and when should you avoid one?

Every index adds storage and slows writes because inserts, updates, and deletes must maintain each index. The decision to add one comes down to read frequency versus write frequency and whether the query pattern justifies it. You should be able to articulate these trade-offs:

  1. A write-heavy table with few reads may be better off with no index beyond the primary key.
  2. A composite index is only useful when the query filters on a leftmost prefix of its columns.
  3. An index on a low-cardinality column (e.g., a boolean flag) rarely gets picked because the optimizer estimates a scan is cheaper.
  4. A covering index avoids the key lookup, but adding too many included columns bloats the index and negates the write-cost savings.
  5. The optimizer may ignore an index if statistics are stale or if the query uses a function on the indexed column, preventing a seek.

How do you debug a query that ignores an index you created?

Start by reading the execution plan: a table scan where you expected an index seek tells you the optimizer saw a cheaper plan. Common root causes include wrapping the indexed column in a function (e.g., WHERE UPPER(name) = 'JANE'), implicit type conversions that change the predicate from a seek to a scan, and statistics that no longer reflect the data distribution. Updating statistics, rewriting the predicate to be sargable, or adding the right included columns usually fixes it. In practice, the ability to explain sargability and leftmost-prefix rules is what separates a junior answer from a senior one in this part of the interview.

Key facts

  • Tarmac has 73 Databases & SQL interview questions on this topic, 25 of them on this page, at difficulty 1–5 of 5.
  • Tarmac tracked 3,405 job postings asking for Databases & SQL in November 2026.
  • Roles asking for Databases & SQL advertise a median base salary of £70,000, across 1,264 job postings as of November 2026.
  • Tarmac last reviewed these Databases & SQL interview questions on 13 October 2026.

At a glance

Questions25 shown · 73 in the bank
Difficulty1–5 of 5
FormatsTrue / false, Fill in the blank, Multiple choice, Short answer, Multiple answer, Flashcard

What you'll review

  1. indexes
  2. btree
  3. composite index
  4. index tradeoffs
  5. covering index

Practice questions

Try one before you open the answer. Pick an option and press Check; it's marked on the spot.

True or false: creating an index on a column generally speeds up SELECT queries that filter on that column, but adds write overhead to INSERT, UPDATE, and DELETE operations, because the index must be maintained whenever the data it covers changes.#

Options

Show answer

True. A database index is a separate structure, usually a B-tree, that lets the database find matching rows without scanning the whole table, so filtered SELECT queries on that column generally get faster. The cost is that every write touching indexed data must also update the index, which slows INSERT, UPDATE, and DELETE and uses extra storage.

Why:

A database index is a separate data structure, typically a B-tree, that lets the database locate matching rows without scanning the whole table, so queries that filter on the indexed column usually run faster. The index must stay in sync with the table, so writes that change indexed data also have to update the index, which adds overhead to INSERT, UPDATE, and DELETE. The statement is true because it names both sides of this classic trade-off: faster reads in exchange for slower writes and extra storage.

To create an index named idx_email on the email column of a table named users, you write the following SQL statement: _____ INDEX idx_email ON users(email); Fill in the missing SQL keyword.#

Show answer

To create an index named idx_email on the email column of a table named users, you write the following SQL statement: **CREATE** INDEX idx_email ON users(email); Fill in the missing SQL keyword.

Why:

The standard SQL syntax for creating an index is CREATE INDEX <index_name> ON <table_name>(<column_name>);. Filling the blank with CREATE produces the valid statement CREATE INDEX idx_email ON users(email);, which creates an index named idx_email on the email column of the users table.

True or false: a database index improves SELECT query performance but typically increases the cost of INSERT, UPDATE, and DELETE operations on the indexed table.#

Options

Show answer

True. A database index speeds up SELECT queries because the engine can locate matching rows through a lookup structure, such as a B-tree, instead of scanning the entire table. The trade-off is that the index must be maintained on every INSERT, UPDATE, and DELETE on the indexed table, which adds write overhead.

Why:

This is true. An index is an auxiliary lookup structure, commonly a B-tree, that lets the engine find matching rows without scanning the whole table, which speeds up reads. The index must also be maintained whenever rows are inserted, updated, or deleted, so write operations pay an extra cost.

A table employees has a composite B-tree index on (last_name, first_name). Assume the database uses only the standard leftmost-prefix seek (no skip-scan optimization). Which query below cannot use this index to avoid a full table scan?#

Options

Show answer

A query that filters only on first_name, such as WHERE first_name = 'John', cannot seek into a composite B-tree index on (last_name, first_name). Because the index is ordered by the leading column last_name, the leftmost-prefix rule requires a filter on that column to avoid a full table scan.

Why:

Composite B-tree indexes follow the leftmost-prefix rule: the index can be sought efficiently only when the leading column appears in the filter. The index is ordered by last_name first, then first_name, so a filter on first_name alone has no starting point in the index. The query filtering only on first_name must scan the table, while the queries that filter on last_name alone, on last_name and first_name together, or on a range of last_name all constrain the leading column and can use the index.

A PostgreSQL 17 database has a table employees with a composite B-tree index on (last_name, first_name). Which single-column filter cannot use this index for an index seek (a targeted B-tree traversal directly to matching entries)?#

Options

Show answer

In PostgreSQL 17, a query filtering only on first_name cannot use a composite B-tree index on (last_name, first_name) for an index seek. The index is ordered by last_name first, and that version has no skip scan to bypass a missing leading column. Filters on last_name, alone or combined with first_name, can seek directly.

Why:

A B-tree index is ordered by its leading column first and by later columns only within equal leading values, so the index can be traversed directly only when the leading column (last_name) appears in the predicate. A filter on first_name alone omits the leading column; PostgreSQL 17 has no skip scan, so it cannot jump through the index to the matching first_name values and would need a full index scan or a table scan instead. The filters on last_name alone, on a last_name range, and on both columns all constrain the leading column and can seek directly to matching entries.

True or false: adding a B-tree index to a column that frequently appears in WHERE clauses always improves overall query performance.#

Options

Show answer

False. A B-tree index on a frequently filtered column does not always improve overall performance. It speeds up matching reads but adds write maintenance cost and storage. On small tables, low-selectivity columns, or write-heavy workloads, a sequential scan or no index can be faster overall.

Why:

The statement is false because of the word 'always'. An index speeds up read lookups on that column, but every INSERT, UPDATE, and DELETE must also maintain the index, which adds write overhead and consumes extra storage and memory. On small tables a sequential scan can be as fast or faster, and on low-selectivity columns the optimizer may still choose a full scan because the index would return too many matching rows. For write-heavy workloads the maintenance cost can outweigh the read gains, so indexing is a trade-off rather than a guaranteed improvement.

A table employees has a composite B-tree index on (dept_id, last_name, first_name). Under the leftmost-prefix rule, which WHERE clause allows the index to seek on every column it filters on?#

Options

Show answer

The WHERE clause filtering on dept_id and last_name lets a (dept_id, last_name, first_name) index seek on every filtered column. Under the leftmost-prefix rule, a seek needs a contiguous prefix of the indexed columns starting at the first one. Any clause that skips dept_id or leaves a gap before a column cannot seek on that column.

Why:

A composite B-tree index can seek only on a contiguous leftmost prefix of its columns. The filter on dept_id and last_name matches the prefix (dept_id, last_name), so both columns take part in the seek. Filtering on last_name alone skips dept_id, so no prefix match exists. Filtering on dept_id and first_name skips last_name, which breaks the prefix chain, so first_name cannot be seeked. Filtering on first_name and last_name skips dept_id entirely, so neither column can be seeked.

Which predicate can a standard B-tree index on created_at accelerate with a single index range scan?#

Options

Show answer

WHERE created_at >= '2024-01-01' is the one a B-tree can accelerate with a single range scan. A B-tree stores keys in sorted order, so range and equality predicates on the bare column map to a contiguous slice. Wrapping the column in a function such as EXTRACT or a cast, or using a leading-wildcard LIKE or <>, defeats the index because the stored key order no longer lines up with the predicate.

Why:

A B-tree stores keys in sorted order, so range and equality predicates on the bare column (>=, >, <, BETWEEN, =) map to a contiguous slice it can scan. Wrapping the column in a function (EXTRACT, a cast) or using a leading-wildcard LIKE or <> makes the index unusable because the stored key order no longer lines up with the predicate.

A standard B-tree index on name can efficiently satisfy WHERE name LIKE '%son' (a leading-wildcard pattern).#

Options

Show answer

False. A B-tree is ordered by the start of the key, so it can seek prefix patterns like LIKE 'son%' but not leading-wildcard patterns like LIKE '%son', which would require checking every key. Suffix or substring search needs a different approach, such as indexing the reversed string, a trigram index, or full-text search.

Why:

A B-tree is ordered by the start of the key, so it can seek prefixes (LIKE 'son%') but not patterns that begin with a wildcard (LIKE '%son'), which would require checking every key. Suffix or substring search needs a different approach, such as indexing the reversed string, a trigram index, or full-text search.

In PostgreSQL 17 (before skip-scan support arrived in version 18), a table orders has a composite B-tree index on (customer_id, order_date, status). Three queries run against this table:#

Show answer

The query filtering on order_date and status (query 3) cannot use the index for a directed seek, because it omits customer_id, the leftmost column of the composite index. A B-tree composite index can only seek when the predicates cover a contiguous leftmost prefix of the indexed columns. The query on customer_id and order_date matches the full prefix (customer_id, order_date), and the query on customer_id and status seeks on customer_id, with status applied as a filter; both can use the index, but query 3 has no leftmost-column constraint, so the planner falls back to a full index scan or a sequential scan.

Why:

A composite B-tree index can perform a directed seek only when the WHERE clause constrains a contiguous prefix starting at the leftmost column. The query filtering on customer_id and order_date constrains the full prefix (customer_id, order_date), and the query filtering on customer_id and status constrains customer_id, so both can seek; in the second, status is applied as a filter after the seek. The query filtering on order_date and status never constrains customer_id, so the index cannot narrow the search to a range and PostgreSQL 17 would use a full index scan or sequential scan instead. The determining principle is the leftmost-prefix rule.

In PostgreSQL 16, a table orders has a composite B-tree index on (customer_id, order_date, status). Which of the following queries gives the index no condition on its leading column customer_id, so the index cannot restrict the scan to a contiguous range of its entries?#

Options

Show answer

The query SELECT * FROM orders WHERE order_date >= '2024-01-01' AND status = 'shipped'; cannot have its index range restricted by a composite index on (customer_id, order_date, status). It has no condition on the leading column customer_id, so the index entries cannot be bounded to a contiguous range; the other queries all constrain customer_id by equality or a range.

Why:

A B-tree index stores entries sorted first by the leading column, then by the next column, and so on. A condition can only bound a contiguous range of entries when it constrains the leading column (by equality or a range) or a leftmost prefix of columns. In the query filtering only on order_date and status, there is no condition on customer_id, so the index entries cannot be narrowed to a contiguous range; PostgreSQL can still read the whole index, but the index does not restrict what is read. The other three queries each constrain customer_id: the first with equality plus conditions on the following columns, the second with equality on customer_id (with status applied as a filter inside that range), and the fourth with a range on customer_id. Each therefore bounds a contiguous range of the index.

In PostgreSQL 17, a B-tree index on (customer_id, order_date, status) on table orders lets the planner perform an efficient index seek (descending directly to the matching index entries) for SELECT * FROM orders WHERE order_date = '2024-01-01' AND status = 'shipped'; because both order_date and status are key columns of the index.#

Options

Show answer

False. In PostgreSQL 17, a B-tree index on (customer_id, order_date, status) cannot give an efficient index seek for a filter on order_date and status alone, because the leading column customer_id is unconstrained. Skip scan, which works around this case, was added in PostgreSQL 18.

Why:

The statement is false for PostgreSQL 17. A B-tree descends to matching entries efficiently only when the query has equality conditions on the leading key column, here customer_id. Since customer_id is unconstrained, PostgreSQL 17 cannot seek directly to the rows with the given order_date and status. The planner may still scan the entire index and apply order_date and status as index conditions, but that is a full index scan, not an efficient seek. Including order_date and status as key columns does not change this, because their position after an unconstrained leading column prevents a directed descent. PostgreSQL 18 adds skip scan, which can handle this pattern efficiently when customer_id has few distinct values, so the claim would depend on version; the question pins version 17, where it is false.

In PostgreSQL 17 (which has no skip scan), consider a composite B-tree index on (customer_id, order_date, status). For each predicate set below, state whether the planner can use this index for an index seek (navigating directly to matching entries rather than scanning the whole index and filtering afterward), and explain why. Set I: customer_id = 42. Set II: customer_id = 42 AND order_date = '2024-01-15'. Set III: order_date = '2024-01-15' AND status = 'shipped'. Set IV: customer_id = 42 AND status = 'shipped'.#

Show answer

Seekable: Set I, Set II, and Set IV. Set I constrains only customer_id, the leftmost column, so the B-tree can seek directly to that customer's entries. Set II constrains customer_id and order_date, a contiguous leftmost prefix, so the seek narrows to one exact range. Set IV seeks on customer_id; status skips the middle column order_date, so it can only be checked against the index entries inside that customer's range and does not narrow the seek. Not seekable: Set III. It omits the leftmost column customer_id, so the B-tree has no starting point for a seek; PostgreSQL 17 must scan the whole index (or the table) and filter on order_date and status.

Why:

A composite B-tree index supports an index seek only when its leading column is constrained, because the tree is ordered by the first column and then by later columns within each prefix. Set I and Set II constrain the leftmost column (and, in Set II, the next column as a contiguous prefix), so the planner can seek to a precise range. Set IV constrains the leftmost column and skips order_date; the seek is on customer_id, and status is evaluated against entries in that range. Set III constrains only later columns, so there is no starting point in the tree and the index must be scanned in full. The version is pinned to PostgreSQL 17 because PostgreSQL 18 added skip scan, which can seek over distinct leading-column values and changes the answer for Set III.

In PostgreSQL 16, a table orders has a composite B-tree index created with CREATE INDEX idx ON orders(region, country, order_date);. Consider these queries:#

Show answer

Q1, Q2 and Q4 have a equality predicate on the leading column region (with Q2 and Q4 continuing contiguously through country and order_date), so the B-tree can descend directly to a bounded contiguous range. Q3 (only country) and Q5 (only order_date) do not constrain the leading column, so in PostgreSQL 16 they cannot be used to bound the scan. At best the planner may scan the whole index and check those predicates against index entries, which is far less efficient than a bounded range scan; whether it chooses that or a sequential scan depends on cost estimates.

Why:

Q1, Q2 and Q4 constrain the leading column region, with no gaps in Q2 and Q4, so PostgreSQL 16 can descend the B-tree and scan a bounded contiguous range. Q3 and Q5 omit region, so their predicates cannot bound the scan: matching entries are spread throughout the index. The planner may still scan the entire index and apply the predicates as index conditions, or choose a sequential scan, depending on statistics and costs, so the accurate claim is that they get no bounded range scan, not that the index is impossible to use. PostgreSQL 18 adds skip scan, which can improve this for some cases.

A table orders has a composite B-tree index created with CREATE INDEX idx ON orders(region, country, order_date); and no other indexes. Assume the database has no skip-scan optimization. Which query CANNOT use this index to narrow the search range for the indexed columns, forcing a full scan?#

Options

Show answer

A query filtering on country and order_date without region cannot use a composite B-tree index on (region, country, order_date) to narrow its search. B-tree indexes are ordered by the leading column first, so without a predicate on that prefix there is no starting point to seek from, and the engine falls back to a full scan when no skip-scan optimization exists.

Why:

A B-tree on (region, country, order_date) is ordered first by region, then by country within each region, then by date. The query that filters only on country and order_date has no predicate on the leading column, so there is no leftmost prefix to seek on, and without skip-scan support the engine must scan the whole index or table. The query filtering on region and order_date still seeks on region, which narrows the rows even though order_date cannot be used as a seek key past the skipped country column. The query with region and country equality plus a date range uses the full prefix, and the query with an IN list on region and equality on country still seeks on the leading columns.

When a single index contains every column a query references (SELECT list, WHERE predicates, JOIN keys, and ORDER BY columns), the database engine can satisfy that query entirely from the index without reading the base table. What is the standard term for an index that 'covers' a query in this way?#

Show answer

A covering index. It is an index that contains all the columns a query needs, so the engine can return results from the index alone (an index-only scan) without fetching rows from the base table or heap.

Why:

An index that contains all the columns needed to satisfy a query (columns in the SELECT list, WHERE predicates, JOIN conditions, and ORDER BY clause) lets the database return results directly from the index structure without a separate lookup into the base table (heap or clustered index). This is called a covering index, and the query is said to be covered by it. The benefit is avoiding random I/O to the base table, which is often the most expensive part of executing the query.

In PostgreSQL 16, table orders has a single B-tree index CREATE INDEX idx ON orders (customer_id, status, created_at); and no other indexes. Assume the table is large and every predicate is highly selective. Which queries can use idx to narrow the index range scanned through an equality or range condition on the leading column(s), i.e. a normal leftmost-prefix match?#

Options

Pick every one that applies.

Show answer

A composite index on (customer_id, status, created_at) can narrow its scan only when the query filters on the leading column customer_id. That works for customer_id + status, for customer_id + created_at (where created_at is checked as a filter), and for all three columns. B-tree entries are sorted by the leftmost column first, so in PostgreSQL 16 filters on only status and/or created_at can't use the index to bound the scan.

Why:

A composite B-tree index is sorted by customer_id first, then status, then created_at, so a range scan can only be bounded by conditions that start at the leading column. customer_id = 42 AND status = 'paid' matches the first two columns. customer_id = 42 AND created_at > ... bounds the scan by customer_id, and the created_at condition is then checked as an index filter because status is skipped. Using all three columns, with equality on the first two and a range on the third, is the ideal leftmost-prefix match. Queries that leave out customer_id have no leading-column condition. PostgreSQL 16 has no skip scan (that arrives in version 18), so those queries can't narrow the scan and could only read the whole index.

What is the primary trade-off of adding more indexes to a heavily-written table?#

Options

Show answer

The trade-off is that every INSERT, UPDATE, and DELETE must also maintain each affected index, slowing writes and consuming extra storage. Indexes speed reads, but each one is an additional structure the database keeps consistent on every write, adding disk and cache pressure. The skill is indexing the columns that selective queries actually filter or join on, not indexing everything.

Why:

Indexes speed reads but every index is an extra structure the database must keep consistent on every write, so more indexes mean slower INSERT/UPDATE/DELETE and more disk and cache pressure. The skill is indexing the columns that selective queries actually filter or join on, not indexing everything.

You have a composite B-tree index on (tenant_id, created_at) in that column order. Which queries can use the index to seek rather than scan the whole table?#

Options

Pick every one that applies.

Show answer

Filtering on tenant_id alone, on tenant_id plus a created_at range, and an ORDER BY tenant_id, created_at after a tenant_id filter can all seek — but filtering on created_at alone cannot. A composite index is usable only on a left-to-right prefix of its columns, so skipping the leading tenant_id defeats it. This is the classic left-prefix rule.

Why:

A composite index is usable only on a left-to-right prefix of its columns. Filtering on the leading tenant_id, or on tenant_id plus a range on the next column created_at, seeks efficiently, and the index's sort order also satisfies the matching ORDER BY after WHERE tenant_id = 42. Filtering on created_at alone (no tenant_id predicate) skips the leading column, so the index can't seek — this is the classic left-prefix rule.

Walk through how a database locates rows for WHERE email = '[email protected]' using a B-tree index on email, and why that beats scanning the table.#

Show answer

The B-tree is a balanced, sorted tree. The engine starts at the root and at each level compares the search key against the separator keys to pick the single child to descend into, walking root → branch → leaf in O(log n) steps instead of reading every row. The matching leaf entry holds a pointer (row id / primary key) to the actual row, which the engine then fetches. A full scan would read all N rows; the index turns that into a handful of page reads down the tree's height.

Why:

A B-tree index is sorted and balanced, so a lookup descends from the root through internal nodes to a leaf in logarithmic time, comparing the key at each level to choose one child. The leaf holds (or points to) the row location, so the engine reads only the few pages on that path instead of the whole table. The cost is O(log n) page reads versus O(n) for a sequential scan — the reason an equality or range predicate on an indexed column is so much faster.

What is a covering index, and why is it faster than a normal index lookup?#

Show answer

A covering index contains every column a query needs (in its key or, in PostgreSQL, an INCLUDE clause), so the query is answered entirely from the index. This gives an index-only scan that skips the extra heap/table fetch a normal index lookup needs to retrieve the remaining columns.

Why:

Normally an index points back to the table row to fetch columns it doesn't store, costing an extra read per match. When the index already holds all referenced columns, the engine does an index-only scan and never touches the table — a big win for hot, selective queries.

In PostgreSQL 17 (which has no B-tree skip scan), you have a 50-million-row table orders with the composite B-tree index CREATE INDEX idx_orders ON orders(customer_id, status, created_at). Consider these four WHERE clauses, each used in a query on orders:#

Show answer

Ranking: Q1, then Q2, then Q3, then Q4. Q1 seeks on 3 columns: the two equality predicates on customer_id and status fix a single position in the B-tree, and the range on created_at then bounds the scan within that narrow slice. Q2 seeks on 1 column: customer_id is an equality, but status has no predicate, so created_at cannot be used as a seek key and is only checked against each entry under customer_id = 42. Q3 seeks on 1 column: the range on customer_id bounds the scan, but a range on the first column stops further seeking, so status is only checked as a filter across the whole customer_id > 42 range. This is a wider scan than Q2 because the leading predicate is a range rather than an equality. Q4 seeks on 0 columns: the leftmost column customer_id has no predicate, so the B-tree has no entry point and the index cannot be navigated; without skip scan (PostgreSQL 17) the planner would use a different access path. The leftmost-prefix rule requires each column used for seeking to be preceded by equality predicates, and a range predicate ends seeking for the columns after it.

Why:

A composite B-tree can seek on a column only if every column to its left has an equality predicate, with the first column being constrained at all. Q1 has equalities on customer_id and status followed by a range on created_at, so all three columns are seek keys and the scan covers only the matching slice. Q2 has an equality on customer_id, but the missing status predicate means created_at is not anchored and is only a post-visit filter, so the seek count is 1. Q3 has a range on the leading column, which bounds the scan but prevents status from being a seek key, so the seek count is 1 and the scanned range is wider than Q2's. Q4 has no predicate on the leading column, so there is no seek entry point and the seek count is 0; PostgreSQL 17 has no skip scan to work around this, though PostgreSQL 18 can sometimes do so.

In PostgreSQL, a B-tree index exists on orders(created_at), where created_at is of type timestamp. In one or two sentences, explain why the predicate WHERE date_trunc('day', created_at) = '2024-01-01' cannot be turned into a direct index range seek on this index, and what the index stores that the predicate does not match.#

Show answer

The B-tree stores the raw created_at values in sorted order, keyed by the bare column. The predicate compares the output of date_trunc() applied to each row, which is not the stored key, so the planner has no starting key to descend to and cannot seek a contiguous key range. It would have to evaluate the function on every row, so the index cannot be used for a range seek unless a matching expression index on date_trunc('day', created_at) exists.

Why:

A B-tree index on created_at stores the raw timestamp values in sorted order, so a seek requires a predicate that compares the bare column against a value. Wrapping the column in date_trunc('day', ...) makes the compared value a derived result that is not the stored key, so the planner cannot descend the tree to a starting point or identify a contiguous key range to scan. The only fix that preserves an index seek is an expression index on the same expression, or rewriting the predicate with range operators on the bare column. This question has a single defensible answer because it asks for the mechanism, not for a specific clause that the planner may or may not choose based on statistics.

Consider the following schema and index in PostgreSQL 16 (heap-table architecture with separate indexes, not a clustered/InnoDB-style store of the PK inside every secondary index):<br><br><code><br>CREATE TABLE t (<br> id integer PRIMARY KEY,<br> status text,<br> amount numeric<br>);<br><br>CREATE INDEX idx_status_amount ON t (status) INCLUDE (amount);<br></code><br><br>Assume every heap page of <code>t</code> is currently marked all-visible in the visibility map. Which of the following queries can the PostgreSQL planner satisfy using an Index Only Scan with <code>idx_status_amount</code>—that is, no heap fetches at execution time?<br><br>a) <code>SELECT amount FROM t WHERE status = 'active'</code><br><br>b) <code>SELECT amount FROM t WHERE amount > 1000 AND id > 500</code><br><br>c) <code>SELECT id FROM t WHERE status = 'active'</code><br><br>d) <code>SELECT amount FROM t WHERE status = 'active' AND amount > 1000</code>#

Show answer

Options a and d can be satisfied as Index Only Scans; b and c cannot.

Why:

idx_status_amount has status as its sole B-tree key column and amount as a non-key (INCLUDE) column. An Index Only Scan requires every column referenced in the target list and WHERE clause to be available from the index entry, and only key columns can drive the B-tree descent.

a) SELECT amount … WHERE status = 'active' — status is a key column so the B-tree navigates to the correct leaf range; amount is an INCLUDE column whose value is stored in the leaf tuple and is available for output. With all-visible pages no heap fetch is needed. ✓

b) SELECT amount … WHERE amount > 1000 AND id > 500 — amount is an INCLUDE (non-key) column and is available in the index leaf, but id is the table's primary key and is not stored in idx_status_amount (only key columns, INCLUDE columns, and the heap TID are). Because the WHERE clause references id, the executor must fetch the heap tuple to evaluate id > 500, so no Index Only Scan is possible. ✗

c) SELECT id … WHERE status = 'active' — PostgreSQL secondary B-tree indexes store only their key columns, their INCLUDE columns, and the heap TID (physical page+offset). The primary-key column id is not stored in idx_status_amount, so the executor must fetch the heap tuple to read id. ✗ (This contrasts with MySQL/InnoDB, where the clustered PK is embedded in every secondary index and this query would be index-only.)

d) SELECT amount … WHERE status = 'active' AND amount > 1000 — status drives the B-tree descent to the 'active' range; amount > 1000 is evaluated as a filter on the INCLUDE column value stored in the leaf tuple, not as a scan key. Because every referenced column (status, amount) lives in the index entry and all pages are all-visible, the filter is applied without any heap access. ✓

In PostgreSQL's cost-based optimizer, the per-column correlation statistic (from pg_stats, range -1.0 to 1.0) reflects how closely the physical heap order matches the index's key order. For a single-column B-tree index, which statement accurately describes its effect on the planner's choice between a plain Index Scan and a Bitmap Heap Scan?#

Options

Show answer

In PostgreSQL's planner, the correlation statistic affects only the estimated cost of a plain Index Scan: a value near 1 or -1 lowers its heap I/O estimate because matching rows sit in nearly index order. The Bitmap Heap Scan cost estimate does not use correlation, so a low value does not penalize it through that statistic.

Why:

Correlation only enters the cost of a plain Index Scan. In cost_index() (via the B-tree cost estimator), the planner interpolates the heap page-fetch cost between a best case (sequential, when the heap is in index order) and a worst case (one random fetch per tuple) using the squared correlation. Because it is squared, a correlation of -1.0 is treated exactly like 1.0, so a high-magnitude value of either sign lowers the estimated heap I/O. The Bitmap Heap Scan estimate (cost_bitmap_heap_scan()) instead estimates distinct heap pages fetched from the selectivity and table size, and does not use correlation. Therefore a near-0 correlation does not make the plain index scan cheaper, a low correlation does not raise the bitmap estimate, and a negative correlation is not a worst case.

Sources

The official documentation these questions are checked against:

Related interview questions

Job market

See databases-sql salaries and hiring demand from live job postings.

The other 48 questions

This page shows 25 and marks what you pick. That's as far as a page can go. A free account opens the other 48 and keeps every answer. What you miss comes back until it's right: after a day, then at longer gaps.

Start with this topic

Free · the whole bank · 100 answers per 30 days, each marked with written feedback

Interview booked? Practise the job ad instead

What moved, monthly

One email a month when the bulletin comes out: what moved in the markets we track, and the new question topics we published. Confirm your address to join. Unsubscribe any time.