SQL Interview Questions for Data Analysts (2026): 25 Questions with Answers and Queries
- Home
- Blog
- Data Analytics & BI
- SQL Interview Questions for Data Analysts (2026): 25 Questions with Answers and Queries
Short answer: data analyst SQL interviews focus on five areas: filtering and sorting, joins, aggregation with GROUP BY and HAVING, window functions (ROW_NUMBER, RANK, running totals), and practical problems such as finding duplicates, the second-highest value or month-on-month change. Interviewers care less about memorised definitions and more about whether you can write a correct query and explain your logic out loud.
This guide groups the questions freshers are asked most, from basic to advanced, with short answers and example queries. The examples use standard SQL that works in MySQL 8+, PostgreSQL and SQL Server with minor changes. Date functions in particular differ between databases.
Basic SQL interview questions
1. What is the difference between WHERE and HAVING?
WHERE filters rows before grouping. HAVING filters groups after aggregation, so it can use aggregate functions such as SUM or COUNT.
SELECT city, COUNT(*) AS customers
FROM customers
WHERE signup_year = 2025
GROUP BY city
HAVING COUNT(*) > 100;
2. What is the logical order of execution of a SELECT query?
FROM and JOIN → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT. This is why you cannot use a SELECT alias inside WHERE in most databases.
3. What is the difference between DELETE, TRUNCATE and DROP?
DELETE removes selected rows and can use WHERE. TRUNCATE removes all rows quickly but keeps the table structure. DROP removes the table itself, structure and all.
4. What is the difference between a primary key and a foreign key?
A primary key uniquely identifies each row in a table and cannot be NULL. A foreign key in one table refers to the primary key of another table, which keeps related data consistent.
5. What is the difference between UNION and UNION ALL?
UNION combines results and removes duplicates. UNION ALL keeps duplicates and is faster. Use UNION ALL unless you specifically need to remove duplicates.
6. COUNT(*) vs COUNT(column) vs COUNT(DISTINCT column)?
COUNT(*) counts all rows. COUNT(column) counts rows where that column is not NULL. COUNT(DISTINCT column) counts unique non-NULL values.
7. How do NULLs behave in SQL?
NULL means unknown, so = NULL never matches. Use IS NULL or IS NOT NULL. Aggregates such as SUM and AVG ignore NULLs. Use COALESCE to replace NULLs with a default value.
Joins interview questions
8. Explain the main types of joins.
| Join | Returns |
|---|---|
| INNER JOIN | Only rows with a match in both tables |
| LEFT JOIN | All rows from the left table, plus matches from the right (NULL where none) |
| RIGHT JOIN | All rows from the right table, plus matches from the left |
| FULL OUTER JOIN | All rows from both tables, matched where possible (not supported directly in MySQL) |
| CROSS JOIN | Every combination of rows from both tables |
| SELF JOIN | A table joined to itself, for example employees and their managers |
9. Find customers who have never placed an order.
SELECT c.customer_id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;
10. Why might a join return more rows than expected?
Because the join key is not unique on one side, so each row matches several rows. Always check the grain of each table (one row per what?) before joining, and verify row counts after.
11. List each employee with their manager’s name.
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id;
Aggregation and grouping questions
12. Find total revenue by month.
-- MySQL
SELECT DATE_FORMAT(order_date, '%Y-%m') AS month,
SUM(amount) AS revenue
FROM orders
GROUP BY DATE_FORMAT(order_date, '%Y-%m')
ORDER BY month;
13. Find duplicate email addresses.
SELECT email, COUNT(*) AS occurrences
FROM customers
GROUP BY email
HAVING COUNT(*) > 1;
14. Find the second-highest salary.
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
Interviewers often follow up with “the Nth highest”, which window functions solve more cleanly (see question 17).
15. What is the difference between a subquery and a CTE?
Both let you build a query in steps. A CTE (the WITH clause) is named and defined at the top, which makes long queries easier to read and lets you reference the same result more than once.
Window function questions
16. What are window functions?
Window functions calculate across a set of rows related to the current row without collapsing them into one row, unlike GROUP BY. They use the OVER (PARTITION BY ... ORDER BY ...) clause.
17. ROW_NUMBER vs RANK vs DENSE_RANK?
| Function | With ties (scores 90, 90, 80) |
|---|---|
| ROW_NUMBER | 1, 2, 3 (always unique) |
| RANK | 1, 1, 3 (skips after ties) |
| DENSE_RANK | 1, 1, 2 (no gaps) |
-- Top 3 salaries in each department
WITH ranked AS (
SELECT name, department, salary,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rnk
FROM employees
)
SELECT * FROM ranked WHERE rnk <= 3;
18. Calculate a running total of daily sales.
SELECT order_date,
daily_sales,
SUM(daily_sales) OVER (ORDER BY order_date) AS running_total
FROM daily_sales;
19. Calculate month-on-month growth.
WITH monthly AS (
SELECT DATE_FORMAT(order_date, '%Y-%m') AS month, SUM(amount) AS revenue
FROM orders
GROUP BY DATE_FORMAT(order_date, '%Y-%m')
)
SELECT month, revenue,
LAG(revenue) OVER (ORDER BY month) AS prev_revenue,
ROUND(100 * (revenue - LAG(revenue) OVER (ORDER BY month))
/ LAG(revenue) OVER (ORDER BY month), 1) AS growth_pct
FROM monthly;
20. Find each customer’s first order.
WITH firsts AS (
SELECT customer_id, order_id, order_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS rn
FROM orders
)
SELECT customer_id, order_id, order_date FROM firsts WHERE rn = 1;
Design and performance questions
21. What is normalisation?
Organising tables to reduce duplicate data and update errors, typically up to third normal form (3NF): each table describes one thing, and every non-key column depends only on the key. Analysts also meet denormalised star schemas in data warehouses, built for fast reporting.
22. What is an index and when does it help?
An index is a data structure that speeds up lookups on a column, much like a book index. It helps filters, joins and sorting on that column, but adds storage and slows inserts and updates slightly.
23. What is the difference between a view and a table?
A table stores data. A view is a saved query that shows data from one or more tables when you query it. Views simplify complex logic and can limit which columns users see.
24. CASE WHEN: how would you bucket customers by spend?
SELECT customer_id, total_spend,
CASE WHEN total_spend >= 50000 THEN 'High'
WHEN total_spend >= 10000 THEN 'Medium'
ELSE 'Low' END AS segment
FROM customer_spend;
25. How do you approach a SQL question you have not seen before?
Clarify the expected output and the grain of each table, write the query in small steps (often with CTEs), check edge cases such as NULLs, ties and duplicates, and explain your reasoning as you go. Interviewers reward clear thinking even when the first attempt needs fixing.
How to prepare in two weeks
- Days 1–4: SELECT, filtering, sorting, aggregation and joins on a sample sales database.
- Days 5–8: subqueries, CTEs, CASE WHEN and date functions.
- Days 9–12: window functions and practical problems (duplicates, top N per group, growth).
- Days 13–14: timed practice and explaining answers aloud.
Build the foundation with top SQL skills for beginners and how SQL solves real business problems. For the full interview process, see our tech interview preparation guide, and for where SQL fits in the bigger picture, the data analyst roadmap.
Learn SQL at TryCatch Classes
Practise SQL hands-on in our MySQL Training, or learn it as part of the complete Data Analytics Training with Advanced Excel, Power BI, Tableau, Python and projects. Learn at our Borivali West classroom or live online, with interview preparation and a 100% placement guarantee.
Frequently asked questions
How much SQL does a data analyst need?
Enough to filter, join, aggregate and use window functions confidently on real business tables. Most fresher interviews test joins, GROUP BY with HAVING and at least one window function problem.
Which SQL database should I practise on?
MySQL or PostgreSQL are good free choices. The core SQL is the same, and interviews rarely depend on database-specific features beyond date functions.
Are SQL interviews written or live?
Both are common. Many companies use an online test followed by a live round where you write and explain queries while sharing your screen or on a whiteboard.
Should I memorise these answers?
Understand them rather than memorise them. Interviewers change the tables and add twists, so practise writing queries from scratch on sample data.
Cover image: photo by Taylor Vick on Unsplash.
Keep reading
Data Analytics & BIData Analyst Interview Questions for Freshers (2026): With AnswersCommon data-analyst interview questions for freshers with short answers, grouped by SQL, Excel, statistics and metrics, visualisation and the case/behavioural round, plus how to prepare and…Data Analytics & BIExcel for Beginners (2026): Formulas, Charts and Data BasicsData Analytics & BISQL for Beginners (2026): Write Your First Queries, Step by Step← Previous articleSelenium vs Playwright: Which Automation Tool Should Freshers Learn in 2026?Next article →Data Analyst Portfolio Projects for Freshers: 7 Ideas That Get You Shortlisted
