DBMS and SQL interview questionswith answers and explanations
23 original DBMS and SQL questions of the kind asked in technical MCQ rounds and interviews, each with the answer and a short explanation. To answer them yourself first, open the CS fundamentals practice.
- questions
- 23
- easy
- 12
- medium
- 8
- hard
- 3
Question 1
EasyWhich normal form removes transitive dependencies of non-key attributes on the primary key?
- A1NF
- B2NF
- C3NFAnswer
- D4NF
Explanation
3NF requires that no non-key attribute depends on the key through another non-key attribute.
Question 2
EasyTable Emp(id, dept, salary). Which query returns the number of employees in each department?
- ASELECT dept, COUNT(*) FROM Emp GROUP BY dept;Answer
- BSELECT dept, COUNT(*) FROM Emp;
- CSELECT COUNT(dept) FROM Emp GROUP BY id;
- DSELECT dept FROM Emp HAVING COUNT(*) > 0;
Explanation
GROUP BY dept makes one group per department, and COUNT(*) counts rows in each group.
Question 3
EasyWhich ACID property guarantees that a committed transaction's changes survive a crash?
- AAtomicity
- BConsistency
- CIsolation
- DDurabilityAnswer
Explanation
Durability means committed data is persisted, typically through a write-ahead log.
Question 4
EasyWhich key uniquely identifies each row of a table and can never be NULL?
- AForeign key
- BPrimary keyAnswer
- CA column with a DEFAULT value
- DAny indexed column
Explanation
A primary key must be unique and NOT NULL. A foreign key refers to a key elsewhere and may repeat or be NULL, and an ordinary index does not force uniqueness.
Question 5
EasyWhich SQL statement removes a table completely, both its structure and its data?
- ADELETE FROM
- BTRUNCATE TABLE
- CALTER TABLE
- DDROP TABLEAnswer
Explanation
DROP TABLE removes the table definition along with its rows. DELETE and TRUNCATE remove rows but keep the empty table, and ALTER TABLE changes its structure.
Question 6
EasyWhat is a foreign key?
- AA column whose values must match a key in another table (or the same table)Answer
- BThe primary key of the same table under another name
- CA key stored outside the database
- DA column whose values must all be different
Explanation
A foreign key enforces referential integrity: each value must exist in the referenced key column, such as Orders.customer_id pointing to Customers.id.
Question 7
EasyWhich join returns every row of the left table, with NULLs in the right table's columns where there is no match?
- AINNER JOIN
- BLEFT JOINAnswer
- CRIGHT JOIN
- DCROSS JOIN
Explanation
A LEFT (outer) JOIN keeps all left rows. An INNER JOIN keeps only matching pairs, a RIGHT JOIN keeps all right rows, and a CROSS JOIN pairs every row with every row.
Question 8
EasyWhat is the difference between WHERE and HAVING?
- AHAVING filters rows before grouping; WHERE filters groups
- BThey are interchangeable in every query
- CWHERE can use aggregates such as SUM directly; HAVING cannot
- DWHERE filters rows before grouping; HAVING filters groups after aggregationAnswer
Explanation
WHERE runs on individual rows before GROUP BY, so it cannot use aggregates. HAVING runs on the groups afterwards, so conditions like HAVING COUNT(*) > 5 go there.
Question 9
MediumTable Emp(id, name, salary). Which query returns the second highest distinct salary?
- ASELECT MAX(salary) FROM Emp WHERE salary > (SELECT MAX(salary) FROM Emp);
- BSELECT salary FROM Emp ORDER BY salary DESC LIMIT 2;
- CSELECT MAX(salary) FROM Emp WHERE salary < (SELECT MAX(salary) FROM Emp);Answer
- DSELECT MIN(salary) FROM Emp;
Explanation
The subquery finds the top salary, and the outer MAX picks the largest salary below it. ORDER BY ... LIMIT 2 returns two rows, and both could be the same top salary.
Question 10
MediumRelation R(A, B, C, D) has the functional dependencies A → B, B → C and C → D. What is the candidate key of R?
- AAB
- BB
- CABCD
- DAAnswer
Explanation
From A we get B, then C, then D, so A determines every attribute. Nothing determines A, so it is in every key, and A alone is minimal. AB and ABCD are superkeys but not minimal.
Question 11
MediumR(A, B, C, D, E) has the functional dependencies A → B, B → C and CD → E. What is the closure {A, D}⁺?
- A{A, B, C, D}
- B{A, D}
- C{A, B, C, D, E}Answer
- D{A, B, D}
Explanation
Start with {A, D}. A → B adds B, B → C adds C, and now C and D together give E through CD → E. The closure is {A, B, C, D, E}.
Question 12
MediumA transaction reads a value written by another transaction that has not committed yet. What is this problem called?
- ANon-repeatable read
- BPhantom read
- CDirty readAnswer
- DLost update
Explanation
Reading uncommitted data is a dirty read: if the writer rolls back, the reader used a value that never officially existed.
Question 13
MediumIn the SQL standard, which isolation level prevents dirty reads but still allows non-repeatable reads?
- ARead committedAnswer
- BRead uncommitted
- CRepeatable read
- DSerializable
Explanation
Read committed only lets a transaction see committed data, but a row read twice can change in between if another transaction commits. Repeatable read and serializable prevent that too.
Question 14
MediumTable T has 10 rows, and its column bonus is NULL in 3 of them. What does SELECT COUNT(bonus) FROM T; return?
- A10
- B7Answer
- C3
- DNULL
Explanation
COUNT(column) counts only non-NULL values, so it returns 7. COUNT(*) would count all 10 rows.
Question 15
EasyWhat is the main cost of adding an index on a column?
- ASlower inserts and updates, plus extra storageAnswer
- BSlower SELECT queries that filter on that column
- CDuplicate rows being deleted automatically
- DNULLs no longer being allowed in the column
Explanation
An index speeds up lookups, but every insert, update or delete must also update the index, and the index takes space.
Question 16
MediumTable Enrolment has the composite primary key (StudentID, CourseID), and the column StudentName depends only on StudentID. Which is the lowest normal form this table fails?
- A1NF
- B3NF
- CBCNF
- D2NFAnswer
Explanation
StudentName depends on part of the key, which is a partial dependency, and 2NF forbids exactly that. The fix is a separate Student(StudentID, StudentName) table.
Question 17
HardR(A, B, C) has the functional dependencies AB → C and C → B. Which statement is true?
- AR is in BCNF
- BR is not in 2NF
- CR is in 3NF but not in BCNFAnswer
- DR is not in 3NF
Explanation
The candidate keys are AB and AC, so every attribute is prime. C → B breaks BCNF because C is not a superkey, but 3NF allows it because B is a prime attribute.
Question 18
MediumOrders(id, customer, amount) has the rows (1, 'Asha', 500), (2, 'Ravi', 300), (3, 'Asha', 700), (4, 'Meena', 200) and (5, 'Ravi', 400). Which customers does this query return?
SELECT customer FROM Orders GROUP BY customer HAVING SUM(amount) > 600;- AAsha and RaviAnswer
- BAsha only
- CAsha, Ravi and Meena
- DRavi only
Explanation
The totals are Asha 1,200, Ravi 700 and Meena 200. HAVING keeps the groups above 600: Asha and Ravi.
Question 19
EasyWhich SQL keyword removes duplicate rows from a query's result?
- AUNIQUE
- BDISTINCTAnswer
- CGROUP
- DONLY
Explanation
SELECT DISTINCT returns each different row once. UNIQUE is a constraint on a column, not a query keyword for de-duplicating results.
Question 20
EasyTable A has 4 rows and table B has 5 rows. How many rows does A CROSS JOIN B return?
- A20Answer
- B9
- C5
- D4
Explanation
A cross join pairs every row of A with every row of B: 4 × 5 = 20 rows.
Question 21
HardStudents(id) has the ids 1, 2, 3 and 4. Marks(student_id, marks) has the rows (1, 80), (1, 75) and (3, 90). How many rows does this query return?
SELECT * FROM Students s LEFT JOIN Marks m ON s.id = m.student_id;- A4
- B3
- C5Answer
- D12
Explanation
Student 1 matches two marks rows (2 rows), student 3 matches one (1 row), and students 2 and 4 match none but are kept with NULLs (1 row each). Total = 5.
Question 22
EasyA bank transfer debits one account and credits another. The server crashes after the debit but before the credit. Which ACID property makes sure the debit is undone?
- AConsistency
- BIsolation
- CDurability
- DAtomicityAnswer
Explanation
Atomicity means a transaction happens completely or not at all, so recovery rolls back the half-done transfer. Durability is about keeping committed changes.
Question 23
HardIs this schedule conflict serializable? R1(X), W2(X), W1(X) (R1(X) means transaction 1 reads X; W2(X) means transaction 2 writes X.)
- AYes, it is equivalent to running T1 then T2
- BNo, its precedence graph has a cycleAnswer
- CYes, it is equivalent to running T2 then T1
- DYes, every schedule of two transactions is
Explanation
R1(X) comes before the conflicting W2(X), giving an edge T1 → T2. W2(X) comes before the conflicting W1(X), giving T2 → T1. The cycle means no serial order matches, so it is not conflict serializable.