Monday , November 30 2026
Top SQL interview questions with answers and tested queries: joins, NULLs, second highest salary, duplicates, top-N per group, window functions and more.

SQL Interview Questions and Answers (with Tested Queries)

Updated: 29 November 2026Level: All levelsReading time: about 15 minutesSQL · answers tested on SQLite

The most frequently asked SQL interview questions for data analyst, data scientist and data engineer roles – from basics to tricky query problems – with clear answers and tested solutions.

Sample Table for the Query Questions

Example
CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT, dept TEXT, salary INTEGER, manager_id INTEGER);
INSERT INTO employees VALUES
(1,'Aman','IT',90000,NULL), (2,'Riya','IT',75000,1), (3,'Karan','HR',60000,1),
(4,'Neha','IT',75000,2), (5,'Simran','HR',65000,3), (6,'Arjun','Sales',50000,1),
(7,'Riya','IT',75000,1);      -- a duplicate employee row

Basic Questions

Q: What is the difference between WHERE and HAVING?
WHERE filters rows before grouping; HAVING filters groups after GROUP BY and can use aggregates like COUNT(*).

Q: What are the types of JOIN?
INNER (matching rows), LEFT (all left + matches), RIGHT (all right + matches), FULL OUTER (all rows from both), CROSS (every combination) and SELF join (a table joined to itself).

Q: Primary key vs unique key?
Both enforce uniqueness; a table has one primary key and it cannot be NULL, while it can have several unique keys that may allow NULL (depending on the database).

Q: DELETE vs TRUNCATE vs DROP?
DELETE removes chosen rows (can use WHERE, can be rolled back); TRUNCATE removes all rows quickly; DROP removes the whole table and its structure.

Q: What is normalization?
Organising tables to reduce redundancy (1NF, 2NF, 3NF). See Database Design.

Q: What does NULL mean and how do you test for it?
NULL means unknown/missing. Use IS NULL / IS NOT NULL – = NULL never matches. COUNT(column) ignores NULLs; COUNT(*) does not.

Q: UNION vs UNION ALL?
UNION removes duplicates (slower); UNION ALL keeps all rows.

Q: What is the logical order of SQL execution?
FROM/JOIN → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT.

Query Problems

1. Second Highest Salary

Example
SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
Output
75000

2. Nth Highest Salary (e.g. 3rd)

Example
SELECT DISTINCT salary FROM (
  SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS r FROM employees
) t WHERE r = 3;
Output
65000

Explanation: DENSE_RANK gives ties the same rank without gaps, so duplicate salaries are counted once (90000 → 1, 75000 → 2, 65000 → 3).

3. Find Duplicate Rows

Example
SELECT name, dept, salary, COUNT(*) AS copies
FROM employees GROUP BY name, dept, salary HAVING COUNT(*) > 1;
Output
Riya | IT | 75000 | 2

4. Delete Duplicates, Keep One

Example
DELETE FROM employees
WHERE id NOT IN (SELECT MIN(id) FROM employees GROUP BY name, dept, salary);
Output
7 rows → 6 rows

Explanation: Keeps the lowest id of each duplicate group. In MySQL, wrap the subquery in another SELECT because it cannot select from the table being deleted.

5. Highest-Paid Employee in Each Department

Example
SELECT dept, name, salary FROM (
  SELECT *, ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) AS rn FROM employees
) t WHERE rn = 1 ORDER BY dept;
Output
HR    | Simran | 65000
IT    | Aman   | 90000
Sales | Arjun  | 50000

6. Employees Earning Above Their Department Average

Example
SELECT name, dept, salary FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept = e.dept);
Output
Simran | HR | 65000
Aman   | IT | 90000

Explanation: A correlated subquery: the inner query runs per row using that row’s department.

7. Employee and Manager Names (Self Join)

Example
SELECT e.name, m.name AS manager
FROM employees e LEFT JOIN employees m ON m.id = e.manager_id;
Output
Aman   | NULL
Riya   | Aman
Karan  | Aman
Neha   | Riya
Simran | Karan
Arjun  | Aman

8. Departments with at Least 2 Employees

Example
SELECT dept, COUNT(*) AS staff, SUM(salary) AS payroll
FROM employees GROUP BY dept HAVING COUNT(*) >= 2;
Output
HR | 2 | 125000
IT | 4 | 315000
Note: Outputs for 7 and 8 use the table before duplicates were deleted, except where stated.

Advanced Questions

Q: RANK vs DENSE_RANK vs ROW_NUMBER?
For ties: ROW_NUMBER gives 1,2,3; RANK gives 1,1,3; DENSE_RANK gives 1,1,2.

Q: What is a CTE and when is it useful?
A named temporary result (WITH …) that makes complex queries readable and allows recursion. See Advanced SQL.

Q: How do you calculate a running total?
SUM(amount) OVER (ORDER BY date).

Q: How do you find month-over-month growth?
Aggregate by month and use LAG() – see SQL for Data Analytics.

Q: How do indexes work and when can they hurt?
They are sorted structures (B-trees) that speed lookups but slow writes and use space. See SQL Indexing.

Q: What is a view? A stored procedure?
A view is a saved query (Views); a stored procedure is saved procedural logic called with CALL (Stored Procedures).

Q: What is a transaction and ACID?
A group of statements that succeed or fail together; ACID = Atomicity, Consistency, Isolation, Durability.

Q: How do you prevent SQL injection?
Use parameterised queries/placeholders, never string concatenation with user input.

Q: OLTP vs OLAP?
OLTP handles many small transactions (app databases); OLAP handles large analytical queries (warehouses). See Data Warehousing.

Tips for the Interview

  • Say your approach before writing: “group by department, then filter with HAVING”.
  • Ask about ties and NULLs – interviewers love edge cases.
  • Practise daily on real data; try queries with Python’s built-in sqlite3.
  • Revise with the SQL cheat sheet and Practice Quiz.

Series

SQL series: SQL Basics · Advanced SQL · Views · Indexing · Stored Procedures · Database Design · SQL for Analytics · MySQL · PostgreSQL · MongoDB · NoSQL vs SQL · Interview Questions

Summary

Master the basics (joins, WHERE vs HAVING, NULLs, keys), the classic query problems (Nth highest, duplicates, top-N per group, self joins) and window functions – they cover most SQL interviews.

Next: Advanced SQL Queries · Interview Questions (all topics)

About Machine Learning

Check Also

SQL Views: Create, Use and Drop with Examples

SQL views explained: create and query views, security and consistency benefits, limitations and materialized views, with tested examples.