Database systems the backbone of modern applications, handling everything from transactional analytical workloads. At the intermediate level, interviews often probe deeper understanding beyond basic SQL syntax — focusing on design patterns, optimization strategies, scalability challenges, and theoretical principles. This topic is commonly tested atmid-to-senior roles** atAmazon, Google, Microsoft, Netflix, and FAANG+**, where robust data handling is critical.
BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE user_id = 1; UPDATE accounts SET balance = balance + 100 WHERE user_id = 2; COMMIT; -- Either both updates happen or neither
users, orders, order_itemsorders table includes user_name for faster reporting(a,b) ≠ (b,a))CREATE INDEX idx_user_email ON users(email); -- Speed up WHERE email = '...' CREATE INDEX idx_orders_date_status ON orders(order_date, status); -- Good for range queries
Definition: : Degrees to which transactions are isolated from each other.
Key Points:
Comparison Table:
| Isolation Level | Dirty Reads | Non-repeatable Reads | Phantom Reads |
|---|---|---|---|
| Read Uncommitted | ✅ | ✅ | ✅ |
| Read Committed | ❌ | ✅ | ✅ |
| Repeatable Read | ❌ | ❌ | ✅ |
| Serializable | ❌ | ❌ | ❌ |
What are the differences between OLTP and OLAP systems? How would you design a schema for each?
orders with foreign keys.Explain how to optimize this query: SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31' ORDER BY total DESC LIMIT 10;
CREATE INDEX idx_orders_date_total ON orders(order_date, total DESC);What is a deadlock? How would you resolve it?
How do you handle data consistency in a distributed database?
What are window functions? Give an example use case.
SELECT order_date, amount, SUM(amount) OVER (ORDER BY order_date) AS running_total FROM sales;
Scenario: Design a database for a Twitter-like feed with 1B users.
Steps:
CREATE TABLE users ( user_id BIGINT PRIMARY KEY, username TEXT UNIQUE, created_at TIMESTAMP ); CREATE TABLE tweets ( tweet_id BIGINT PRIMARY KEY, user_id BIGINT REFERENCES users(user_id), content TEXT, created_at TIMESTAMP );
Query:
EXPLAIN ANALYZE SELECT product_id, AVG(price) FROM sales WHERE sale_date >= '2023-01-01' GROUP BY product_id;
Optimization Steps:
sale_date (e.g., monthly partitions)Easy: Write a query to find the nth highest salary from the employees table.
> Hint: Use Window Functions (RANK() or DENSE_RANK())
Intermediate: Design a shopping cart database schema supporting concurrent updates and idempotent operations.
> Hint: Consider optimistic locking with version numbers
Intermediate+: Explain how to implement eventual consistency for a user profile service that syncs with a analytics database.
> Hint: Use message queues (Kafka) and change data capture (CDC)
Start a new session to explore different topics or increase the difficulty level.
Start New Session