Senior interviewers probe performance tuning, concurrency, distributed systems, and real-world edge cases:
Q16. Differentiate between ROW_NUMBER(), RANK(), and DENSE_RANK() window functions.
Experienced • 3-5+ Yrs
ROW_NUMBER() assigns a unique sequential integer to each row within a partition. RANK() assigns the same rank to identical values but skips subsequent ranks (e.g., 1, 2, 2, 4). DENSE_RANK() assigns the same rank to ties without skipping numbers (e.g., 1, 2, 2, 3), making it ideal for calculating Nth highest values.
Q17. Explain CTEs (Common Table Expressions) and recursive CTEs for hierarchical queries.
Experienced • 3-5+ Yrs
A CTE (WITH cte_name AS (...)) defines a temporary named result set within a single SELECT/INSERT/UPDATE/DELETE. Recursive CTEs consist of an Anchor member and a Recursive member combined with UNION ALL, terminating when the recursive step returns an empty set. They are used to traverse organizational charts, bill of materials, and graph networks.
Q18. How does B-Tree indexing work and what is the Leftmost Prefix Rule for composite indexes?
Experienced • 3-5+ Yrs
B-Trees maintain balanced hierarchical search trees where leaf nodes store sorted key values and row pointers, enabling O(log N) lookups. When an index spans multiple columns (A, B, C), the Leftmost Prefix Rule dictates that queries can utilize the index only if conditions filter from the leftmost column sequentially (e.g., A, or A and B). Filtering on only B and C bypasses the index.
Q19. How do you read and interpret a SQL Execution Plan (EXPLAIN ANALYZE)?
Experienced • 3-5+ Yrs
EXPLAIN shows the query planner's estimated cost, while EXPLAIN ANALYZE executes the query and reports actual runtime statistics. Key metrics to inspect include: Table Scans (Seq Scan vs Index Scan), Join algorithms (Nested Loop, Hash Join, Merge Join), Actual Rows vs Estimated Rows (identifying stale statistics), and Buffer/Cache hit ratios.
Q20. What are Transaction Isolation Levels and what concurrency anomalies do they prevent?
Experienced • 3-5+ Yrs
SQL-92 defines 4 isolation levels: Read Uncommitted (allows dirty reads), Read Committed (prevents dirty reads, allows non-repeatable reads), Repeatable Read (prevents dirty & non-repeatable reads, allows phantom reads), and Serializable (strict serial execution, prevents all anomalies). Higher isolation increases locking overhead and deadlock risks.
Q21. Explain Dirty Read, Non-Repeatable Read, and Phantom Read anomalies.
Experienced • 3-5+ Yrs
A Dirty Read occurs when Transaction A reads uncommitted data modified by Transaction B (which may rollback). A Non-Repeatable Read occurs when Transaction A re-reads a row and finds data modified by Transaction B. A Phantom Read occurs when Transaction A re-runs a range query and discovers newly inserted/deleted rows committed by Transaction B.
Q22. What causes SQL deadlocks and how do you prevent and resolve them in production?
Experienced • 3-5+ Yrs
Deadlocks occur when two or more transactions cyclically wait for locks held by each other. To prevent deadlocks: access database objects in a consistent global order across all application code, keep transactions short, use lower isolation levels when appropriate, index foreign keys, and utilize row-level locking (SELECT ... FOR UPDATE).
Q23. What is the difference between Optimistic and Pessimistic concurrency control?
Experienced • 3-5+ Yrs
Pessimistic locking assumes conflicts will occur and explicitly locks rows (SELECT FOR UPDATE) preventing others from modifying data until commit. Optimistic locking assumes conflicts are rare; transactions proceed without locks, validating record version numbers or timestamps before committing and aborting/retrying if another update occurred in between.
Q24. When should a database schema be denormalized?
Experienced • 3-5+ Yrs
Denormalization intentionally introduces redundancy into a normalized schema to optimize read performance in OLAP (Analytical) workloads or data warehouses. It avoids expensive multi-table joins, pre-aggregates metrics, and simplifies reporting queries at the cost of increased storage and complex update pipelines.
Q25. Explain table partitioning strategies (Range, List, Hash) and Partition Pruning.
Experienced • 3-5+ Yrs
Partitioning splits a massive table into smaller physical chunks while presenting a unified logical table. Range partitioning divides rows by value boundaries (e.g., dates by year). List partitioning splits by explicit category values (e.g., country codes). Hash partitioning distributes evenly across N partitions via hash algorithms. Partition Pruning allows the query optimizer to skip scanning irrelevant partitions entirely.
Q26. What is Index Fragmentation and how do you resolve it?
Experienced • 3-5+ Yrs
Fragmentation occurs when frequent INSERTs, UPDATEs, and DELETEs cause out-of-order page allocations and empty space on B-Tree index pages, degrading sequential I/O. In PostgreSQL, run VACUUM FULL or REINDEX. In SQL Server, REORGANIZE indexes when fragmentation is between 10%-30%, and REBUILD when fragmentation exceeds 30%.
Q27. How do Window Functions like LEAD() and LAG() work for time-series and delta calculations?
Experienced • 3-5+ Yrs
LEAD(col, offset) retrieves data from subsequent rows within a partition without a self-join, while LAG(col, offset) accesses data from preceding rows. They are heavily utilized in financial and time-series analytics to calculate day-over-day growth, elapsed time between events, and session activity gaps.
Q28. Explain the difference between Nested Loop, Hash Join, and Merge Join in query execution.
Experienced • 3-5+ Yrs
Nested Loop Join iterates outer rows and probes the inner table; it excels when one dataset is tiny and the other has an index. Hash Join builds an in-memory hash table of the smaller dataset and streams the larger table; it excels for large unsorted datasets. Merge Join sorts both datasets on join keys and merges simultaneously; it is fastest when inputs are already indexed or presorted.
Q29. How do you handle slow-running queries caused by parameter sniffing?
Experienced • 3-5+ Yrs
Parameter sniffing occurs when a database compiles an execution plan tailored to parameters passed on initial execution, which proves disastrous for skewed data distributions on subsequent runs. Mitigate by using query hints (OPTIMIZE FOR UNKNOWN / RECOMPILE), utilizing local variables inside stored procedures, or splitting logic into dedicated procedures.
Q30. What are the differences between Row-Store and Column-Store database architectures?
Experienced • 3-5+ Yrs
Row-store databases (Postgres, MySQL) store entire tuples contiguously on data pages, optimal for OLTP write/read of full individual records. Column-store databases (Snowflake, ClickHouse, Redshift) store individual columns contiguously, enabling high compression, vectorized CPU processing, and scanning only queried columns for massive analytical aggregations.