If you are preparing for a data analyst, business intelligence, or data engineering role, SQL will definitely come up.
But here is the truth:
Most candidates practice syntax.
Interviewers test thinking.
In this guide, I will walk you through 25 SQL interview questions with real answers from beginner to advanced level — so you can confidently explain both the logic and the query.
Basic SQL Interview Questions
1. What is the difference between WHERE and HAVING?
WHEREfilters rows before aggregationHAVINGfilters afterGROUP BY
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
2. What is the difference between INNER JOIN and LEFT JOIN?
INNER JOINreturns matching records from both tablesLEFT JOINreturns all records from the left table and matched records from the right table
3. How do you remove duplicates?
SELECT DISTINCT column_name
FROM table_name;
4. What does GROUP BY do?
It groups rows with the same values so aggregate functions (SUM, COUNT, AVG) can be applied.
5. How do you sort results?
SELECT *
FROM employees
ORDER BY salary DESC;
Intermediate SQL Interview Questions
6. How do you find the second highest salary?
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
7. How do you count NULL values?
SELECT COUNT(*) - COUNT(column_name)
FROM table_name;
8. What is a window function?
A function that performs calculations across a set of rows related to the current row.
Example:
SELECT employee_id,
salary,
RANK() OVER (ORDER BY salary DESC) AS salary_rank
FROM employees;
9. Difference between RANK() and DENSE_RANK()?
RANK()skips numbers after tiesDENSE_RANK()does not skip numbers
10. How do you use CASE statements?
SELECT employee_name,
CASE
WHEN salary > 100000 THEN 'High'
ELSE 'Standard'
END AS salary_category
FROM employees;
Advanced SQL Interview Questions
11. How do you identify duplicate records?
SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
HAVING COUNT(*) > 1;
12. What is the difference between UNION and UNION ALL?
UNIONremoves duplicatesUNION ALLkeeps duplicates and is faster
13. How do you calculate running totals?
SELECT order_date,
SUM(sales) OVER (ORDER BY order_date) AS running_total
FROM orders;
14. How do you find missing values between dates?
Use a calendar table and LEFT JOIN to detect gaps.
15. What is an index?
An index improves query performance by speeding up data retrieval.
Scenario-Based SQL Interview Questions
16. Find customers who never placed orders
SELECT c.customer_id
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.customer_id IS NULL;
17. Calculate month-over-month growth
SELECT month,
revenue,
revenue - LAG(revenue) OVER (ORDER BY month) AS mom_growth
FROM sales;
18. Find top 3 products by sales
SELECT product_name
FROM (
SELECT product_name,
RANK() OVER (ORDER BY SUM(sales) DESC) AS rnk
FROM sales
GROUP BY product_name
) ranked
WHERE rnk <= 3;
19. How do you optimize slow queries?
- Add indexes
- Avoid SELECT *
- Filter early
- Use proper joins
- Analyze execution plans
20. What is normalization?
A database design technique to reduce redundancy and improve data integrity.
Practical Data Analyst SQL Questions
21. How do you pivot data?
Use CASE statements or built-in PIVOT functions (depending on database).
22. How do you handle NULL values?
SELECT COALESCE(column_name, 0)
FROM table_name;
23. Difference between DELETE, TRUNCATE, and DROP?
DELETEremoves rowsTRUNCATEremoves all rows quicklyDROPremoves the entire table
24. What is a CTE?
A Common Table Expression improves query readability.
WITH revenue_cte AS (
SELECT customer_id, SUM(amount) AS total_spent
FROM orders
GROUP BY customer_id
)
SELECT *
FROM revenue_cte
WHERE total_spent > 1000;
25. What is the difference between a subquery and a join?
- A subquery is a query inside another query
- A join combines rows from multiple tables
If you average around 250 impressions per day on codewithfimi.com and want more clicks, SQL interview content is a strong traffic driver. It targets:
- Job seekers
- Bootcamp students
- Career switchers
- Junior analysts
To stand out, do not just memorize answers. Practice explaining:
- Why the query works
- When to use it
- Performance trade-offs
That is what separates average candidates from strong analysts.
FAQs
Are SQL interview questions difficult?
They range from basic syntax to advanced window functions and optimization.
How can I practice SQL for interviews?
Use real datasets and practice solving business problems.
Are window functions important for interviews?
Yes. Many companies test ranking, running totals, and time comparisons.
How long should I prepare for SQL interviews?
2–4 weeks of focused daily practice is usually enough.
Do companies test SQL live?
Yes. Many interviews include live SQL coding exercises.