Advanced Interview Questions

What are the ACID properties?

  • Atomicity: all statements in a transaction succeed or none do.
  • Consistency: constraints hold before and after every transaction.
  • Isolation: concurrent transactions do not see each other's uncommitted changes.
  • Durability: committed data survives crashes (write-ahead log).

Explain isolation levels and the anomalies they prevent.

LevelDirty readNon-repeatable readPhantom
Read Uncommittedyesyesyes
Read Committednoyesyes
Repeatable Readnonoyes (no in InnoDB/PostgreSQL)
Serializablenonono

What is a deadlock and how do you avoid it?

Two transactions each hold a lock the other needs, so neither can continue; the database aborts one. Avoid it by accessing objects in a consistent order, keeping transactions short, indexing to lock fewer rows, and retrying the victim transaction.

Optimistic vs pessimistic locking?

Pessimistic: lock the row when reading (SELECT ... FOR UPDATE), so others wait. Good for high contention. Optimistic: do not lock; on update check a version column (WHERE Version = :v) and retry if 0 rows changed. Good when conflicts are rare.

Difference between ROW_NUMBER, RANK and DENSE_RANK?

For values 100, 90, 90, 80: ROW_NUMBER → 1, 2, 3, 4; RANK → 1, 2, 2, 4; DENSE_RANK → 1, 2, 2, 3.

GROUP BY vs window functions?

GROUP BY collapses each group to one row. A window function (... OVER (PARTITION BY ...)) computes the same aggregate but keeps every row, so you can show detail and totals side by side.

What is a recursive CTE? Give a use case.

A CTE that references itself: an anchor query plus a recursive part joined by UNION ALL, repeated until no new rows appear. Uses: org charts, category trees, bill of materials, generating date series.

How do you remove duplicate rows but keep one?

Example

Number the duplicates and delete all but the first:

INSERT INTO Customers VALUES (9, 'Delta Mart', 'Priya Nair', 'Bengaluru', 'India');

DELETE FROM Customers
WHERE CustomerID IN (
  SELECT CustomerID FROM (
    SELECT CustomerID,
           ROW_NUMBER() OVER (PARTITION BY CustomerName, City ORDER BY CustomerID) AS rn
    FROM Customers
  ) t WHERE rn > 1
);

SELECT CustomerID, CustomerName FROM Customers WHERE CustomerName = 'Delta Mart';

Result: 1 row

CustomerIDCustomerName
4Delta Mart

A query is slow. How do you troubleshoot it?

  1. Reproduce and measure it with realistic data.
  2. Read the execution plan (EXPLAIN ANALYZE): look for scans, bad estimates, large sorts.
  3. Check indexes on WHERE/JOIN/ORDER BY columns and whether conditions are sargable.
  4. Update statistics; check for locking/blocking.
  5. Rewrite (fewer columns, early filtering, joins instead of per-row subqueries), then re-measure.

What is the N+1 query problem?

Code runs one query to load a list (N items) and then one more query per item: N+1 round trips. Fix it by loading related data in one query with a JOIN or WHERE id IN (...), or eager loading in the ORM.

When would you denormalize?

When measured read performance is not good enough and the joins/aggregations are the bottleneck: reporting tables, cached totals, data warehouses. Accept the cost of keeping redundant data in sync (triggers, jobs) and document it.

How do you prevent SQL injection?

Always use parameterised queries / prepared statements, never string concatenation of user input. Also: least-privilege database accounts, allow-lists for dynamic identifiers, and hiding raw database errors from users.

OLTP vs OLAP?

OLTPOLAP
Purposeday-to-day transactionsanalysis and reporting
Queriesshort, many, read/write single rowslong, complex, read-mostly aggregates
Schemanormalized (3NF)denormalized (star/snowflake)
Exampleplacing an ordermonthly revenue by region