SQL Interview Questions: The Complete Guide
Search "SQL interview questions" with a company name in front and you'll find pages for TCS, Infosys, Wipro, Accenture, and Capgemini that all cover almost the same ground — because they genuinely do ask the same core SQL fundamentals. Rather than repeat one question pool five times with a different logo, this guide organizes every commonly reported question by topic, with a short note at the end on which companies lean on which topics.
- WHERE vs HAVING
- JOIN types
- Primary vs foreign vs unique key
- Normalization (1NF–3NF)
- DELETE vs TRUNCATE vs DROP
- Nth highest salary
- Self-joins & date filtering
WHERE vs HAVING
Asked in nearly every service-company interview. WHERE filters individual rows before any grouping happens and cannot reference aggregate functions. HAVING filters groups after GROUP BY has run, and is typically paired with COUNT, SUM, or AVG.
SELECT department, COUNT(*) AS emp_count FROM employees WHERE status = 'active' GROUP BY department HAVING COUNT(*) > 10;
JOIN types
Interviewers expect you to name all four and then write at least one from memory: INNER JOIN (only matching rows in both tables), LEFT JOIN (every row from the left table plus matches from the right), RIGHT JOIN (every row from the right table plus matches from the left), and FULL JOIN (every row from both tables, matched where possible).
-- LEFT JOIN: every customer, even with zero orders SELECT c.name FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE o.id IS NULL; -- customers with NO orders -- Joining 3 tables SELECT o.id, c.name, p.product_name FROM orders o JOIN customers c ON o.customer_id = c.id JOIN products p ON o.product_id = p.id; -- Self-join: employee alongside their manager SELECT e.name AS employee, m.name AS manager FROM employees e LEFT JOIN employees m ON e.manager_id = m.id;
The most common JOIN mistakes in interviews: forgetting the ON condition (which silently produces a cross join), picking the wrong join type when the question says "including rows with no match," not recognizing when a self-join is the right tool, and forgetting GROUP BY after adding an aggregate like COUNT().
Primary key vs foreign key vs unique key
A primary key uniquely identifies each row and cannot be NULL. A foreign key references another table's primary key to enforce referential integrity. A unique key also enforces uniqueness, but a table can have several unique keys, and most databases allow one NULL value in a unique column — a distinction that trips people up if they haven't seen it asked directly.
Normalization: 1NF, 2NF, 3NF
Normalization organizes tables to cut redundancy and prevent update anomalies. 1NF requires atomic column values with no repeating groups. 2NF is 1NF plus every non-key column depending on the entire primary key (matters once you have composite keys). 3NF is 2NF plus removing transitive dependencies — a non-key column should not depend on another non-key column.
DELETE vs TRUNCATE vs DROP
One of the most reliably asked theory questions. DELETE removes rows one at a time, can use a WHERE clause, is logged, and can be rolled back inside a transaction. TRUNCATE removes every row at once, resets identity/auto-increment columns, and cannot take a WHERE clause. DROP removes the entire table structure along with its data.
DELETE FROM employees WHERE department = 'HR'; -- removes matching rows, logged TRUNCATE TABLE employees; -- removes all rows, keeps structure DROP TABLE employees; -- removes table and data entirely
Nth (or second) highest salary
The single most-reported SQL interview question across every company in this guide. Three approaches come up, each worth knowing:
-- 1. LIMIT/OFFSET — simplest, but doesn't handle tied salaries correctly SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 1; -- 2. Correlated subquery — works even on older MySQL without OFFSET SELECT MAX(salary) AS second_highest FROM employees WHERE salary < (SELECT MAX(salary) FROM employees); -- 3. DENSE_RANK — the correct choice when ties must rank identically, -- and the only one that generalizes cleanly to "Nth highest" WITH ranked AS ( SELECT name, salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FROM employees ) SELECT name, salary FROM ranked WHERE rnk = 3; -- change 3 to N
More query-writing round questions
- Find employees who joined in the last 6 months (date filtering — syntax differs between MySQL's
DATE_SUB(CURDATE(), INTERVAL 6 MONTH)and PostgreSQL'sCURRENT_DATE - INTERVAL '6 months'). - Count employees per department using GROUP BY.
- Find departments with more than 5 employees using GROUP BY and HAVING together.
- Find employees earning above the company average using a subquery.
- Explain a correlated subquery vs a regular subquery.
- Explain indexing and how it speeds up a query.
- Explain UNION vs UNION ALL.
- Explain the ACID properties and why they matter for transactions.
Which companies focus on what
The overlap is large, but reported interview patterns lean slightly differently: TCS, Infosys, and Wipro stay closest to core fundamentals — SELECT basics, JOIN types, GROUP BY/HAVING, subqueries, DELETE/TRUNCATE/DROP, and normalization. Accenture and Capgemini ask the same fundamentals but more often layer on a date-filtering query or a "common conceptual questions" round covering ACID, correlated subqueries, and self-joins. None of the five reliably ask anything outside this shared pool — treat company-specific prep as a matter of emphasis, not different material.
Frequently Asked Questions
Mostly no — they draw from the same core pool: JOIN types, WHERE vs HAVING, primary and foreign keys, normalization, DELETE vs TRUNCATE vs DROP, and the Nth highest salary query. TCS, Infosys, and Wipro tend to stay closer to fundamentals; Accenture and Capgemini are slightly more likely to add a date-filtering or self-join question. Preparing by topic covers all five.
The Nth (or second) highest salary query and the WHERE vs HAVING distinction are the two most consistently reported questions across all five companies.
Usually both — written or online assessments often include SQL multiple-choice questions, and the technical interview round follows up with conceptual questions and live query-writing.