20 DBMS Interview Questions and Answers
Prepare for your next interview with our comprehensive guide on DBMS, featuring common and advanced questions to enhance your understanding and skills.
Prepare for your next interview with our comprehensive guide on DBMS, featuring common and advanced questions to enhance your understanding and skills.
Database Management Systems (DBMS) are crucial for efficiently storing, retrieving, and managing data in various applications. From small startups to large enterprises, DBMS solutions like MySQL, PostgreSQL, and Oracle are integral to handling vast amounts of data and ensuring its integrity and security. Mastery of DBMS concepts is essential for roles involving data management, software development, and system administration.
This article offers a curated selection of DBMS interview questions designed to test and enhance your understanding of key concepts. By working through these questions, you will be better prepared to demonstrate your expertise and problem-solving abilities in any technical interview setting.
A primary key uniquely identifies a record in a database table, ensuring no duplicate or NULL values exist in the primary key column(s). This maintains data integrity and enables efficient data retrieval. To define a primary key in SQL, specify it during table creation or alter an existing table. For example:
CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), Department VARCHAR(50) );
Alternatively, add a primary key to an existing table:
ALTER TABLE Employees ADD PRIMARY KEY (EmployeeID);
To find the second highest salary from an Employee table, use the DENSE_RANK()
window function, which assigns a rank to each row within a partition of a result set. Here’s an example:
SELECT salary FROM ( SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rank FROM Employee ) ranked_salaries WHERE rank = 2;
The inner subquery ranks each salary in descending order, and the outer query filters to return the salary with a rank of 2.
A foreign key constraint links two tables, ensuring the value in the child table matches a value in the parent table, maintaining referential integrity. Define the foreign key in the child table and reference the primary key of the parent table. For example:
CREATE TABLE parent_table ( id INT PRIMARY KEY, name VARCHAR(50) ); CREATE TABLE child_table ( id INT PRIMARY KEY, parent_id INT, FOREIGN KEY (parent_id) REFERENCES parent_table(id) );
Here, parent_id
in child_table
references id
in parent_table
.
ACID properties ensure reliable database transactions: Atomicity (transactions are all-or-nothing), Consistency (transactions bring the database from one valid state to another), Isolation (transactions do not interfere with each other), and Durability (committed transactions persist despite failures).
In SQL, a join combines rows from two or more tables based on a related column. Here’s an example of joining two tables, employees
and departments
, and selecting specific columns:
SELECT employees.employee_id, employees.first_name, employees.last_name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.department_id;
This query joins the tables using the department_id
column and selects specific columns from each table.
Indexes are data structures that speed up data retrieval. They are created on columns frequently used in query conditions. Types include primary, unique, clustered, and non-clustered indexes. While indexes improve query performance by reducing data scanning, they also increase storage requirements and can affect data modification operations.
A many-to-many relationship in a relational database is managed using a junction table, which breaks down the relationship into two one-to-many relationships. The junction table contains foreign keys referencing the primary keys of the related tables. For example:
CREATE TABLE Students ( student_id INT PRIMARY KEY, student_name VARCHAR(100) ); CREATE TABLE Courses ( course_id INT PRIMARY KEY, course_name VARCHAR(100) ); CREATE TABLE Enrollments ( student_id INT, course_id INT, PRIMARY KEY (student_id, course_id), FOREIGN KEY (student_id) REFERENCES Students(student_id), FOREIGN KEY (course_id) REFERENCES Courses(course_id) );
Here, Enrollments
is the junction table.
A database transaction log records all transactions and changes, aiding in data recovery, concurrency control, and providing an audit trail. It includes transaction ID, operation type, affected data, and timestamps, crucial for rollback and roll-forward operations.
A recursive CTE (Common Table Expression) performs recursive queries, useful for hierarchical data structures. Here’s an example to find an employee hierarchy:
WITH RECURSIVE EmployeeHierarchy AS ( SELECT EmployeeID, ManagerID, EmployeeName FROM Employees WHERE ManagerID IS NULL UNION ALL SELECT e.EmployeeID, e.ManagerID, e.EmployeeName FROM Employees e INNER JOIN EmployeeHierarchy eh ON e.ManagerID = eh.EmployeeID ) SELECT * FROM EmployeeHierarchy;
This CTE retrieves all levels of the hierarchy.
Triggers are stored procedures that execute automatically when specific events occur in a database table. They can enforce business rules, maintain audit trails, and ensure data integrity. For example:
CREATE TRIGGER update_timestamp BEFORE UPDATE ON employees FOR EACH ROW BEGIN SET NEW.last_modified = NOW(); END;
This trigger updates the last_modified
column before any update on the employees
table.
A deadlock occurs when transactions wait indefinitely for each other to release resources. Strategies to resolve deadlocks include prevention, detection, avoidance, and timeouts. Prevention involves acquiring all required locks at once, detection checks for cycles in resource allocation, avoidance carefully allocates resources, and timeouts roll back transactions after a set period.
A full outer join returns all records with matches in either table. If not supported, use a combination of LEFT JOIN, RIGHT JOIN, and UNION:
SELECT table1.*, table2.* FROM table1 LEFT JOIN table2 ON table1.id = table2.id UNION SELECT table1.*, table2.* FROM table1 RIGHT JOIN table2 ON table1.id = table2.id;
This simulates a full outer join.
The CAP theorem states that a distributed system cannot simultaneously provide Consistency, Availability, and Partition Tolerance. Systems must trade-off between these properties, such as prioritizing consistency and partition tolerance (CP) or availability and partition tolerance (AP).
To rank employees by salary within each department, use the RANK() function:
SELECT employee_id, department_id, salary, RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank FROM employees;
This query ranks employees within their department based on salary.
A materialized view stores query results on disk, improving performance for complex queries. Unlike regular views, which are computed dynamically, materialized views need refreshing to reflect data changes. They are useful when query performance is important, and data changes infrequently.
Data integrity ensures data accuracy and consistency. Constraints like primary key, foreign key, unique, not null, check, and default enforce integrity by ensuring data validity and adherence to rules.
Database security protects data from unauthorized access and breaches. Practices include access control, encryption, regular audits, patch management, backup and recovery, and data masking. These measures help maintain data integrity, confidentiality, and availability.
Backing up and recovering a database involves creating copies of the database and restoring it to a previous state. Backup types include full, incremental, and differential. The process involves determining backup type, creating backups, storing them securely, and using tools to restore and apply backups as needed.
NoSQL databases, like MongoDB and Cassandra, handle large volumes of data and frequent schema changes. They differ from relational databases in data model, schema flexibility, scalability, and transaction support. NoSQL databases are suitable for applications with large-scale data storage needs and flexible data models.
NoSQL databases are categorized into document, key-value, column-family, and graph databases, each suited for different use cases. Document databases like MongoDB store data in documents, key-value stores like Redis use key-value pairs, column-family stores like Cassandra handle large data volumes, and graph databases like Neo4j manage complex relationships.