DBMS and SQL MCQs with answers and explanations
Study relational models, keys, normalisation, transactions, indexes and SQL query concepts. Practise DBMS questions with explained answers, including SQL topics, for placement tests and GATE CS preparation.
Database Management Systems (DBMS) is software that enables users to create, manage, and manipulate databases. It provides an interface between users/applications and the database, ensuring data integrity, security, and efficient retrieval through structured query languages like SQL. Understanding DBMS fundamentals is essential for technical interviews and software development roles.
Key formulas and rules
8 rulesCartesian Product Cardinality: , with degree
Natural Join Cardinality Bounds: (at most when no attributes overlap, and at most when the join is on a foreign key in referencing a unique primary key in )
Set Operations: Degree of and equals , requiring union-compatibility
First Normal Form (1NF): All column values must be atomic; no repeating groups or multi-valued attributes
Second Normal Form (2NF): Satisfies 1NF and contains no partial dependencies (every non-prime attribute is fully functionally dependent on every candidate key)
Third Normal Form (3NF): Satisfies 2NF and contains no transitive dependencies (for every FD , either is a superkey or is a prime attribute)
Boyce-Codd Normal Form (BCNF): For every non-trivial functional dependency , must be a superkey
ACID Transactions: Atomicity (all-or-nothing), Consistency (integrity constraints preserved), Isolation (serializability), Durability (committed updates persist)
Key concepts
Data Models and Database Architecture
The three-schema architecture consists of: (1) External Level - user views and how data appears to specific users; (2) Conceptual Level - logical structure of the entire database including entities, relationships, and constraints; (3) Internal Level - physical storage structure and access methods. Common data models include: Hierarchical (tree structure), Network (graph structure), Relational (tables with rows and columns), and Object-Oriented. The Relational Model uses tables (relations) where rows are tuples and columns are attributes, with keys (Primary, Foreign, Candidate, Super) defining relationships.
SQL Operations and Query Processing
SQL commands are categorized as: DDL (Data Definition Language) - CREATE, ALTER, DROP, TRUNCATE for schema modification; DML (Data Manipulation Language) - SELECT, INSERT, UPDATE, DELETE for data operations; DCL (Data Control Language) - GRANT, REVOKE for permissions; TCL (Transaction Control Language) - COMMIT, ROLLBACK, SAVEPOINT. Important SQL operations include: JOINs (INNER, LEFT, RIGHT, FULL OUTER, CROSS, SELF), subqueries (correlated and non-correlated), aggregate functions (COUNT, SUM, AVG, MAX, MIN), GROUP BY with HAVING clause, and set operations (UNION, INTERSECT, EXCEPT). Query optimization involves indexes, query rewriting, and understanding execution plans.
Normalization and Functional Dependencies
Normalization is the process of organizing data to reduce redundancy and improve integrity. Functional Dependency (FD): X -> Y means X uniquely determines Y. Closure of an attribute set () with respect to FDs finds all attributes functionally determined by . Normal Forms in order: 1NF eliminates repeating groups; 2NF eliminates partial dependencies (non-prime attributes must depend on entire candidate key); 3NF eliminates transitive dependencies; BCNF requires every determinant to be a superkey. Higher forms (4NF, 5NF) handle multi-valued and join dependencies. Decomposition must be lossless (natural join recovers original) and dependency-preserving (all FDs are preserved in decomposed relations).
Transaction Management and Concurrency Control
A transaction is a logical unit of work with ACID properties: Atomicity (all-or-nothing execution), Consistency (valid state transitions), Isolation (concurrent transactions don't interfere), Durability (committed changes persist). Concurrency issues include: Lost Update, Dirty Read, Non-Repeatable Read, Phantom Read. Locking mechanisms: Binary Locks (S/X), Shared/Exclusive Locks, Two-Phase Locking (2PL - growing then shrinking phase ensures serializability). Deadlock handling: Prevention (wait-die, wound-wait), Avoidance (Banker's algorithm), Detection (wait-for graph), Recovery. Timestamp ordering assigns unique timestamps to transactions. Recovery techniques: Deferred Update (redo only), Immediate Update (undo-redo), Shadow Paging, Checkpoints.
Indexing and Query Optimization
Indexes speed up data retrieval at the cost of storage and maintenance overhead. Types: Primary Index (on ordering key field), Secondary Index (on non-ordering fields), Clustered Index (determines physical order), Non-clustered Index (separate structure with pointers), Dense Index (entry for every record), Sparse Index (entry for each block). B-Trees and B+ Trees are balanced tree structures for efficient searching, insertion, and deletion. Hash indexes use hash functions for direct access. Query optimization involves: cost-based selection of access paths, join ordering, use of indexes, materialized views, and understanding query execution plans. The optimizer estimates costs based on statistics (cardinality, selectivity, distribution).
ER Model and Database Design
The Entity-Relationship (ER) Model is a high-level conceptual data model. Components: Entities (objects with independent existence - strong/weak), Attributes (simple, composite, multi-valued, derived, key), Relationships (associations between entities with cardinality ratios: 1:1, 1:N, M:N). ER Diagram notation: rectangles for entities, ellipses for attributes, diamonds for relationships, lines connecting them with cardinality markers. Extended ER features: Specialization (top-down, ISA hierarchy with disjoint/overlapping constraints), Generalization (bottom-up), Aggregation (treating relationship as entity). Converting ER to Relational: Entities become tables, M:N relationships become junction tables, weak entities include owner's key, ISA hierarchies use single table, multiple tables, or separate relations.
Worked examples
Example 1
Given relation R(A, B, C, D) with functional dependencies: A -> B, B -> C, C -> D. Is R in 3NF? If not, decompose to 3NF.
Solution
Step 1: Find candidate key.
(since , , ).
So is the only candidate key.
Step 2: Check for partial dependencies (2NF).
Since the key is single attribute , there are no partial dependencies. is in 2NF.
Step 3: Check for transitive dependencies (3NF).
and : This is a transitive dependency (non-prime determines non-prime ).
and : This is another transitive dependency.
So is NOT in 3NF.
Step 4: Decompose to 3NF using synthesis algorithm.
From : Create with key
From : Create with key
From : Create with key
Step 5: Check if original key is preserved.
Yes, is in .
Final decomposition: , ,
All are in 3NF and dependency-preserving.
Example 2
Two transactions: T1 reads A, writes A; T2 reads A, writes A. Show a schedule that results in Lost Update and explain how 2PL prevents it.
Solution
Step 1: Lost Update Example (non-serializable schedule):
Time 1: T1: Read(A) ->
Time 2: T2: Read(A) ->
Time 3: T1: , Write(A) ->
Time 4: T2: , Write(A) ->
Result: T1's update is lost! Correct should be 130 (100+10+20).
Step 2: How 2PL Prevents This:
In Two-Phase Locking:
- Growing phase: Acquire locks
- Shrinking phase: Release locks (no new locks after first release)
Step 3: 2PL Schedule:
Time 1: T1: Lock-S(A) -> Read(A)
Time 2: T1: Upgrade to Lock-X(A) -> T1 holds exclusive lock
Time 3: T2: Lock-S(A) -> BLOCKED (T1 has X lock)
Time 4: T1: Write(A), Unlock(A)
Time 5: T2: Lock-S(A) granted -> Read(A) gets 110
Time 6: T2: Upgrade to Lock-X(A) -> Write(A) = 130
Result: 2PL ensures serializability. Final value is correct: 130.
Example 3
Given Employee(EID, Name, DeptID, Salary, ManagerID) and Department(DeptID, DName, Location). Write SQL to find employees who earn more than their department's average salary, and explain if an index would help.
Solution
Step 1: SQL Query:
SELECT e.EID, e.Name, e.Salary, d.DName
FROM Employee e
JOIN Department d ON e.DeptID = d.DeptID
WHERE e.Salary > (
SELECT AVG(e2.Salary)
FROM Employee e2
WHERE e2.DeptID = e.DeptID
);
Step 2: Alternative with JOIN:
SELECT e.EID, e.Name, e.Salary, d.DName, avg_sal.avg_salary
FROM Employee e
JOIN Department d ON e.DeptID = d.DeptID
JOIN (
SELECT DeptID, AVG(Salary) as avg_salary
FROM Employee
GROUP BY DeptID
) avg_sal ON e.DeptID = avg_sal.DeptID
WHERE e.Salary > avg_sal.avg_salary;
Step 3: Index Analysis:
- Index on Employee(DeptID): Helps join and group by operations, significantly improves correlated subquery
- Index on Employee(Salary): Less helpful here (range comparison after join)
- Composite index on Employee(DeptID, Salary): Most effective - supports both grouping and salary comparison
- Index on Department(DeptID): Helps join (usually primary key, already indexed)
Step 4: Query Plan Considerations:
With proper indexing, optimizer can use index seek for department grouping, avoiding full table scan. The correlated subquery version may be rewritten as join by optimizer.
Example 4
Convert this ER scenario to relational schema: University has Departments (DeptID, Name). Each Department has Professors (ProfID, Name, Specialization). A Professor belongs to exactly one Department. Professors teach Courses (CourseID, Title, Credits). A Course can be taught by multiple Professors, and a Professor can teach multiple Courses. Some Courses are prerequisites for others.
Solution
Step 1: Identify Entities and Relationships:
- Department: Strong entity
- Professor: Strong entity, belongs to Department (1:N)
- Course: Strong entity
- Teaches: M:N between Professor and Course
- Prerequisite: M:N self-referencing on Course
Step 2: Relational Schema:
Department(DeptID, Name)
Primary Key: DeptID
Professor(ProfID, Name, Specialization, DeptID)
Primary Key: ProfID
Foreign Key: DeptID REFERENCES Department(DeptID)
(1:N - Professor belongs to one Department)
Course(CourseID, Title, Credits)
Primary Key: CourseID
Teaches(ProfID, CourseID, Semester, Year)
Primary Key: (ProfID, CourseID, Semester, Year)
Foreign Key: ProfID REFERENCES Professor(ProfID)
Foreign Key: CourseID REFERENCES Course(CourseID)
(M:N relationship with its own attributes)
Prerequisite(CourseID, PrereqCourseID)
Primary Key: (CourseID, PrereqCourseID)
Foreign Key: CourseID REFERENCES Course(CourseID)
Foreign Key: PrereqCourseID REFERENCES Course(CourseID)
(M:N self-referencing relationship)
Step 3: Notes:
- Department-Professor is 1:N, so DeptID is stored in Professor
- Teaches is M:N, so it becomes a separate table
- Prerequisite is M:N on same entity, requiring two FKs to Course
- Composite PK in Prerequisite prevents duplicate prerequisites
- Semester and Year are descriptive attributes of the Teaches relationship
Representative solved questions
See the kind of question in this topic before opening the full practice set.
Question 1
Which normal form eliminates partial dependencies?
Answer: B. 2NF
ExplanationStep 1: 1NF eliminates repeating groups and ensures atomic values.
Step 2: 2NF eliminates partial dependencies (non-key attributes must depend on the entire key).
Step 3: A partial dependency occurs when a non-key attribute depends on part of a composite key.
Answer: 2NF
Sources and review notes
This is an AISEA-authored practice question.
Review status: accepted · Reviewed 2026-08-13 · structure and answer-key checks, editorial quality checks, duplicate screening
Question 2
Which type of join returns all records when there is a match in either left or right table?
Answer: C. FULL OUTER JOIN
ExplanationStep 1: INNER JOIN returns only matching records from both tables.
Step 2: LEFT JOIN returns all records from left table and matched records from right.
Step 3: FULL OUTER JOIN returns all records when there is a match in either table, filling NULLs where no match exists.
Answer: FULL OUTER JOIN
Sources and review notes
This is an AISEA-authored practice question.
Review status: accepted · Reviewed 2026-08-13 · structure and answer-key checks, editorial quality checks, duplicate screening
Question 3
Which SQL statement removes all records from a table but keeps the table structure?
Answer: C. TRUNCATE
ExplanationStep 1: DELETE removes specific rows based on WHERE clause (can be rolled back).
Step 2: DROP removes the entire table including structure and data.
Step 3: TRUNCATE removes all rows quickly, cannot be rolled back in some DBMS, preserves table structure.
Answer: TRUNCATE
Sources and review notes
This is an AISEA-authored practice question.
Review status: accepted · Reviewed 2026-08-13 · structure and answer-key checks, editorial quality checks, duplicate screening
Common mistakes and useful habits
- Always identify candidate keys first when analyzing normal forms - every normal form check depends on knowing what the keys are
- For SQL queries involving multiple tables, draw the join diagram mentally: INNER JOIN returns matching rows only, LEFT JOIN returns all from left with NULLs for non-matches
- In transaction questions, check for serializability using precedence graph (conflict serializability) or view equivalence (view serializability)
- Remember: BCNF is stricter than 3NF. A relation in BCNF is automatically in 3NF, but not vice versa. Decomposition to BCNF may lose some functional dependencies
- When calculating index selectivity: High cardinality columns (many unique values) benefit more from B+ tree indexes; low cardinality may favor bitmap indexes
- For ER-to-Relational conversion: 1:N relationships place the '1' side key on the 'N' side; M:N relationships always become a separate table with both keys
Ready to test your understanding?
Work through 194 questions with explanations after each answer.