SQL is tested in almost every data analyst interview, usually in two parts: a few theory questions and a live round where you write queries on a sample table. This list covers the 25 questions that come up most often, with short answers and the query you should be able to write from memory.
The queries use two tables: employees (emp_id, name, dept_id, salary, manager_id, hire_date) and departments (dept_id, dept_name).
Basic questions
1. What is the difference between WHERE and HAVING?
WHERE filters rows before grouping; HAVING filters groups after GROUP BY. You cannot use an aggregate like SUM(salary) in WHERE.
2. What is the order in which SQL runs a query?
FROM and JOIN → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT. This is why a column alias from SELECT usually cannot be used in WHERE.
3. What is the difference between DELETE, TRUNCATE and DROP?
DELETE removes chosen rows and can use WHERE. TRUNCATE removes all rows quickly but keeps the table. DROP removes the table itself.
4. What is a primary key and a foreign key?
A primary key uniquely identifies each row and cannot be NULL. A foreign key is a column that refers to the primary key of another table, for example employees.dept_id referring to departments.dept_id.
5. What is the difference between UNION and UNION ALL?
UNION combines results and removes duplicates; UNION ALL keeps duplicates and is faster.
6. How do you handle NULL values?
Test with IS NULL / IS NOT NULL (never = NULL) and replace them with COALESCE(column, default).
7. What is the difference between COUNT(*), COUNT(column) and COUNT(DISTINCT column)?
COUNT(*) counts all rows, COUNT(column) counts non-NULL values, and COUNT(DISTINCT column) counts unique non-NULL values.
Joins
8. Explain INNER, LEFT, RIGHT and FULL JOIN.
INNER returns only matching rows. LEFT returns all rows from the left table plus matches (NULL where none). RIGHT is the mirror of LEFT. FULL returns all rows from both. Visual explanation: SQL JOINs explained.
9. Find employees who are not in any department.
SELECT e.name
FROM employees e
LEFT JOIN departments d ON d.dept_id = e.dept_id
WHERE d.dept_id IS NULL;
10. What is a self join? Show each employee with their manager.
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON m.emp_id = e.manager_id;
The same table is joined to itself using two aliases.
11. Why does a join return more rows than expected?
The join key is not unique on one side, so each row matches several rows. Check with GROUP BY key HAVING COUNT(*) > 1.
Aggregation
12. Total and average salary per department.
SELECT d.dept_name, SUM(e.salary) AS total_salary, AVG(e.salary) AS avg_salary
FROM employees e
JOIN departments d ON d.dept_id = e.dept_id
GROUP BY d.dept_name;
13. Departments with more than 5 employees.
SELECT dept_id, COUNT(*) AS headcount
FROM employees
GROUP BY dept_id
HAVING COUNT(*) > 5;
14. Find duplicate names.
SELECT name, COUNT(*) AS cnt
FROM employees
GROUP BY name
HAVING COUNT(*) > 1;
15. Count employees by salary band.
SELECT
SUM(CASE WHEN salary < 50000 THEN 1 ELSE 0 END) AS below_50k,
SUM(CASE WHEN salary BETWEEN 50000 AND 100000 THEN 1 ELSE 0 END) AS from_50k_to_1l,
SUM(CASE WHEN salary > 100000 THEN 1 ELSE 0 END) AS above_1l
FROM employees;
More on this pattern: SQL CASE WHEN.
Classic problem questions
16. Find the second highest salary.
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
Returns NULL if there is no second value, which is usually what the interviewer wants.
17. Find the Nth highest salary (for example the 3rd).
SELECT DISTINCT salary
FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
) t
WHERE rnk = 3;
DENSE_RANK gives equal salaries the same rank without gaps.
18. Highest-paid employee in each department.
SELECT dept_id, name, salary
FROM (
SELECT dept_id, name, salary,
RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rnk
FROM employees
) t
WHERE rnk = 1;
19. Employees earning more than their department average.
SELECT name, dept_id, salary
FROM (
SELECT name, dept_id, salary,
AVG(salary) OVER (PARTITION BY dept_id) AS dept_avg
FROM employees
) t
WHERE salary > dept_avg;
20. Running total of salary by hire date.
SELECT name, hire_date, salary,
SUM(salary) OVER (ORDER BY hire_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total
FROM employees;
21. Delete duplicate rows but keep one.
WITH d AS (
SELECT emp_id,
ROW_NUMBER() OVER (PARTITION BY name, dept_id, salary ORDER BY emp_id) AS rn
FROM employees
)
DELETE FROM employees
WHERE emp_id IN (SELECT emp_id FROM d WHERE rn > 1);
Run the SELECT part first to check which rows will be removed. This works in PostgreSQL and SQL Server; MySQL does not allow a subquery on the same table you delete from, so there you would join to a derived table or copy the IDs to a temporary table first.
Concepts
22. What is the difference between RANK, DENSE_RANK and ROW_NUMBER?
For salaries 90, 80, 80, 70: ROW_NUMBER gives 1, 2, 3, 4; RANK gives 1, 2, 2, 4; DENSE_RANK gives 1, 2, 2, 3. Details: SQL window functions.
23. What is a CTE and why use it?
A named temporary result written with WITH. It makes multi-step queries easier to read and allows recursion. Comparison: subquery vs CTE.
24. What is an index?
A structure that helps the database find rows faster, like a book index. It speeds up filtering and joins on that column but slows down inserts and updates slightly.
25. What is normalisation?
Organising tables so each fact is stored once, for example keeping department names in departments instead of repeating them in every employee row. It reduces duplicate data and update errors. Analytics models (star schemas) are deliberately less normalised for faster reporting.
How to prepare for the live round
- Practise on a free tool: install PostgreSQL or MySQL, or use an online SQL editor.
- Say your approach out loud before typing: "first I group by department, then I filter with HAVING".
- Check edge cases: ties, NULLs and departments with no employees.
- Write clean SQL: one clause per line, clear aliases.
Also preparing for Excel and Power BI rounds? See Excel interview questions and practise GROUP BY and HAVING.
