Whether preparing for campus placement technical interviews, university semester viva exams, or senior backend developer certifications, this categorized collection of 100+ Essential SQL Questions & Answers provides the exact explanations and code snippets interviewers expect.
Category 1: Core Fundamentals & Architecture (Q1 - Q25)
Q1: What are Edgar Codd's 12 Rules for Relational Databases?
Proposed by Dr. Edgar F. Codd in 1985, these 12 rules (numbered 0 to 12) define what constitutes a true Relational Database Management System. Key rules include the Information Rule (all information must be represented logically as table values), Guaranteed Access Rule (every atomic datum is accessible via table name, primary key, and column name), and Systematic Treatment of Null Values.
Q2: What is Database Normalization and what are 1NF, 2NF, and 3NF?
Normalization is the structural design technique of organizing tables to eliminate data redundancy and prevent insertion, update, and deletion anomalies:
• 1NF (First Normal Form): All column values must be atomic (no arrays or multi-valued lists) and each record must have a unique identifier.
• 2NF (Second Normal Form): Must be in 1NF, and all non-key columns must depend on the entire primary key (eliminates partial functional dependencies in composite keys).
• 3NF (Third Normal Form): Must be in 2NF, and non-key columns must not depend on other non-key columns (eliminates transitive dependencies).
Q3: What is the difference between Primary Key and Unique Key?
A table can have only ONE Primary Key, and it strictly forbids NULL values. A table can have MULTIPLE Unique Keys, and Unique columns permit NULL values in standard SQL.
Category 2: Query Writing & Complex Joins (Q26 - Q60)
Q26: How do you find duplicate rows in a table?
SELECT email, COUNT(*) AS occurrences
FROM students
GROUP BY email
HAVING COUNT(*) > 1;
Q27: How do you delete duplicate rows while keeping one copy?
-- Using CTE and ROW_NUMBER() in PostgreSQL / SQL Server / MySQL 8.0:
WITH RankedDuplicates AS (
SELECT id, ROW_NUMBER() OVER (PARTITION BY email ORDER BY id) as rn
FROM students
)
DELETE FROM students
WHERE id IN (SELECT id FROM RankedDuplicates WHERE rn > 1);
Q28: How do you find the Nth highest salary in SQL?
-- Standard Modern Approach using DENSE_RANK():
WITH SalaryRank AS (
SELECT emp_name, salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
)
SELECT emp_name, salary
FROM SalaryRank
WHERE rnk = 2; -- Change '2' to any N!
Category 3: Advanced Optimization & Storage (Q61 - Q100+)
Q61: What is a Deadlock and how does an RDBMS resolve it?
A Deadlock occurs when Transaction A holds a lock on Resource 1 and waits for Resource 2, while Transaction B holds a lock on Resource 2 and waits for Resource 1. Neither can proceed. Modern engines implement Deadlock Detection algorithms (using wait-for graphs); when detected, the engine automatically selects the cheaper transaction as the "victim" and rolls it back with an error.
Q62: What is MVCC (Multi-Version Concurrency Control)?
MVCC is the concurrency mechanism used by PostgreSQL and MySQL InnoDB where readers do not block writers, and writers do not block readers. When a row is updated, the engine retains old versions in an Undo Log, presenting a consistent historical snapshot to read transactions without acquiring intrusive table locks.
Next, let us explore common pitfalls and troubleshooting in SQL Discussion, Troubleshooting & FAQs!
Frequently Asked Questions (FAQ)
Common questions include: Difference between DELETE, TRUNCATE, and DROP; Difference between Primary Key and Unique Key; Types of joins; Difference between WHERE and HAVING; and finding the Nth highest salary using subqueries or DENSE_RANK().
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);
WITH RankedSalaries AS (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rnk FROM employees) SELECT salary FROM RankedSalaries WHERE rnk = N LIMIT 1;
Related SQL Masterclass Lessons & Recommended Next Topics
Hands-on query exercises to prepare for coding interviews.
Ask questions, report issues, and discuss complex query topics.
Reviewing transaction isolation levels and concurrency.
Explaining B-trees, clustered indexes, and query plans.