Software Engineer Interview Question

How does a database index work, and when does adding one make things worse?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

An index is a separate sorted structure, usually a B tree, mapping column values to row locations so the engine can seek instead of scanning. It speeds up reads that filter or sort on those columns. It costs you on writes, since every insert, update and delete has to maintain it, and it costs storage. Low selectivity columns and indexes nothing queries are usually a net loss.

Why interviewers ask this

Indexes are the highest leverage tool available to most engineers and the easiest one to cargo cult. The interviewer is checking that you understand the write side of the trade, that you know selectivity determines whether the planner will even use the index, and that you can explain why indexing every column is not a strategy. It also opens the door to query planning.

How to structure your answer

  • Describe the index as a sorted lookup structure with row pointers.
  • Explain the read benefit in terms of seeks versus scans.
  • Name the write and storage costs explicitly.
  • Give a case where the planner ignores the index anyway.

Example answer

Spoken example, first person

An index is essentially a sorted copy of one or more columns with pointers back to the rows, usually a B tree, so the engine can binary search instead of reading every page. That turns a filter on a big table from a sequential scan into a handful of page reads. The cost is that every write has to update the index too, so a table with eight indexes pays eight times on insert, and bulk loads slow right down. The other thing I watch is selectivity. If a column is a boolean that is true for ninety percent of rows, the planner will usually just scan, because random access to that many rows costs more than reading sequentially. I had a case where somebody added an index on a status column with three values and nothing improved at all. What actually fixed it was a composite index on tenant id and created at, matching both the filter and the sort, so the database could seek and then read in order without a separate sort step.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • Why does column order matter in a composite index?
  • What does a covering index give you?
  • How would you tell whether the planner is using your index?

Related software engineer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot