🗄️ Database Interview Questions
Master database interviews with questions on SQL, NoSQL, optimization, indexing, and database design
Indexing, ACID properties, SQL vs NoSQL, normalization, and query optimization
15-Minute Database Cheatsheet
Quick reference for last-minute interview preparation
🗄️ SQL vs NoSQL
⚡ ACID Properties
📇 Indexing
📐 Normalization
🚀 Query Optimization
🔒 Transactions & Locking
📝 Essential SQL Patterns
⚠️ Common Interview Topics
Indexes improve query performance by creating data structures that allow faster lookups. However, they slow down writes and consume additional storage.
Common Index Types:
- B-Tree: Default index, good for equality and range queries
- Hash: Fast for equality comparisons, not for ranges
- GiST/GIN: Full-text search and complex data types
- Partial: Index on subset of rows
- Composite: Index on multiple columns
ACID guarantees reliability of database transactions:
- Atomicity: All operations succeed or all fail (no partial updates)
- Consistency: Data remains valid according to rules/constraints
- Isolation: Concurrent transactions don't interfere with each other
- Durability: Committed data persists even after system failure
SQL (Relational) and NoSQL (Non-relational) databases have different strengths:
SQL (PostgreSQL, MySQL)
- Structured data, fixed schema
- ACID transactions
- Complex queries, JOINs
- Vertical scaling
- Strong consistency
NoSQL (MongoDB, Redis)
- Flexible/dynamic schema
- BASE (Basically Available, Soft state, Eventual consistency)
- Denormalized data
- Horizontal scaling
- High performance for specific use cases
Normalization: Organizing data to reduce redundancy and improve integrity.Denormalization: Intentionally introducing redundancy for performance.
Normal Forms:
- 1NF: Atomic values, no repeating groups
- 2NF: 1NF + no partial dependencies
- 3NF: 2NF + no transitive dependencies
- BCNF: 3NF + stricter rules
Query optimization improves performance by reducing execution time and resource usage.
Window functions perform calculations across rows related to the current row without grouping them into a single output row. They enable analytical queries like running totals, rankings, and moving averages.
Both stored procedures and functions are stored SQL code, but they have different purposes and capabilities:
Stored Procedures
- Can modify database state
- Can have multiple return values (OUT parameters)
- Can return result sets
- Can use transactions
- Cannot be used in SELECT
Functions
- Should not modify database state
- Must return a single value
- Can be used in SELECT, WHERE, HAVING
- Cannot use transactions
- More restricted operations
Triggers are special stored procedures that automatically execute when specific events occur in the database. Common trigger types include BEFORE, AFTER, and INSTEAD OF triggers.
Sharding is a horizontal partitioning strategy that splits data across multiple database servers. Each shard contains a subset of the total data, enabling scalability beyond a single server's capacity.
Common Sharding Strategies:
- Range-based: Partition by value ranges (e.g., user IDs 1-1000, 1001-2000)
- Hash-based: Use hash function on shard key (consistent hashing)
- Geographic: Partition by location (region-based sharding)
- Directory-based: Lookup table maps entities to shards
Connection pooling reuses database connections instead of creating new ones for each request. This significantly improves performance by reducing connection overhead.
Common Table Expressions (CTEs) are temporary named result sets that exist within a single query. Recursive CTEs can reference themselves, enabling hierarchical and graph traversal queries.
Modern databases (PostgreSQL, MySQL, SQL Server) support native JSON data types with rich querying capabilities. This enables flexible schema design while maintaining ACID properties.
Database backup and recovery ensures data protection and business continuity. Different strategies balance between recovery point objective (RPO) and recovery time objective (RTO).
Locking prevents conflicts when multiple transactions access the same data concurrently. Different lock types and isolation levels balance between concurrency and consistency.
Database replication creates copies of data across multiple servers for high availability, load distribution, and disaster recovery. Common strategies include master-slave, master-master, and multi-master replication.
Interview Tips for Database
- ✓ Understand when to use indexes and their tradeoffs
- ✓ Know ACID properties and transaction isolation levels
- ✓ Be familiar with both SQL and NoSQL databases
- ✓ Practice query optimization using EXPLAIN
- ✓ Understand normalization vs denormalization tradeoffs
- ✓ Know how to design schemas for different use cases
- ✓ Be ready to discuss scaling strategies (sharding, replication)
- ✓ Master window functions for analytical queries
- ✓ Understand locking mechanisms and deadlock prevention
- ✓ Know backup/recovery strategies and RPO/RTO concepts