SQL · Queries that survive scale

What is index selectivity and why does it matter?

HardselectivitystatisticsindexesUpdated Oct 2026
Quiz this track

Short answer

Selectivity measures how many distinct values a column has relative to row count. Highly selective columns are better index candidates because a lookup returns a small fraction of the table.

An index is most useful when it eliminates most rows. If a predicate matches 40 percent of a table, reading the index plus many table pages can cost more than a sequential scan. The planner uses statistics to estimate this before choosing a plan.

sql
SELECT
  COUNT(*) AS total_rows,
  COUNT(DISTINCT status) AS distinct_status,
  COUNT(DISTINCT user_id) AS distinct_users
FROM orders;
Compare distinct counts with total rows. A status column with three values is poorly selective; user_id is usually highly selective.

How to use the idea

  • High selectivity: email, uuid, user_id, or a narrow date range
  • Low selectivity: boolean flags, status with few values, or similar enums
  • A low-selectivity column can work in a partial index that targets a rare value
  • Composite indexes can combine a low-selectivity equality column with a selective one
  • Stale statistics produce bad estimates, so run ANALYZE after large data changes
  • Histograms and extended statistics help when columns are correlated
sql
CREATE INDEX idx_orders_pending
  ON orders (created_at)
  WHERE status = 'pending';
A partial index stays small and useful when pending orders are a tiny fraction of the table.

Selectivity is not only about distinct values. The distribution matters: a column can have many distinct values but still be dominated by one value. The planner's histogram captures that skew, which is why fresh statistics are important.

Do not judge an index only by its definition. Measure the actual rows returned and the plan on production-like data. A selective index can still be ignored if the predicate is not written in a sargable way.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1How do database statistics influence index choice?
  2. 2When is a partial index better than a full index?
  3. 3Why can a low-selectivity column still be useful in a composite index?
  4. 4What makes a predicate non-sargable?

Say it in the interview

Selectivity is the fraction of rows a predicate returns, or how many distinct values a column has relative to the row count. Highly selective predicates benefit most from an index because they eliminate most of the table. Low-selectivity columns often cause the planner to choose a sequential scan. I use partial indexes for rare values, keep statistics fresh, and verify with real row counts rather than guessing.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live SQL mock interview and get scored feedback on depth, clarity and confidence.