Detailed explanation
A clustered index stores the table’s rows at the leaf level of its B+ tree; a non-clustered index is a separate B+ tree containing indexed keys and row locators. Both use upper levels to navigate to matching leaf entries. Clustering determines the logical ordering of data pages by key, not guaranteed contiguous placement on disk. A table can have only one clustered index because its rows have one leaf-level organisation, but it can have multiple non-clustered indexes, subject to engine limits and maintenance costs.
The details depend on the database engine. In SQL Server, a PRIMARY KEY normally creates a clustered index if no clustered index already exists and NONCLUSTERED is not specified. A table without a clustered index is a heap. Non-clustered index locators are clustered keys for clustered tables and row identifiers for heaps. In MySQL/InnoDB, every table has a clustered index: the primary key, otherwise the first suitable UNIQUE index whose columns are all NOT NULL, otherwise a generated hidden row ID. InnoDB secondary indexes contain the clustered key as the row locator.
Clustering is useful for range predicates because nearby key values occupy logically neighbouring leaf pages, supporting efficient traversal and read-ahead. Reaching a clustered leaf gives access to the base row without a separate index-to-table lookup, although off-row values may require additional reads. A non-clustered index can find qualifying entries quickly, but retrieving missing columns requires key lookups in SQL Server or clustered-index lookups in InnoDB. When many rows qualify, repeated lookups may cost more than scanning, so the optimiser can choose a scan instead.
For example, with orders clustered on order_id, a non-clustered index on customer_id helps locate one customer’s orders. In SQL Server, adding INCLUDE (order_date, total) covers a query selecting those columns and avoids base-row lookups. Index design also affects writes: every relevant index must be maintained. Wide clustered keys enlarge secondary row locators, while random insertion patterns can increase page splits and fragmentation. Narrow, stable, increasing keys are often useful, but increasing keys can create hot-page contention. Interviewers expect these trade-offs and the reminder that only ORDER BY guarantees result ordering.
Key points to remember
- Clustered leaves hold base rows; non-clustered leaves hold index entries and row locators.
- A table has at most one clustered index but can have multiple non-clustered indexes.
- SQL Server supports heaps; InnoDB always organises table data through a clustered index.
- Covering indexes avoid base-row lookups by supplying every column needed by a query.
- Choose keys using read patterns, key width, insertion behaviour, and write-maintenance costs.
Common mistakes candidates make
- Claiming clustered pages are necessarily contiguous on disk or that queries return sorted results without ORDER BY.
- Assuming a primary key is always clustered across all database engines and configurations.
- Saying every non-clustered access requires exactly one extra physical read, ignoring covering indexes, caching, and tree traversal.
Likely follow-up questions
How does a covering index differ from a composite index?
A composite index has multiple key columns, which participate in its ordering. Covering describes whether an index supplies everything a particular query needs; SQL Server INCLUDE columns can provide coverage without becoming search-key columns.
Why might the optimiser ignore a non-clustered index?
If many rows qualify, traversing the index and performing repeated base-row lookups may cost more than scanning. The choice depends on estimated selectivity, statistics, coverage, and the engine’s cost model, not a universal percentage threshold.
Should the primary key always be the clustered key?
Not necessarily: a primary key enforces row identity, while clustering is a storage-design decision. SQL Server lets you choose different keys for these roles; InnoDB uses the declared primary key for clustering.
Interview tip: Start with what each index stores at its leaves, then explain one lookup example and qualify the behaviour by database engine.
