DEV Community

Cover image for 40 SQL Interview Questions and Answers (2026)
Dev Encyclopedia
Dev Encyclopedia

Posted on • Originally published at devencyclopedia.com

40 SQL Interview Questions and Answers (2026)

SQL shows up in almost every backend and full-stack interview. Not just basic SELECT queries — interviewers want to know if you understand what's happening underneath.

This guide covers 40 questions across 6 categories, from fundamentals to the kind of advanced questions that separate mid-level from senior candidates.

What's covered

  • Fundamentals: SQL sublanguages, NULL behavior, DELETE vs TRUNCATE vs DROP, WHERE vs HAVING, query execution order
  • Joins and relationships: all JOIN types, self-joins, anti-join pattern, correlated subqueries, N+1 problem
  • Indexes and performance: B-Tree indexes, clustered vs non-clustered, composite indexes, covering indexes, EXPLAIN ANALYZE
  • Transactions and concurrency: ACID, isolation levels, deadlocks, MVCC, optimistic vs pessimistic locking
  • Advanced SQL: window functions, ROW_NUMBER vs RANK vs DENSE_RANK, LAG/LEAD, CTEs, recursive CTEs, materialized views
  • MySQL vs PostgreSQL: key differences, upsert patterns, JSONB, classic query challenges

A few questions worth knowing cold

Why does NULL = NULL return NULL and not TRUE?
SQL uses three-valued logic: TRUE, FALSE, and NULL (unknown). A comparison with NULL always returns NULL. Use IS NULL / IS NOT NULL instead.

What's the N+1 query problem?
It happens when you fetch a list of records, then run a separate query for each one. Fix it with a JOIN or eager loading at the ORM level.

When should you NOT add an index?
On small tables, columns with low cardinality (like a boolean), or tables with very high write volume where index maintenance cost outweighs read gains.

What's the difference between RANK and DENSE_RANK?
Both assign rankings with ties, but RANK skips numbers after a tie (1, 1, 3) while DENSE_RANK does not (1, 1, 2).


Full answers, code examples, and EXPLAIN output walkthroughs for all 40 questions are on Dev Encyclopedia.

👉 https://devencyclopedia.com/blog/sql-database-interview-questions

Top comments (0)