DBMS Notes
Learn about interview questions — dbms in Database Management Systems with detailed explanations and examples.
Fresher Level Questions
Q1. What is the difference between SQL and NoSQL databases?
| Feature | SQL (Relational) | NoSQL |
|---|---|---|
| Schema | Fixed, predefined | Dynamic, flexible |
| Scaling | Vertical (scale up) | Horizontal (scale out) |
| Transactions | Full ACID | BASE (eventual consistency) |
| Joins | Supported | Generally not supported |
| Examples | MySQL, PostgreSQL | MongoDB, Cassandra, Redis |
| Best for | Structured data, complex queries | Large-scale, unstructured data |
Q3. What is a stored procedure vs. a function?
| Feature | Stored Procedure | Function |
|---|---|---|
| Return value | Can return 0 or more values | Must return exactly one value |
| Usage | Called with CALL statement | Used inside SQL expressions |
| DML operations | Can execute DML (INSERT, UPDATE) | Cannot modify data (in most DBs) |
| Transaction control | Can use COMMIT/ROLLBACK | Cannot |
| Purpose | Complex business logic | Computation / calculations |
Q4. Explain different types of database relationships.
Q5. What are database transactions? Explain with an example.
A transaction is a logical unit of work that consists of one or more SQL operations that must all succeed or all fail together.
Example — Online Banking:
Transaction: Transfer ₹5000 from Savings to Current
BEGIN TRANSACTION;
UPDATE Accounts SET balance = balance - 5000 WHERE acct_id = 'SAV001';
UPDATE Accounts SET balance = balance + 5000 WHERE acct_id = 'CUR001';
COMMIT;
If any step fails → ROLLBACK (both operations are undone)Intermediate Level Questions
Q6. What is query optimization? How does a query optimizer work?
Query optimization is the process of selecting the most efficient execution plan for a given SQL query. The optimizer evaluates multiple possible plans and chooses the one with the lowest estimated cost (I/O, CPU, memory).
Steps:
- Parsing: SQL is parsed into an internal representation (parse tree)
- Logical Optimization: Apply algebraic transformations (push selections down, eliminate redundancy)
- Physical Plan Generation: Generate multiple candidate physical plans
- Cost Estimation: Estimate cost using statistics (row counts, index availability, data distribution)
- Plan Selection: Choose the plan with minimum estimated cost
- Execution: Execute the chosen plan
Q7. What is deadlock detection using a Wait-For Graph?
A Wait-For Graph (WFG) is used to detect deadlocks. Nodes represent transactions; a directed edge T1 → T2 means T1 is waiting for a resource held by T2.
Q8. Explain MVCC (Multi-Version Concurrency Control).
MVCC maintains multiple versions of each data item so that readers never block writers and writers never block readers.
- Every write creates a new version of the row with a timestamp.
- Readers see the most recent committed version as of their transaction start time.
- Used by PostgreSQL, Oracle, MySQL InnoDB.
Advantages:
- High read concurrency
- Non-blocking reads
- Consistent snapshots
Q9. What is the CAP Theorem?
The CAP Theorem states that a distributed database can guarantee at most two of the following three properties simultaneously:
| CA | Traditional relational databases (single node) |
| CP | MongoDB, HBase (sacrifice availability during partition) |
| AP | Cassandra, CouchDB (sacrifice consistency during partition) |
- Consistency: Every read receives the most recent write
- Availability: Every request receives a response (not necessarily the latest)
- Partition Tolerance: System continues operating despite network partitions
Q10. How would you design a database for an e-commerce platform?
Core Entities:
Key design decisions:
- Use foreign keys to enforce referential integrity
- Index on UserID (Orders), ProductID (OrderItems), Email (Users)
- Separate OrderItems to handle multiple products per order (normalization)
- Store UnitPrice in OrderItems (not just from Products) to preserve historical pricing
Advanced Level Questions
Q11. What is database sharding?
Sharding is a horizontal partitioning technique where data is distributed across multiple database servers (shards) based on a shard key.
| Shard Key | UserID |
| Shard 1: UserID 1–100,000 | Server A |
| Shard 2: UserID 100,001–200,000 | Server B |
| Shard 3: UserID 200,001–300,000 | Server C |
Advantages: Linear horizontal scalability, improved performance. Challenges: Cross-shard queries are expensive, re-sharding is complex, no cross-shard transactions.
Q12. Explain the differences between OLTP and OLAP systems.
| Feature | OLTP | OLAP |
|---|---|---|
| Purpose | Day-to-day transactions | Analytics and reporting |
| Data volume | Gigabytes | Terabytes to petabytes |
| Query type | Simple, short | Complex, aggregations |
| Response time | Milliseconds | Seconds to minutes |
| Schema | Normalized (3NF) | Denormalized (Star/Snowflake) |
| Operations | INSERT, UPDATE, DELETE | Mostly SELECT (read-heavy) |
| Examples | Banking, e-commerce | Data warehouses, BI tools |
Q13. What is eventual consistency in distributed systems?
Eventual consistency is a consistency model used in distributed databases where it is guaranteed that, if no new updates are made, all replicas will eventually converge to the same value.
It is a weaker guarantee than strong consistency but allows higher availability and partition tolerance (AP systems in CAP theorem).
Example: DNS, Amazon DynamoDB, Apache Cassandra.
Continue the lesson path