Skip to content

SQL Interview Questions for Data Analyst

7 SQL questions with answers for data analyst roles. Tick what you have solved to track your progress.

Sheet progressStart practicing
01

What is the difference between WHERE and HAVING?

Show answer
WHERE filters rows before grouping. HAVING filters groups after GROUP BY and can use aggregates like SUM() or COUNT().
02

Find the second highest salary in SQL.

Show answer
SELECT MAX(salary) FROM emp WHERE salary < (SELECT MAX(salary) FROM emp); -- or: DENSE_RANK() OVER (ORDER BY salary DESC) = 2
03

RANK vs DENSE_RANK vs ROW_NUMBER?

Show answer
ROW_NUMBER gives a unique sequence. RANK gives ties the same rank and skips the next (1, 1, 3). DENSE_RANK gives ties the same rank without gaps (1, 1, 2).
04

Write SQL for a running total of sales by date.

Show answer
SELECT order_date, SUM(amount) OVER (ORDER BY order_date) AS running_total FROM sales;
05

Find customers who ordered in January but not in February.

Show answer
SELECT DISTINCT customer_id FROM orders WHERE month = 'Jan' EXCEPT SELECT customer_id FROM orders WHERE month = 'Feb';
06

INNER JOIN vs LEFT JOIN — with an example.

Show answer
INNER JOIN returns only matching rows. LEFT JOIN returns every row from the left table and NULLs where the right table has no match — e.g. all customers, including those with no orders.
07

What is a CTE and when would you use one?

Show answer
A Common Table Expression (WITH name AS (...)) is a named temporary result used inside one query. Use it to break complex logic into readable steps or for recursive queries.