DBMS Interview Questions and Answers (2026)
Preparing for a DBMS interview? This comprehensive guide covers the most frequently asked Database Management System interview questions and answers for freshers, intermediate, and experienced candidates in 2026.
From core concepts like normalization, ACID properties, and indexing to advanced topics like query optimization, transactions, and concurrency control — this list is designed to help you ace technical rounds at top tech companies, product startups, and IT firms.
What You'll Learn
- Fundamental DBMS concepts asked in every technical interview
- SQL queries — basic to advanced (JOINs, subqueries, aggregations)
- Normalization forms: 1NF, 2NF, 3NF, BCNF
- Transactions, ACID properties, and concurrency control
- Indexing, query optimization, and performance tuning
- Difference between RDBMS, NoSQL, and NewSQL
- Real-world schema design and ER diagram questions
What is DBMS?
A Database Management System (DBMS) is a software application that interacts with users, applications, and the database itself to capture and analyze data. It allows for the creation, retrieval, updating, and management of data efficiently.
Why DBMS is Important from a Placements Perspective
Having a strong grip on DBMS is one of the keys to cracking technical interviews and securing dream placements in software development, data engineering, and IT roles. Here's why it should be a priority:
- High Demand: Most companies — from startups to MNCs — rely on databases, making DBMS expertise a must-have skill for developers and data professionals.
- Core Interview Topic: DBMS questions appear in nearly every backend, full-stack, and data engineering interview, often alongside DSA and system design rounds.
- Real-World Application: Knowledge of SQL, normalization, and transactions directly applies to building scalable, production-grade systems.
- Versatility: DBMS skills are valued across roles — backend development, database administration, data engineering, and analytics.
- Systems Thinking: Understanding DBMS deepens your grasp of data structures, storage engines, and how real-world applications manage state at scale.
Complete DBMS & SQL Free resources
Basic DBMS Interview Questions
1. What is DBMS?
A Database Management System (DBMS) is software that enables users to create, manage, and interact with databases. It acts as an interface between the end user and the database, handling storage, retrieval, data integrity, security, and concurrent access. Examples include MySQL, PostgreSQL, Oracle, and MongoDB.
2. What is a database?
A database is an organized, structured collection of data stored electronically. It allows data to be easily accessed, managed, and updated. Databases are managed by a DBMS and can range from simple flat files to complex relational or distributed systems.
3. What is a database system?
A database system refers to the combination of the database itself (the stored data), the DBMS software, and the applications that interact with it. It includes the hardware, software, data, users, and procedures that together form a complete data management environment.
4. What are the advantages of DBMS over traditional file-based systems?
DBMS overcomes the major drawbacks of file-based systems: data redundancy is reduced through centralized storage, data inconsistency is avoided, concurrent access by multiple users is supported safely, data integrity is enforced through constraints, security controls are applied at the database level, and backup and recovery mechanisms are built in.
5. Explain the difference between DBMS and RDBMS.
A DBMS stores data as files without enforcing relationships between them. An RDBMS (Relational DBMS) stores data in structured tables with rows and columns and enforces relationships between tables using keys. RDBMS supports SQL, ensures ACID properties, and is built on Codd's relational model. Examples: MySQL, PostgreSQL (RDBMS) vs. early dBASE or simple flat-file managers (DBMS).
6. What is a database schema?
A database schema is the logical blueprint or structure of a database. It defines the tables, columns, data types, relationships, constraints, and indexes without containing the actual data. The schema is like the skeleton — it describes the shape of the data, not the values stored in it.
7. What is normalization, and why is it used?
Normalization is the process of organizing a database to reduce data redundancy and improve data integrity. It involves decomposing tables into smaller, well-structured ones following a series of normal forms (1NF, 2NF, 3NF, BCNF). It ensures that each piece of data is stored in only one place, making updates, deletions, and insertions efficient and consistent.
8. What are the different types of keys in DBMS?
DBMS defines several key types: Primary Key (unique identifier for each row), Foreign Key (references a primary key in another table), Candidate Key (any column that could be a primary key), Super Key (any set of columns that uniquely identify a row), Composite Key (primary key made of multiple columns), Surrogate Key (system-generated unique identifier), and Unique Key (unique but allows one NULL).
9. Explain primary key, foreign key, and unique key.
A primary key uniquely identifies each record in a table and cannot be NULL. A foreign key is a column in one table that references the primary key of another table, establishing a relationship between them. A unique key also enforces uniqueness across a column but allows a single NULL value, unlike a primary key which prohibits NULLs entirely.
10. What is a composite key?
A composite key is a primary key composed of two or more columns that together uniquely identify a record. Neither column alone is unique, but their combination is. For example, in an OrderItems table, (order_id, product_id) together form a composite key since the same product can appear in different orders and the same order can have multiple products.
11. What is a surrogate key?
A surrogate key is an artificially generated unique identifier assigned to a record, with no business meaning. It is typically an auto-incremented integer or UUID. Surrogate keys are used when no natural key exists or when natural keys are too large or changeable to be practical as primary keys.
12. What is a candidate key?
A candidate key is any column (or combination of columns) that can uniquely identify a row in a table. A table may have multiple candidate keys, but only one is chosen as the primary key. The remaining candidate keys become alternate keys. For example, in a Users table, both user_id and email might be candidate keys.
13. What is the difference between a super key and a candidate key?
A super key is any set of one or more attributes that can uniquely identify a tuple in a relation — it may contain extra attributes that are not necessary for uniqueness. A candidate key is a minimal super key, meaning no attribute can be removed from it without losing the uniqueness property. All candidate keys are super keys, but not all super keys are candidate keys.
14. What is a relationship in DBMS, and what are the types of relationships?
A relationship describes the association between entities in a database. The three types are: One-to-One (1:1) — one record in table A corresponds to exactly one record in table B; One-to-Many (1:N) — one record in table A corresponds to multiple records in table B (most common); Many-to-Many (M:N) — multiple records in table A relate to multiple records in table B, typically resolved using a junction table.
15. What is an entity in DBMS?
An entity is a real-world object or concept that has distinct existence and can be uniquely identified. In a database, entities are represented as tables. For example, Student, Employee, and Product are entities. Each row in the table represents one instance (occurrence) of that entity.
16. What is an attribute in DBMS?
An attribute is a property or characteristic of an entity. In a table, attributes correspond to columns. For example, the Student entity might have attributes like student_id, name, age, and email. Attributes can be simple, composite, derived, or multi-valued depending on their nature.
17. What is a tuple in DBMS?
A tuple is a single row in a relational database table. It represents one complete record of an entity instance. For example, a row (1, 'Alice', 'alice@example.com') in a Users table is a tuple. The collection of all tuples in a table is called a relation.
18. What is the difference between primary key and unique constraints?
Both enforce uniqueness, but a primary key does not allow NULL values and there can be only one primary key per table. A unique constraint allows one NULL value (since NULL is considered distinct from all values including itself) and a table can have multiple unique constraints on different columns.
19. What is the purpose of normalization in DBMS?
Normalization eliminates data redundancy and prevents anomalies during insert, update, and delete operations. Without normalization, the same data stored in multiple places can become inconsistent when updated in one place but not others. By organizing data into properly structured tables, normalization ensures data integrity and makes the database easier to maintain.
20. What is the difference between a database schema and a database state?
A database schema is the structure or design of the database — it defines tables, columns, types, and relationships. It rarely changes. A database state (also called a database instance) is the actual data stored in the database at a particular point in time. The state changes frequently as records are inserted, updated, or deleted, but the schema stays the same unless explicitly altered.
21. What is the concept of sub-query in terms of SQL?
A subquery is a query nested inside another query. It can appear in the SELECT, FROM, or WHERE clause. The inner query executes first, and its result is used by the outer query. For example: SELECT name FROM employees WHERE dept_id = (SELECT id FROM departments WHERE name = 'HR'). Subqueries can be correlated (referencing the outer query) or non-correlated (independent).
22. What is the use of the DROP command, and what are the differences between DROP, TRUNCATE, and DELETE commands?
DROP permanently removes the entire table structure and all its data from the database — it cannot be rolled back in most databases. TRUNCATE removes all rows from a table but keeps the table structure; it is faster than DELETE and typically cannot be rolled back. DELETE removes specific rows based on a WHERE clause, can be rolled back within a transaction, and fires triggers. Use DELETE for selective removal, TRUNCATE to empty a table fast, and DROP to remove the table entirely.
23. What is the main difference between UNION and UNION ALL?
UNION combines the results of two SELECT statements and removes duplicate rows. UNION ALL combines results but keeps all duplicates, making it faster since no deduplication step is needed. Use UNION when unique results are required; use UNION ALL when performance matters and duplicates are acceptable or expected.
24. What is Correlated Subquery in DBMS?
A correlated subquery is a subquery that references columns from the outer query, making it dependent on the outer query for its execution. It is executed once for each row processed by the outer query. For example: SELECT e.name FROM employees e WHERE e.salary > (SELECT AVG(salary) FROM employees WHERE dept_id = e.dept_id) — here the inner query uses e.dept_id from the outer query.
25. Explain Entity, Entity Type, and Entity Set in DBMS.
An entity is a specific, distinguishable object (e.g., the student "Alice"). An entity type is the category or class of similar entities sharing the same attributes (e.g., Student). An entity set is the collection of all entities of a particular type stored in the database at a given time — essentially, the rows in a table.
26. What are the different levels of abstraction in the DBMS?
DBMS has three levels of abstraction: Physical level (how data is actually stored on disk — files, blocks, indexes), Logical level (what data is stored and the relationships between them — tables, schemas), and View level (how specific users see the data — custom views, restricted access). This three-tier architecture hides complexity and provides data independence.
27. What integrity rules exist in the DBMS?
The two fundamental integrity rules are: Entity Integrity — the primary key of a table must be unique and cannot be NULL, ensuring each row is uniquely identifiable. Referential Integrity — a foreign key value must either match an existing primary key value in the referenced table or be NULL, ensuring relationships between tables remain consistent and valid.
28. What is the E-R model in DBMS?
The Entity-Relationship (E-R) model is a high-level conceptual data model used to design databases visually. It represents data as entities (objects), attributes (properties of entities), and relationships (associations between entities). E-R diagrams use rectangles for entities, ellipses for attributes, and diamonds for relationships. It serves as a blueprint before translating the design into actual database tables.
29. What is a functional dependency in DBMS?
A functional dependency (FD) is a relationship between two attributes where the value of one attribute determines the value of another. Written as A → B, it means knowing the value of A uniquely determines the value of B. For example, student_id → student_name means knowing the student ID tells you exactly which student it is. FDs are the foundation of normalization theory.
30. What is 1NF in DBMS?
First Normal Form (1NF) requires that a table has no repeating groups or arrays, each column contains atomic (indivisible) values, and each column holds values of a single type. A table violates 1NF if a cell contains multiple values (like a comma-separated list of phone numbers). To fix it, each value must be placed in its own row.
31. What is 2NF in DBMS?
Second Normal Form (2NF) builds on 1NF and additionally requires that every non-key attribute be fully functionally dependent on the entire primary key — not just part of it. This is only relevant when the primary key is composite. If a non-key column depends on only one part of the composite key, that dependency must be moved to a separate table, eliminating partial dependencies.
32. What is 3NF in DBMS?
Third Normal Form (3NF) builds on 2NF and requires that no non-key attribute is transitively dependent on the primary key through another non-key attribute. In other words, non-key columns must depend only on the primary key, not on other non-key columns. If A → B → C where A is the key, then B → C is a transitive dependency and must be extracted into its own table.
33. What is BCNF in DBMS?
Boyce-Codd Normal Form (BCNF) is a stricter version of 3NF. A table is in BCNF if for every functional dependency A → B, A must be a super key. BCNF handles anomalies that 3NF misses in cases with multiple overlapping candidate keys. Most tables in 3NF are also in BCNF, but not all. Decomposing into BCNF may sometimes result in loss of functional dependencies.
34. What is a CLAUSE in SQL?
A clause is a component of a SQL statement that performs a specific function. Common clauses include: WHERE (filter rows), GROUP BY (aggregate rows by a column), HAVING (filter aggregated results), ORDER BY (sort results), LIMIT / TOP (restrict the number of rows returned), JOIN (combine rows from multiple tables), and FROM (specify the source table).
35. What is the difference between a database and a schema?
A database is the entire storage system — it contains all data, tables, indexes, procedures, and configuration. A schema is a logical namespace within a database that groups related objects (tables, views, functions). One database can contain multiple schemas. For example, in PostgreSQL you might have a public schema and a reporting schema within the same database.
36. What is the purpose of SQL?
SQL (Structured Query Language) is the standard language for interacting with relational databases. It serves multiple purposes: DDL (Data Definition Language) to define and modify schema (CREATE, ALTER, DROP), DML (Data Manipulation Language) to insert, update, delete, and query data (INSERT, UPDATE, DELETE, SELECT), DCL (Data Control Language) to manage permissions (GRANT, REVOKE), and TCL (Transaction Control Language) to manage transactions (COMMIT, ROLLBACK).
37. What are the main differences between Primary Key and Unique Key?
A Primary Key uniquely identifies each row, cannot contain NULL values, and there can be only one per table. A Unique Key also enforces uniqueness but allows one NULL value and a table can have multiple unique keys. Both create a unique index internally, but the primary key additionally signals the main identifier of the entity.
38. What is the difference between a database and a filesystem?
A filesystem stores data as raw files with no structure enforcement — it's the responsibility of the application to interpret the data. A database provides structured storage with a query language, integrity constraints, concurrent access control, transactions, and indexing. Databases offer consistency guarantees and powerful querying capabilities that filesystems do not natively provide.
39. What is the purpose of the CASE statement in SQL?
The CASE statement adds conditional logic to SQL queries, similar to if-else in programming. It evaluates conditions and returns a value for the first matching condition. Example: SELECT name, CASE WHEN salary > 100000 THEN 'High' WHEN salary > 50000 THEN 'Medium' ELSE 'Low' END AS salary_band FROM employees. It's commonly used in SELECT, ORDER BY, and HAVING clauses.
40. What is the difference between a database and a blockchain?
A database is a centralized system managed by a trusted authority, optimized for read/write performance, and allows data to be updated or deleted. A blockchain is a decentralized, distributed ledger where data is stored in immutable, cryptographically linked blocks — once written, it cannot be altered. Databases are best for general-purpose data management; blockchains are suited for trustless systems requiring tamper-proof audit trails.
SQL Queries
41. What is SQL?
SQL (Structured Query Language) is a domain-specific language designed for managing and querying relational databases. It was developed by IBM in the 1970s and is now the standard language for all major relational database systems. SQL is declarative — you specify what data you want, not how to retrieve it, and the database engine figures out the optimal execution path.
42. Explain the difference between DDL, DML, and DCL.
DDL (Data Definition Language) defines and modifies database structure: CREATE, ALTER, DROP, TRUNCATE. DML (Data Manipulation Language) operates on data within tables: SELECT, INSERT, UPDATE, DELETE. DCL (Data Control Language) manages access permissions: GRANT and REVOKE. A fourth category, TCL (Transaction Control Language), handles transactions: COMMIT, ROLLBACK, SAVEPOINT.
43. What is the difference between DELETE and TRUNCATE?
DELETE removes rows one at a time, logs each deletion, can use a WHERE clause to target specific rows, fires triggers, and can be rolled back within a transaction. TRUNCATE removes all rows at once by deallocating data pages, is much faster, does not fire row-level triggers, resets identity/auto-increment counters, and in many databases cannot be rolled back once executed.
44. What is the difference between WHERE and HAVING?
WHERE filters rows before any grouping or aggregation occurs — it operates on individual rows and cannot use aggregate functions. HAVING filters groups after GROUP BY aggregation — it can use aggregate functions like COUNT, SUM, AVG. Example: SELECT dept, COUNT(*) FROM employees WHERE active = 1 GROUP BY dept HAVING COUNT(*) > 5 — WHERE filters active employees first, then HAVING filters departments with more than 5 of them.
45. What is a JOIN? Explain different types of JOINs.
A JOIN combines rows from two or more tables based on a related column. Types: INNER JOIN returns only matching rows from both tables. LEFT JOIN returns all rows from the left table plus matching rows from the right (NULLs for non-matches). RIGHT JOIN is the mirror of LEFT JOIN. FULL OUTER JOIN returns all rows from both tables, with NULLs where no match exists. CROSS JOIN returns the Cartesian product of both tables. SELF JOIN joins a table with itself.
46. What is a subquery?
A subquery is a query embedded within another SQL query, enclosed in parentheses. The inner query runs first and its result is passed to the outer query. Subqueries can return a single value (scalar), a single row, a column, or a full result set. They can appear in SELECT, FROM, WHERE, and HAVING clauses. When the subquery references the outer query's columns, it's called a correlated subquery.
47. What is a view in SQL? How to create a view?
A view is a virtual table based on the result of a SQL query. It does not store data itself but presents data from one or more underlying tables. Create a view with: CREATE VIEW active_employees AS SELECT id, name, dept FROM employees WHERE active = 1. Query it like a regular table: SELECT * FROM active_employees. Views simplify complex queries and provide an abstraction layer.
48. What are the uses of a view?
Views serve multiple purposes: they simplify complex queries by encapsulating joins and filters into a reusable name, provide security by exposing only specific columns/rows to certain users, present data differently without altering the underlying schema, ensure backward compatibility when tables are restructured, and abstract business logic so application queries remain clean and consistent.
49. What is an index? Explain different types of indexes.
An index is a data structure that speeds up data retrieval at the cost of extra storage and slower writes. Types include: Clustered Index — physically reorders table rows to match the index (one per table, typically on the primary key). Non-Clustered Index — stores a separate structure pointing to row locations (multiple allowed per table). Unique Index — enforces uniqueness. Composite Index — covers multiple columns. Full-Text Index — optimizes text searches. Hash Index — fast for equality lookups.
50. What is a stored procedure?
A stored procedure is a precompiled set of SQL statements stored in the database and executed as a unit. It accepts input parameters, performs operations (queries, inserts, logic), and can return output. Example: CREATE PROCEDURE GetEmployee(IN emp_id INT) BEGIN SELECT * FROM employees WHERE id = emp_id; END. Benefits include code reuse, reduced network traffic, improved security, and encapsulated business logic.
51. What is a trigger?
A trigger is a database object that automatically executes a predefined set of SQL statements in response to specific events (INSERT, UPDATE, DELETE) on a table. Triggers can fire BEFORE or AFTER the event. They are used to enforce business rules, maintain audit logs, automatically update related tables, or validate data beyond what constraints can express.
52. What is the difference between UNION and UNION ALL?
UNION merges two result sets and automatically removes duplicate rows using an internal sort/deduplication step, making it slightly slower. UNION ALL merges result sets and retains all duplicates, making it faster since no deduplication is performed. Both require the same number of columns and compatible data types in both SELECT statements.
53. What is the difference between INNER JOIN and OUTER JOIN?
INNER JOIN returns only the rows where there is a match in both tables — unmatched rows are excluded. OUTER JOIN returns matched rows plus unmatched rows from one or both tables (filled with NULLs). LEFT OUTER JOIN keeps all rows from the left table; RIGHT OUTER JOIN keeps all from the right; FULL OUTER JOIN keeps all from both tables.
54. What is a self-join?
A self-join is when a table is joined with itself. It is used to find relationships within the same table. For example, finding all employees and their managers when both are stored in the same employees table: SELECT e.name AS employee, m.name AS manager FROM employees e JOIN employees m ON e.manager_id = m.id. An alias is required to distinguish the two references to the same table.
55. What is a correlated subquery?
A correlated subquery is a subquery that references one or more columns from the outer query, making it re-evaluated for every row processed by the outer query. This is different from a regular subquery which executes once. Example: SELECT name FROM employees e1 WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e2.dept_id = e1.dept_id). It is powerful but can be slow on large datasets.
56. What is the difference between GROUP BY and ORDER BY?
GROUP BY groups rows sharing the same value in specified columns into summary rows and is used with aggregate functions like COUNT, SUM, AVG. It does not sort the output. ORDER BY sorts the result set by one or more columns in ascending (ASC) or descending (DESC) order. Both can appear in the same query; GROUP BY is processed before ORDER BY.
57. What is the purpose of the DISTINCT keyword?
DISTINCT eliminates duplicate rows from the result set of a SELECT query. Example: SELECT DISTINCT department FROM employees returns each department name only once. It works across all selected columns, so SELECT DISTINCT first_name, last_name returns unique combinations of both columns. DISTINCT can slow down queries on large tables because it requires sorting or hashing to find duplicates.
58. What is the difference between CHAR and VARCHAR data types?
CHAR(n) is a fixed-length character type — it always uses exactly n bytes, padding with spaces if the value is shorter. VARCHAR(n) is a variable-length type — it uses only as many bytes as needed (plus 1-2 bytes for length storage). CHAR is slightly faster for fixed-length data (like country codes); VARCHAR is more space-efficient for variable-length strings like names or addresses.
59. What is the purpose of the LIMIT clause?
LIMIT (or TOP in SQL Server / ROWNUM in Oracle) restricts the number of rows returned by a query. It is useful for pagination, sampling data, or getting the top-N results. Example: SELECT * FROM products ORDER BY price DESC LIMIT 10 returns the 10 most expensive products. Combined with OFFSET, it enables pagination: LIMIT 10 OFFSET 20 returns rows 21–30.
60. What is the difference between a database and a table?
A database is the entire system that stores and manages data — it contains tables, views, indexes, stored procedures, users, and configuration. A table is a single structured data object within a database, organized into rows and columns representing one entity type. One database can contain hundreds of tables, each storing a different category of data.
61. What is the purpose of the EXISTS clause?
EXISTS is a boolean operator used in a subquery context — it returns TRUE if the subquery returns at least one row. Example: SELECT name FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id) returns customers who have placed at least one order. It is often faster than IN for large datasets because it short-circuits as soon as a matching row is found.
62. How to print duplicate rows in a table?
Use GROUP BY with HAVING COUNT(*) > 1:
SELECT email, COUNT(*)
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
To see the full duplicate rows with all columns, use a self-join or a subquery to match rows sharing the same identifying columns. This technique is commonly used for data quality checks and deduplication tasks.
63. What is Identity?
An identity (or auto-increment) column automatically generates a unique numeric value for each new row inserted into a table. It is commonly used for surrogate primary keys. In SQL Server: id INT IDENTITY(1,1). In MySQL: id INT AUTO_INCREMENT. In PostgreSQL: id SERIAL or GENERATED ALWAYS AS IDENTITY. The database manages the counter, so applications do not need to provide a value for this column.
64. What is the difference between Trigger and Stored Procedure?
A stored procedure is explicitly called by an application or user using EXEC or CALL. A trigger is automatically executed by the database in response to a DML event (INSERT, UPDATE, DELETE) on a specific table — it cannot be called directly. Triggers have no parameters. Stored procedures can accept and return parameters, making them more flexible for general-purpose logic.
65. What is the purpose of the WITH clause in SQL?
The WITH clause (Common Table Expression or CTE) defines a temporary named result set that can be referenced within the same query. It improves readability by breaking complex queries into logical steps. Example:
WITH dept_avg AS (
SELECT dept_id, AVG(salary) AS avg_sal FROM employees GROUP BY dept_id
)
SELECT e.name FROM employees e JOIN dept_avg d ON e.dept_id = d.dept_id WHERE e.salary > d.avg_sal;
CTEs can also be recursive, making them useful for hierarchical data like org charts or category trees.
66. How can you get the alternate records from the table in SQL?
To retrieve odd-numbered rows: SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY id) AS rn FROM employees) t WHERE rn % 2 = 1. For even-numbered rows, use rn % 2 = 0. In MySQL, MOD(rn, 2) achieves the same. This technique uses window functions to assign row numbers and then filters based on modulo arithmetic.
67. How is pattern matching done in SQL?
Pattern matching in SQL is done using the LIKE operator with wildcard characters. % matches any sequence of zero or more characters; _ matches exactly one character. Example: SELECT * FROM users WHERE name LIKE 'A%' finds names starting with A. LIKE '_ohn' matches 'John' or 'Bohn'. For more advanced regex-based matching, databases like PostgreSQL support ~ (regex match) and MySQL supports REGEXP.
68. What are the different types of joins in SQL?
SQL supports: INNER JOIN (only matching rows), LEFT OUTER JOIN (all left rows + matching right), RIGHT OUTER JOIN (all right rows + matching left), FULL OUTER JOIN (all rows from both, NULLs for non-matches), CROSS JOIN (Cartesian product — every row from left paired with every row from right), and SELF JOIN (a table joined with itself using aliases). Natural Join automatically joins on columns with the same name, though it's rarely recommended.
69. What is the difference between HAVING and WHERE clauses?
WHERE filters individual rows before any grouping — it cannot reference aggregate functions. HAVING filters groups after GROUP BY is applied — it can use aggregate functions. If no GROUP BY is used, HAVING behaves like WHERE for the entire result set. In query execution order: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY.
70. What is the purpose of the DROP command?
DROP permanently removes a database object (table, view, index, database, or procedure) along with all its data and associated metadata. Unlike TRUNCATE which only removes data, DROP removes the structure itself. It cannot be rolled back in most database systems. Use with caution — DROP TABLE employees will irreversibly delete the table definition and all its rows.
Transactions and Concurrency
71. What is a transaction?
A transaction is a logical unit of work comprising one or more SQL operations that must all succeed or all fail together. If any operation within the transaction fails, the entire transaction is rolled back to its initial state. Transactions ensure data integrity in multi-step operations. Example: transferring money between bank accounts requires debiting one account and crediting another — both must succeed or neither should.
72. Explain ACID properties.
ACID guarantees reliable transaction processing: Atomicity — a transaction is all-or-nothing; either all operations complete or none do. Consistency — a transaction brings the database from one valid state to another, respecting all integrity constraints. Isolation — concurrent transactions execute as if they were sequential; intermediate states are not visible to others. Durability — once committed, a transaction's changes are permanent even in the event of a system crash.
73. What is a deadlock?
A deadlock occurs when two or more transactions are each waiting for a resource held by another, creating a circular dependency where none can proceed. Example: Transaction A holds a lock on Table 1 and waits for Table 2; Transaction B holds a lock on Table 2 and waits for Table 1. The DBMS detects this cycle, selects one transaction as a victim, rolls it back, and allows the others to proceed.
74. What is concurrency control?
Concurrency control is the mechanism by which a DBMS manages simultaneous access to the database by multiple transactions while ensuring data consistency. Without it, problems like dirty reads, lost updates, and phantom reads can occur. Techniques include locking protocols (shared/exclusive locks), timestamp ordering, and multiversion concurrency control (MVCC), which is used by PostgreSQL and Oracle.
75. Explain the difference between optimistic and pessimistic locking.
Pessimistic locking assumes conflicts will happen frequently. It locks the resource when a transaction begins reading or modifying data, preventing others from accessing it until the lock is released. This is safe but reduces concurrency. Optimistic locking assumes conflicts are rare. It allows multiple transactions to read and modify data without locks, then checks for conflicts at commit time using a version number or timestamp. If a conflict is detected, one transaction is rolled back.
76. What is a rollback?
A rollback undoes all changes made by the current transaction, restoring the database to its state before the transaction began. It is executed when an error occurs or when explicitly called with ROLLBACK. This ensures atomicity — if one operation in a multi-step transaction fails, the entire transaction is reversed to maintain data integrity.
77. What is a savepoint?
A savepoint is a named marker within a transaction that allows partial rollback. Instead of rolling back the entire transaction, you can roll back to a specific savepoint while retaining changes made before it. Example: SAVEPOINT sp1; /* some operations */ ROLLBACK TO sp1; — this undoes operations after sp1 but keeps earlier changes. Savepoints are useful in long transactions where only a portion needs to be retried.
78. What is the difference between a transaction and a batch?
A transaction is a logical unit of work that follows ACID properties — it either fully commits or fully rolls back, ensuring data consistency. A batch is a set of SQL statements sent to the database server together in a single network round-trip for efficiency. Batches optimize performance by reducing network overhead but do not inherently provide atomicity. A batch can contain multiple transactions.
79. What is a dirty read in DBMS?
A dirty read occurs when a transaction reads data that has been modified by another transaction that has not yet committed. If the other transaction later rolls back, the first transaction has read data that never officially existed. This violates isolation and can lead to incorrect application behavior. It is prevented by using the READ COMMITTED isolation level or higher.
80. What is a phantom read in DBMS?
A phantom read occurs when a transaction re-executes a query and finds new rows that were inserted by another committed transaction since the first execution. Even though no existing rows changed, the result set is different. This happens at the REPEATABLE READ isolation level. Prevented by SERIALIZABLE isolation, which locks the entire range of rows that a query might return.
81. What is a non-repeatable read in DBMS?
A non-repeatable read occurs when a transaction reads the same row twice and gets different values because another transaction modified and committed that row between the two reads. Unlike dirty reads (which involve uncommitted changes), non-repeatable reads involve committed changes. Prevented by the REPEATABLE READ isolation level, which holds read locks until the transaction completes.
82. What is two-phase locking (2PL)?
Two-phase locking is a concurrency control protocol that ensures serializability. It has two phases: Growing phase — a transaction may acquire locks but cannot release any. Shrinking phase — once a lock is released, the transaction can no longer acquire new locks. 2PL guarantees conflict-serializable schedules but can lead to deadlocks. Strict 2PL holds all exclusive locks until commit, preventing cascading rollbacks.
83. What is the difference between serializable and repeatable read isolation levels?
REPEATABLE READ ensures that if a transaction reads a row, it will see the same data if it reads that row again — preventing dirty and non-repeatable reads, but phantom reads are still possible (new rows can appear). SERIALIZABLE is the strictest level — it prevents dirty reads, non-repeatable reads, AND phantom reads by effectively serializing all transactions, as if they ran one after another. Serializable provides the highest data integrity but lowest concurrency.
84. What is a log file in DBMS?
A transaction log (or redo/undo log) is a sequential record of all changes made to the database. Every INSERT, UPDATE, and DELETE is recorded before being applied. The log is used for recovery after a crash (redo committed transactions, undo uncommitted ones), replication, and auditing. Write-Ahead Logging (WAL) requires that log records be written to disk before the actual data is modified, ensuring durability.
85. What is the purpose of the COMMIT statement?
COMMIT permanently saves all changes made during the current transaction to the database. Once committed, the changes are durable — they survive system failures. It also releases all locks held by the transaction. COMMIT marks the successful end of a transaction. Auto-commit mode (enabled by default in many clients) automatically issues a COMMIT after each individual SQL statement.
86. What is a Live Lock?
A livelock is a situation where two or more transactions continuously retry their operations in response to each other without making any actual progress — they are active (not blocked like in deadlock) but unable to proceed. Think of two people in a narrow hallway both stepping aside in the same direction repeatedly to let the other pass. The DBMS typically detects and resolves livelocks by introducing random backoff delays or priority schemes.
87. What is the difference between a shared lock and an exclusive lock?
A shared lock (read lock) allows multiple transactions to read a resource simultaneously. No transaction can modify the resource while a shared lock is held, but multiple readers can coexist. An exclusive lock (write lock) gives a single transaction full control over a resource — no other transaction can read or write it while the exclusive lock is held. Exclusive locks prevent dirty reads by blocking all other access during modification.
Advanced DBMS Concepts
88. What is database partitioning?
Database partitioning divides a large table into smaller, more manageable pieces called partitions, while still appearing as a single logical table to queries. Types include: Range partitioning (by date ranges), List partitioning (by specific values), Hash partitioning (by hash of a key), and Composite partitioning (combination). Partitioning improves query performance, simplifies maintenance (archiving old data), and allows parallel processing.
89. What is database sharding?
Sharding is a horizontal scaling technique where data is distributed across multiple separate database instances (shards), each holding a subset of the data. Unlike partitioning (which stays within one database), sharding splits data across different servers. Each shard operates independently. A shard key determines which shard stores a given record. Sharding enables massive scale but adds complexity in cross-shard queries and distributed transactions.
90. Explain CAP theorem.
The CAP theorem states that a distributed database system can guarantee only two of three properties simultaneously: Consistency (every read receives the most recent write or an error), Availability (every request receives a response, though it may not be the most recent), and Partition Tolerance (the system continues operating despite network failures between nodes). Since network partitions are unavoidable in distributed systems, real-world databases choose between CP (consistent and partition-tolerant, e.g., HBase) and AP (available and partition-tolerant, e.g., Cassandra).
91. What is NoSQL?
NoSQL (Not Only SQL) refers to a broad category of databases that do not use the traditional relational table model. They are designed for specific use cases requiring high scalability, flexible schemas, or special data models. Types include: Document stores (MongoDB — JSON documents), Key-Value stores (Redis — simple key-value pairs), Column-family stores (Cassandra — wide-column model), and Graph databases (Neo4j — nodes and edges). NoSQL databases typically sacrifice some ACID guarantees for scale and performance.
92. Explain the difference between SQL and NoSQL.
SQL databases are relational, use structured tables with a fixed schema, support ACID transactions, and are excellent for complex queries with JOINs. They scale vertically. NoSQL databases are non-relational, have flexible or schema-less data models, prioritize horizontal scalability, and typically offer eventual consistency. SQL is ideal for structured business data; NoSQL excels at unstructured data, high-velocity writes, and massive scale (social media, IoT, caching).
93. What is a distributed database?
A distributed database is a database where data is stored across multiple physical locations — different machines, data centers, or even geographic regions — but appears as a single logical database to users. Data may be replicated (copies on multiple nodes) or fragmented (each node holds different data). Distributed databases improve availability, fault tolerance, and geographic performance but introduce challenges in consistency and distributed transaction management.
94. What is database replication?
Database replication is the process of copying and maintaining database data across multiple servers (replicas) in real time or near real time. Master-slave replication has writes going to the master and reads distributed across slaves. Multi-master replication allows writes on multiple nodes. Replication improves read performance, provides high availability (failover), and enables geographic distribution. The trade-off is replication lag and potential consistency issues in asynchronous setups.
95. What is a materialized view?
A materialized view is a view that physically stores the query result on disk, unlike a regular view which is computed on every access. It is refreshed periodically or on demand. Materialized views dramatically improve performance for expensive aggregate queries run frequently. Example: a dashboard showing total sales by region can use a materialized view updated hourly rather than re-running the heavy aggregation on every page load.
96. What is database indexing, and how does it work?
Database indexing creates a separate data structure (typically a B-tree or hash table) that maps column values to their row locations, allowing the database to find data without scanning every row. When you query WHERE email = 'alice@example.com', an index on email lets the engine jump directly to the matching rows rather than doing a full table scan. Indexes speed up reads but slow down writes (since the index must also be updated) and consume extra storage.
97. What is a B-tree index?
A B-tree (Balanced Tree) index is the most common index structure in relational databases. It organizes index entries in a sorted, balanced tree where all leaf nodes are at the same depth. This allows O(log n) time complexity for lookups, range queries, and ordered scans. B-tree indexes work well for equality (=), range (>, <, BETWEEN), and prefix (LIKE 'abc%') queries. Most RDBMS default indexes are B-tree.
98. What is a hash index?
A hash index uses a hash function to map column values to bucket positions, enabling O(1) average-time lookups for exact equality matches. They are extremely fast for WHERE id = 42 style queries. However, hash indexes do not support range queries (>, <, BETWEEN) or sorting, since hash values have no natural order. PostgreSQL supports hash indexes; MySQL uses them only for MEMORY tables.
99. What is the difference between horizontal and vertical partitioning?
Horizontal partitioning (sharding) divides a table by rows — each partition holds a subset of rows (e.g., rows for different date ranges or regions). The schema is identical across partitions. Vertical partitioning divides a table by columns — frequently accessed columns are separated from rarely accessed ones into different tables. Both share the same primary key. Horizontal partitioning helps with large row counts; vertical partitioning helps when some columns are very wide or rarely needed.
100. What is a database cluster?
A database cluster refers to a group of database servers working together, either for high availability (failover clustering — one active, one standby) or load distribution (read replicas). In PostgreSQL, a "cluster" specifically refers to a collection of databases managed by a single server instance. Clustering ensures that if one server fails, another takes over with minimal downtime, making it critical for production systems with uptime requirements.
101. What is a database trigger, and how is it different from a stored procedure?
A trigger automatically fires in response to DML events (INSERT, UPDATE, DELETE) on a table — you cannot call it manually. It runs implicitly and is tightly coupled to the table event. A stored procedure is explicitly invoked by a user or application, accepts parameters, and can perform a wide range of operations. Triggers are ideal for audit logging, enforcing constraints, or cascading updates; stored procedures are better for reusable business logic.
102. What is a cursor in DBMS?
A cursor is a database object that allows row-by-row processing of a query result set — similar to an iterator in programming. It is useful when you need to perform operations that require processing each row individually. Cursors are declared, opened, fetched from, and then closed. They are generally slower than set-based operations and should be used sparingly; in most cases, a single SQL statement with appropriate logic is more efficient.
103. What is Denormalization?
Denormalization is the deliberate introduction of redundancy into a database schema by merging tables or adding redundant columns to improve read performance. While normalization reduces redundancy for data integrity, denormalization trades storage and write complexity for faster reads. It is commonly used in data warehouses, reporting databases, and high-read systems where query speed is critical and updates are infrequent.
104. What is QBE (Query By Example)?
Query By Example (QBE) is a visual query language where users specify queries by filling in example values in a grid or table-like interface rather than writing SQL text. It was designed to make database querying accessible to non-programmers. Microsoft Access's design view is a well-known QBE implementation. The underlying system translates the visual input into SQL. QBE is largely replaced by SQL in modern systems but is still used in some BI tools.
105. Why are cursors necessary in embedded SQL?
In embedded SQL (SQL embedded within a host language like C or Java), a query can return multiple rows, but the host language variable can only hold one value at a time. Cursors bridge this gap by allowing the application to iterate through the result set one row at a time, processing each row with the host language's control structures (loops, conditions). Without cursors, multi-row result sets could not be meaningfully handled in procedural code.
106. What is the main goal of RAID technology?
RAID (Redundant Array of Independent Disks) improves storage reliability and/or performance by distributing data across multiple disks. Goals: RAID 0 — striping for performance (no redundancy). RAID 1 — mirroring for fault tolerance. RAID 5 — striping with distributed parity (balance of performance and fault tolerance). RAID 10 — combination of striping and mirroring. The main goal is to prevent data loss from disk failure (redundancy) and/or improve I/O speed (performance).
107. What is a checkpoint in DBMS?
A checkpoint is a point in time at which all dirty (modified) data pages in the buffer pool are flushed to disk and a log record is written. Checkpoints reduce recovery time after a crash — the database only needs to redo transactions that occurred after the last checkpoint rather than replaying the entire transaction log. Checkpoints are performed periodically by the DBMS automatically or can be triggered manually.
108. When does a checkpoint occur in DBMS?
Checkpoints occur: periodically based on time intervals configured in the DBMS, when the transaction log reaches a certain size, when explicitly triggered by a DBA using a CHECKPOINT command, or before a clean shutdown of the database to ensure all changes are persisted. The frequency of checkpoints is a trade-off between recovery time (more frequent = faster recovery) and runtime overhead (checkpoint I/O impacts performance).
109. What do you mean by transparent DBMS?
A transparent DBMS hides the internal complexity of data management from users and applications. Users interact with a simple, logical view of the data without needing to know how it is physically stored, where it is located (in distributed systems), how replication works, or how concurrency is managed. Data independence is a key form of transparency — changes to physical storage do not require changes to application queries.
110. What are the unary operations in Relational Algebra?
Unary operations in relational algebra operate on a single relation (table): Selection (σ) filters rows based on a condition (equivalent to SQL WHERE). Projection (π) selects specific columns and removes duplicates (equivalent to SQL SELECT DISTINCT). Rename (ρ) renames the relation or its attributes. These form the foundation of relational query processing and are implemented internally by every SQL query engine.
111. What is Relational Algebra?
Relational Algebra is a procedural query language that serves as the theoretical foundation for SQL. It defines operations on relations (tables): selection, projection, union, intersection, difference, Cartesian product, join, and rename. Unlike SQL (which is declarative), relational algebra specifies the exact sequence of operations. Query optimizers internally translate SQL into relational algebra expressions and then optimize them before execution.
112. What is Relational Calculus?
Relational Calculus is a non-procedural (declarative) query language based on mathematical logic, serving as an alternative theoretical foundation to relational algebra. It specifies what data to retrieve without describing how. Tuple Relational Calculus (TRC) uses tuple variables to express queries. Domain Relational Calculus (DRC) uses domain variables. Both are equivalent in expressive power to relational algebra. SQL is based on tuple relational calculus.
113. What do you understand by query optimization?
Query optimization is the process by which the DBMS's query optimizer analyzes a SQL query and determines the most efficient execution plan. It considers different join orders, index usage, and access methods, estimates the cost of each plan using statistics (row counts, data distribution), and selects the cheapest plan. Good optimization can reduce query time from hours to milliseconds. The EXPLAIN command in SQL shows the execution plan chosen.
114. What do you mean by durability in DBMS?
Durability (the D in ACID) guarantees that once a transaction is committed, its changes are permanently stored and will survive any subsequent failures — power outages, crashes, or errors. Durability is achieved through Write-Ahead Logging (WAL), where transaction changes are written to a persistent log before being applied to data files. Upon recovery, the log is replayed to restore committed state.
115. What is System R? How many of its two major subsystems?
System R was IBM's pioneering research project in the 1970s that implemented the first SQL-based relational database. It demonstrated that relational databases could be both theoretically sound and practically performant. It consisted of two major subsystems: the Relational Data System (RDS) — the upper layer handling SQL parsing, optimization, and query compilation; and the Research Storage System (RSS) — the lower layer handling physical storage, locking, logging, and recovery.
116. What is Data Independence?
Data independence is the ability to change the schema at one level without affecting the schema at higher levels. Physical data independence allows changes to physical storage (file organization, indexes, storage devices) without changing the logical schema. Logical data independence allows changes to the logical schema (adding/removing tables or columns) without changing external views or application programs. It is a key benefit of the three-level architecture in DBMS.
117. What are the three levels of data abstraction?
The three-level ANSI/SPARC architecture: Physical level (internal schema) — describes how data is stored on disk: file formats, storage structures, indexes. Logical level (conceptual schema) — describes what data is stored and the relationships between entities: tables, columns, constraints. View level (external schema) — describes how individual users or applications see the data: customized views that hide irrelevant details and provide security. Each level provides abstraction from the one below it.
Performance Tuning
118. How do you optimize a SQL query?
Query optimization strategies: Use indexes on columns in WHERE, JOIN, and ORDER BY clauses. Avoid SELECT * — fetch only needed columns. Rewrite correlated subqueries as JOINs where possible. Use EXISTS instead of IN for large subqueries. Avoid functions on indexed columns in WHERE clauses (e.g., WHERE YEAR(date) = 2024 prevents index use). Use query hints or CTEs to help the optimizer. Analyze with EXPLAIN to identify bottlenecks like full table scans or poor join orders.
119. What is a query execution plan?
A query execution plan (or query plan) is the sequence of operations the database engine will perform to execute a SQL query. It shows which indexes are used, how tables are joined, in what order operations occur, and estimated costs for each step. Accessed via EXPLAIN (MySQL, PostgreSQL) or EXPLAIN PLAN (Oracle) or SET SHOWPLAN (SQL Server). Reading execution plans is essential for identifying slow queries and understanding why an index isn't being used.
120. What are the common performance issues in databases?
Common issues: Missing indexes causing full table scans. N+1 query problem — loading one record then querying N related records individually. Locking contention — long-running transactions holding locks that block others. Inefficient joins on non-indexed columns. Over-fetching data with SELECT *. Table bloat from dead rows (PostgreSQL). Statistics not updated causing poor query plans. Unbounded queries without LIMIT on large tables. Missing connection pooling causing excessive connection overhead.
121. How do you handle slow-running queries?
Approach: First, run EXPLAIN / EXPLAIN ANALYZE to see the execution plan. Identify full table scans on large tables and add appropriate indexes. Check if indexes exist but aren't being used (may need query rewrite). Look for missing JOINs replaced by subqueries. Consider query rewriting, adding composite indexes, or breaking the query into smaller steps using CTEs. For persistent slow queries, review table statistics and consider denormalization or caching the result.
122. What is database caching?
Database caching stores frequently accessed query results or computed data in fast-access memory (RAM) to avoid repeated expensive database operations. Types: Buffer pool — the DBMS's internal cache of recently accessed data pages. Query result cache — caching complete query results (MySQL query cache, now deprecated). Application-level cache — using Redis or Memcached to cache API or query results. Caching dramatically reduces database load and latency but requires cache invalidation strategies to maintain data freshness.
123. What is the difference between a clustered and a non-clustered index?
A clustered index determines the physical order of data on disk — the table rows are stored sorted by the clustered index key. Each table can have only one clustered index (typically the primary key). A non-clustered index is a separate structure that stores index entries with pointers to the actual row locations. A table can have many non-clustered indexes. Clustered indexes are faster for range queries; non-clustered indexes are more flexible and support additional search patterns.
124. What is query optimization?
Query optimization is the process where the DBMS's optimizer automatically generates multiple possible execution plans for a SQL query, estimates the cost of each (based on table statistics, cardinality estimates, and available indexes), and selects the plan with the lowest estimated cost. Modern optimizers use cost-based optimization (CBO) with statistical models. Developers can assist the optimizer by keeping table statistics current (ANALYZE), creating appropriate indexes, and writing queries in optimizer-friendly ways.
125. What is the purpose of indexing in databases?
Indexing dramatically speeds up data retrieval by creating auxiliary data structures that allow the database to locate rows without scanning every row in the table. Without an index, a query WHERE email = 'x@y.com' on a table with millions of rows requires a full table scan (O(n)). With an index on email, it's an O(log n) B-tree lookup. Indexes are critical for performance on large tables but must be used judiciously — each index adds overhead to INSERT, UPDATE, and DELETE operations.
126. How do you identify and resolve deadlocks?
Identify: Enable deadlock logging (e.g., innodb_print_all_deadlocks in MySQL). The database automatically detects and resolves deadlocks by killing one transaction (the victim). Review deadlock logs for the involved tables and transactions. Resolve: Access tables in a consistent order across all transactions. Keep transactions short. Use lower isolation levels where appropriate. Add indexes to reduce lock scope. Consider using SELECT FOR UPDATE SKIP LOCKED for queue-like patterns. Application code should handle deadlock errors and retry.
127. What is the role of the database buffer pool?
The buffer pool (or buffer cache) is an in-memory area where the database engine caches data pages and index pages read from disk. When data is needed, the engine first checks the buffer pool (cache hit — fast). If not found (cache miss), it reads from disk and loads the page into the buffer pool. A larger buffer pool means more data fits in memory, reducing disk I/O. The buffer pool is managed using replacement algorithms like LRU (Least Recently Used) to evict cold pages when memory is full.
Security
128. What is database security?
Database security encompasses all measures taken to protect the database from unauthorized access, data breaches, tampering, and destruction. It includes authentication (verifying user identity), authorization (controlling what actions users can perform), encryption (protecting data at rest and in transit), auditing (logging all access and changes), and network security (firewalls, VPNs, SSL). Database security is critical because databases often store sensitive personal, financial, and business data.
129. What is SQL injection?
SQL injection is a critical security vulnerability where an attacker inserts malicious SQL code into user input fields, causing the database to execute unintended commands. Example: if a login query is "SELECT * FROM users WHERE name = '" + userInput + "'", an attacker can enter ' OR '1'='1 to bypass authentication or '; DROP TABLE users; -- to destroy data. SQL injection has been the #1 web application vulnerability for years and can expose or destroy entire databases.
130. How do you prevent SQL injection?
The primary defense is parameterized queries (prepared statements) — never concatenate user input directly into SQL strings. Example (safe): SELECT * FROM users WHERE email = ? with the email passed as a parameter — the database treats it as data, not SQL. Additional measures: use an ORM (Object-Relational Mapper), validate and sanitize all input, apply principle of least privilege to database accounts, use stored procedures, and deploy a Web Application Firewall (WAF).
131. What is database encryption?
Database encryption protects data by converting it to an unreadable ciphertext using cryptographic algorithms. Transparent Data Encryption (TDE) encrypts the entire database at rest (files on disk) automatically, without application changes. Column-level encryption encrypts specific sensitive columns (credit card numbers, SSNs). Connection encryption (SSL/TLS) protects data in transit between the application and database. Encryption ensures that even if storage media is stolen, the data remains unreadable without the encryption key.
132. What is role-based access control?
Role-Based Access Control (RBAC) manages database permissions by assigning privileges to roles rather than individual users. Users are then assigned to roles. For example, a readonly role gets SELECT privileges; a data_entry role gets INSERT/UPDATE; a dba role gets full access. RBAC simplifies permission management — instead of updating permissions for every user, you update the role once. SQL commands: CREATE ROLE, GRANT SELECT ON table TO role, GRANT role TO user.
133. What is the difference between authentication and authorization?
Authentication verifies the identity of a user — it answers "who are you?" through usernames/passwords, certificates, or multi-factor authentication. It happens at login. Authorization determines what an authenticated user is allowed to do — it answers "what can you access?" through permissions and roles. In DBMS: authentication verifies login credentials; authorization controls which tables, views, or procedures the user can access and what operations (SELECT, INSERT, DELETE) they can perform.
134. What is database auditing?
Database auditing is the tracking and logging of all database activities — who accessed what data, when, and what changes were made. It creates an immutable audit trail for compliance (GDPR, HIPAA, SOX), security investigations, and forensic analysis after incidents. Audit logs typically capture: login attempts, SELECT queries, data modifications (INSERT/UPDATE/DELETE), schema changes (DDL), and permission grants. Most enterprise databases provide built-in auditing features that can be configured to specific tables or operations.
135. What is data masking?
Data masking replaces sensitive data with realistic but fictitious values to protect it in non-production environments (development, testing, analytics). For example, real credit card numbers are replaced with fake but structurally valid numbers; real names are replaced with random names. Static masking creates a masked copy of the database. Dynamic masking masks data in real time as it is queried based on user permissions. It allows developers and testers to work with realistic datasets without exposing sensitive production data.
136. What is a firewall in the context of database security?
A database firewall is a security layer that monitors, filters, and controls SQL traffic between applications and the database server. It can detect and block SQL injection attacks, unauthorized access patterns, and policy violations in real time. Unlike network firewalls (which filter by IP/port), database firewalls understand SQL syntax and can inspect query content. They also enforce whitelist policies — only allowing known, approved queries — and alert security teams about suspicious activity.
137. What is the purpose of SSL/TLS in database connections?
SSL (Secure Sockets Layer) / TLS (Transport Layer Security) encrypts the communication channel between the application and the database server, preventing man-in-the-middle attacks and eavesdropping on data in transit. Without SSL/TLS, anyone with network access could intercept database queries and results in plaintext — including sensitive data. SSL/TLS also provides server authentication (verifying the database server's identity via certificate) and optionally client authentication. It is essential for databases accessible over public networks.
Miscellaneous
138. What is a data warehouse?
A data warehouse is a centralized repository designed for analytical reporting and business intelligence, storing large volumes of historical data from multiple source systems. Unlike OLTP databases optimized for fast transactional reads/writes, data warehouses are optimized for complex analytical queries (OLAP) across large datasets. They use denormalized schemas (star/snowflake) for query performance. Examples: Amazon Redshift, Google BigQuery, Snowflake. ETL processes feed data from operational databases into the warehouse.
139. What is OLAP and OLTP?
OLTP (Online Transaction Processing) handles day-to-day transactional operations — INSERT, UPDATE, DELETE on individual records. It prioritizes speed, consistency, and concurrency for high-volume, low-latency operations (e.g., e-commerce purchases, banking transactions). Highly normalized. OLAP (Online Analytical Processing) handles complex analytical queries on large historical datasets for reporting and business intelligence. It reads large amounts of data, uses aggregations, and is typically denormalized for query performance. Data warehouses are OLAP systems.
140. Explain the difference between a data lake and a data warehouse.
A data warehouse stores structured, processed data in a predefined schema optimized for SQL-based analytics — data is cleaned and transformed before loading (ETL). A data lake stores raw, unstructured, semi-structured, or structured data in its native format at massive scale. Data lakes use a schema-on-read approach — structure is applied when data is queried, not when stored. Data lakes are cheaper and more flexible but require more effort to query. Modern "lakehouse" architectures (Delta Lake, Apache Iceberg) combine both paradigms.
141. What is ETL?
ETL (Extract, Transform, Load) is the process of moving data from source systems into a data warehouse: Extract — data is pulled from multiple source systems (databases, APIs, files). Transform — data is cleaned, deduplicated, standardized, and reshaped to fit the warehouse schema (handling nulls, converting types, joining records). Load — the transformed data is written to the target data warehouse. Modern alternatives include ELT (Extract, Load, Transform) where raw data is loaded first and transformed using the warehouse's own compute power.
142. What is a database trigger?
A database trigger is a procedural code that automatically executes in response to certain events on a table or view — INSERT, UPDATE, or DELETE. Triggers can fire BEFORE (for validation/modification) or AFTER (for audit/cascade) the event. They are used for maintaining audit logs (recording who changed what and when), enforcing complex business rules that constraints cannot express, synchronizing related tables, and preventing invalid operations.
143. What is the difference between a database and a data warehouse?
A database (OLTP) is designed for operational use — handling real-time transactions with fast reads/writes on current data. It is normalized, supports ACID transactions, and manages concurrent users efficiently. A data warehouse (OLAP) is designed for analytical use — storing years of historical data from multiple sources for reporting. It is denormalized for query speed, typically updated in batches, and optimized for read-heavy analytical workloads with complex aggregations.
144. What is the purpose of a data dictionary?
A data dictionary (or system catalog) is a repository of metadata — data about the database itself. It stores information about tables (names, columns, data types, constraints), indexes, users, views, stored procedures, and relationships. The DBMS uses the data dictionary internally for query parsing, optimization, and access control. DBAs use it to understand database structure and dependencies. In SQL, system catalog tables like INFORMATION_SCHEMA or pg_catalog expose this metadata.
145. What is the difference between a fact table and a dimension table?
In data warehouse design: Fact tables store quantitative, measurable business events — sales transactions, page views, sensor readings. They are large and contain foreign keys to dimension tables plus numeric measures (sales amount, quantity). Dimension tables store descriptive, contextual attributes about the entities in fact records — customer details, product descriptions, date/time hierarchies. They are smaller and change infrequently. Together they form the star or snowflake schema used in OLAP systems.
146. What is a star schema?
A star schema is a dimensional data warehouse schema where a central fact table is surrounded by multiple dimension tables, forming a star shape. The fact table contains measures and foreign keys to each dimension. Dimension tables are denormalized (all attributes in one flat table). Example: a sales_fact table connected to customer_dim, product_dim, date_dim, and store_dim. Star schemas are simple, fast for queries, and easy for BI tools to navigate.
147. What is a snowflake schema?
A snowflake schema is a variation of the star schema where dimension tables are normalized into multiple related tables, forming a snowflake shape. For example, product_dim might be split into product, category, and subcategory tables. Snowflake schemas reduce data redundancy in dimensions and save storage but require more JOINs, making queries more complex and slightly slower than the star schema. It trades query simplicity for data integrity in dimensions.
148. What is the purpose of the WITH clause in SQL?
The WITH clause (Common Table Expression or CTE) creates a temporary named result set scoped to the query. It improves readability by breaking complex queries into named, logical steps. CTEs can reference themselves recursively (WITH RECURSIVE) to query hierarchical data like organization trees or category hierarchies. Example: finding all subordinates of a manager by recursively joining the employee table on manager_id. CTEs do not persist after the query completes.
149. What is the difference between a database and a blockchain?
A relational database is centralized, managed by a trusted administrator, mutable (data can be updated/deleted), optimized for fast queries, and requires trust in the custodian. A blockchain is decentralized (no single authority), immutable (records cannot be altered after confirmation), uses cryptographic consensus for validation, is slower and more storage-intensive, and is designed for trustless environments. Databases are ideal for business applications; blockchains are suited for decentralized finance, supply chain traceability, and smart contracts.
150. What is the difference between logical database design and physical database design?
Logical database design focuses on the conceptual structure of the database — entities, attributes, relationships, normalization, and business rules — independent of any specific DBMS or hardware. It produces an ER diagram and relational schema. Physical database design translates the logical design into actual implementation on a specific DBMS — choosing data types, defining indexes, deciding on partitioning strategies, storage parameters, and access methods to optimize performance for expected workloads.
151. What are temporary tables? When are they useful?
Temporary tables are tables created to store intermediate results during a session or transaction and are automatically dropped when the session ends (or transaction completes, for transaction-scoped temps). They are useful for: breaking complex multi-step queries into stages, storing intermediate results that are referenced multiple times, staging data during batch ETL processes, and isolating changes during complex operations. In SQL Server: #temp_table; in PostgreSQL/MySQL: CREATE TEMPORARY TABLE.
152. Explain different types of failures that occur in the Oracle database.
Oracle and DBMS systems encounter several failure types: Transaction failure — a transaction aborts due to a logical error, deadlock, or explicit ROLLBACK. System failure — OS crash or power failure halts the database; committed transactions are recovered from the redo log. Media failure — physical disk corruption or failure; recovered using backups and archived redo logs. Network failure — connection interruptions between clients and the database. Application failure — bugs in client applications causing incomplete transactions. Oracle's RMAN (Recovery Manager) handles backup and recovery for media failures.
Struggling to Find a Job? These Startups Are Hiring ✅ Startup lits