Practice Discrete Math

Relations / Sql Indexing Cardinality

Least You Need to Know: Indexes, Selectivity, and Cardinality Intuition

Index and query-plan interview questions are mostly about search-space pruning. Good indexes cut down candidate rows quickly; bad selectivity, tiny tables, or the wrong leading columns can erase that advantage.

The least you need to know

Key notation

selectivity fraction of rows a predicate keeps
cardinality estimated number of rows in an intermediate result
(a, b) composite index whose leading column is a

Tiny worked example

  • If a million-row users table has an index on `email`, an equality lookup by one exact email is highly selective.
  • If the same table has an index on `is_active` with values only `true` or `false`, each lookup may still touch a huge fraction of rows.
  • In a composite index on `(country, created_at)`, queries filtering by `country` can often use the index directly, while filtering only by `created_at` may not use the leading prefix efficiently.
  • Query planners compare such estimates when choosing scan and join strategies.

Common mistakes

How to recognize this kind of problem

Next recommended lesson

Continue through this topic with Least You Need to Know: SQL Joins as Relation Operations.

Least You Need to Know: SQL Joins as Relation Operations

Related lessons

Keep going with nearby lessons in the same topic.

More ways to explore

Start practice