Skip to content
Preptima

Databases & SQL interview questions and answers

Query-writing rounds and the storage-engine reasoning behind why a query is slow, for backend, data, and DBA roles alike.

28 published across 8 topics.

Joins, window functions, aggregation, and the analytical queries asked in a shared editor.

mediumCodingDesign

You need a work queue with priorities and you are told to build it in Postgres, not Kafka. What does the dequeue statement have to do, and what happens when ten workers run it at once?

The dequeue has to pick the highest-priority ready row, lock it, and mark it claimed in one statement, using FOR UPDATE SKIP LOCKED so ten workers each take a different row instead of queueing behind the same one. The lock only lasts as long as the transaction, so the claim must be written or the transaction held.

6 minmid, senior
mediumCodingConcept

Find me every customer with no order in the last thirty days, then tell me which ways of writing that get NULLs wrong.

Use NOT EXISTS for customers with no recent order because it expresses an anti-join and avoids NULL traps. NOT IN can return no rows if the subquery contains NULL, and LEFT JOIN becomes wrong if filters are placed in WHERE. Use this SQL answer to show the decision, trade-off, and evidence rather than a memorised definition. It also connects null semantics to the point an interviewer is testing.

4 minentry, mid, senior
mediumCodingConcept

Write a query returning the second-highest salary in each department, then tell me what it does when two people tie for the top.

To get the second highest salary per department in SQL, rank employees inside each department with DENSE_RANK and filter rank two. DENSE_RANK handles ties where ROW_NUMBER or a global MAX gives the wrong result. Use this window functions answer to show the decision, trade-off, and evidence rather than a memorised definition. It also connects sql queries to the point an interviewer is testing.

3 minentry, mid, senior
All 4 in SQL Query Writing

Indexing

4 questions

B-tree mechanics, composite-column order, covering indexes, and why an index goes unused.

hardConceptScenarioDesign

A table has forty million rows, thirty-five million of them soft-deleted, and every query for active rows has got slower. Would a partial index help, and what would you have to be careful about?

A partial index only contains rows matching its predicate, so indexing WHERE deleted_at IS NULL stores five million entries instead of forty million - smaller, shallower, and far likelier to stay cached. The catch is the planner only uses it when it can prove the query predicate implies the index predicate, and it does nothing about dead rows still in the heap.

5 minmid, senior, staff
All 4 in Indexing

ACID guarantees, isolation levels, the anomalies each permits, and locking versus MVCC.

hardConceptScenario

Two doctors can go off call only if at least one remains. Both check the rule at the same moment, both see two doctors on call, and both go off call. What isolation problem is this, and what actually fixes it?

This is write skew: two transactions read the same set, each writes a different row, and the pair commits a state neither would have produced alone. No row-level constraint can catch it because the invariant is a property of the set, so the fix is either serializable isolation, a lock on something both transactions must touch, or remodelling the invariant onto a single row.

6 minmid, senior, staff
hardScenarioDesign

A user submits the form twice and you get two identical orders. Why did the check-then-insert not prevent it, and how do you fix it in the database?

Two transactions can each run the SELECT before either commits, so neither sees the other and both inserts succeed. The fix is a serialisation point the database owns - a unique index on a natural or client-supplied key - plus a defined answer for whoever loses the race. It also connects idempotency to the point an interviewer is testing.

6 minmid, senior, staff
All 4 in Transactions & Isolation

Reading execution plans, statistics and cardinality estimation, and diagnosing real regressions.

mediumConcept

Before you tune anything, how do you decide which queries are worth your time?

Rank by the total time a statement shape consumes across a measured window rather than by the slowest single execution, then check what that time was spent waiting on before touching the SQL. The cheap statement running ten thousand times a minute usually outranks the ten-second report.

4 minmid, senior, staff
mediumConceptScenario

Walk me through how you read a PostgreSQL execution plan.

Read the plan tree innermost-node-first, remember that EXPLAIN only estimates while EXPLAIN ANALYZE executes, multiply each node's actual rows by its loops, and treat a large gap between estimated and actual rows as the primary finding.

5 minmid, senior, staff
All 3 in Query Optimisation

Data Modelling

3 questions

Normalisation and deliberate denormalisation, keys, constraints, and schema evolution under load.

All 3 in Data Modelling

NoSQL Stores

4 questions

Document, key-value, wide-column, and graph stores, with honest selection criteria.

All 4 in NoSQL Stores

PostgreSQL Internals

2 questions

MVCC and vacuum, WAL, connection handling, and the operational depth senior roles expect.

DBA & Operations

4 questions

Backup and recovery, replication and failover, upgrades, capacity planning, and incident response.

hardScenarioDesign

You are moving a sharded datastore to Cassandra with no maintenance window. How do you cut over, and how do you know both copies agree before you do?

Replicate from the source's change log rather than dual-writing from application code, backfill with each row's original write timestamp so history can never overwrite live data, then run continuous range checksums and shadow reads until divergence is understood rather than merely small. The read flip and the write flip are separate steps, each reversible.

6 minsenior, staff, lead
All 4 in DBA & Operations