Skip to content

SQL for Business Analysis Interview Questions for Business Analytics

46 SQL for Business Analysis questions with answers for business analytics roles — 15 easy, 23 medium, 8 hard.

About SQL for Business Analysis

SQL rounds check whether you can pull and validate your own data. Expect joins, GROUP BY and HAVING, subqueries, CTEs and window functions such as ROW_NUMBER, LAG and running totals. Business-flavoured problems like monthly revenue, top customers per segment, retention and funnels are common, along with debugging questions about duplicate rows and wrong totals.

Sheet progressStart practicing
3 QUESTIONS FREESign up to unlock the full question bank and track your progress.Unlock every question and answer, then continue practising company-wise and package-wise.
3 questions free Unlock the rest in seconds
01

What is the difference between SQL and Excel for analysis?

Save to ▾
Show answer
Excel suits small to medium data and quick ad-hoc work; SQL queries large databases directly and repeatably.
02

What is a primary key?

Save to ▾
Show answer
A column or set of columns that uniquely identifies each row in a table and cannot contain NULL.
03

SQL: total revenue per month.

Save to ▾
Show answer
SELECT DATE_TRUNC('month', order_date) AS month, SUM(amount) AS revenue FROM orders GROUP BY 1 ORDER BY 1; -- MySQL: DATE_FORMAT(order_date, '%Y-%m')
04

What is SQL and why do business analysts need it?

Save to ▾
Show answer
A language for querying databases. BAs use it to pull data themselves, validate numbers, check reports and answer questions without waiting for engineers.
05

Difference between INNER JOIN and LEFT JOIN?

Save to ▾
Show answer
INNER returns only matching rows; LEFT returns all rows from the left table and NULLs where there is no match.
06

SQL: top 5 customers by spend.

Save to ▾
Show answer
SELECT customer_id, SUM(amount) AS spend FROM orders GROUP BY customer_id ORDER BY spend DESC LIMIT 5;
07

What are aggregate functions?

Save to ▾
Show answer
Functions that summarise rows into a value, such as COUNT, SUM, AVG, MIN and MAX.
08

What is the difference between UNION and UNION ALL?

Save to ▾
Show answer
Both stack the results of two queries with the same number and type of columns. UNION removes duplicate rows, which needs a sort or hash step and is slower. UNION ALL keeps every row and is faster. Use UNION ALL when combining data such as online and store sales where duplicates are genuine or impossible, and UNION only when you specifically need a distinct list, such as unique customer IDs from two sources.
09

What is a foreign key and what is referential integrity?

Save to ▾
Show answer
A foreign key is a column in one table that refers to the primary key of another, such as orders.customer_id pointing to customers.customer_id. Referential integrity means every foreign key value must exist in the parent table, so you cannot have an order for a customer who does not exist. The database enforces it on insert, update and delete, which keeps joins reliable for reporting.
10

What is an index in a database and when can it hurt performance?

Save to ▾
Show answer
An index is a sorted lookup structure on one or more columns that lets the database find rows without scanning the whole table, much like a book index. It speeds up WHERE filters, joins and ORDER BY on those columns. It hurts when there are too many indexes, because every insert and update must maintain them, and it may not help on low-selectivity columns or when functions are applied to the indexed column.
11

Write a SQL query to find the second-highest salary in an employees table.

Save to ▾
Show answer
SELECT MAX(salary) AS second_highest FROM employees WHERE salary < (SELECT MAX(salary) FROM employees); -- or with a window function SELECT DISTINCT salary FROM ( SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rk FROM employees ) t WHERE rk = 2;
12

Write a SQL query to list customers who have never placed an order.

Save to ▾
Show answer
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; -- alternative SELECT customer_id, name FROM customers c WHERE NOT EXISTS ( SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id );
13

Write a SQL query that groups customers into spend buckets using CASE WHEN.

Save to ▾
Show answer
SELECT CASE WHEN total_spend >= 50000 THEN 'High' WHEN total_spend >= 10000 THEN 'Medium' ELSE 'Low' END AS segment, COUNT(*) AS customers FROM ( SELECT customer_id, SUM(amount) AS total_spend FROM orders GROUP BY customer_id ) t GROUP BY 1 ORDER BY customers DESC;
14

What is the difference between DELETE, TRUNCATE and DROP in SQL?

Save to ▾
Show answer
DELETE removes selected rows using a WHERE clause, logs each row and can usually be rolled back inside a transaction. TRUNCATE removes all rows quickly by deallocating data pages, keeps the table structure and normally resets identity counters; rollback behaviour depends on the database. DROP removes the whole table, including its structure, indexes and permissions. Analysts mostly use DELETE carefully on staging tables and should rarely need TRUNCATE or DROP on production data.
15

What is COALESCE in SQL and how does it differ from ISNULL or IFNULL?

Save to ▾
Show answer
COALESCE returns the first non-NULL value from a list of expressions, for example COALESCE(mobile, landline, 'Not available'). It is ANSI standard and works in all major databases. ISNULL (SQL Server) and IFNULL (MySQL) accept only two arguments and are vendor-specific. Analysts commonly use COALESCE to show zero instead of NULL after a LEFT JOIN, such as COALESCE(SUM(o.amount), 0), so that customers without orders appear with zero revenue.
16

What is the difference between COUNT(*) and COUNT(column)?

Save to ▾
Show answer
COUNT(*) counts all rows; COUNT(column) counts only rows where that column is not NULL.
17

What is a subquery and when would you use it?

Save to ▾
Show answer
A query nested inside another, used for filtering or calculating values such as customers whose spend is above the average.
18

How do you find duplicate customers?

Save to ▾
Show answer
SELECT email, COUNT(*) FROM customers GROUP BY email HAVING COUNT(*) > 1;
19

How do you write a query to find users active in consecutive months?

Save to ▾
Show answer
Get distinct user-month pairs, self-join or use LAG on month per user, and keep rows where the previous month is exactly one month earlier.
20

What does GROUP BY do and how is HAVING different from WHERE?

Save to ▾
Show answer
GROUP BY aggregates rows by column; WHERE filters before grouping and HAVING filters after, using aggregate conditions.
21

What is the difference between OLTP and OLAP systems?

Save to ▾
Show answer
OLTP handles many small day-to-day transactions; OLAP supports analysis over large historical data with aggregates and reporting.
22

How do you validate data before analysis?

Save to ▾
Show answer
Check row counts against the source, nulls, duplicates on keys, value ranges, date coverage and that totals reconcile with finance reports.
23

How do you calculate monthly revenue growth in SQL?

Save to ▾
Show answer
Aggregate revenue per month and use LAG(revenue) OVER (ORDER BY month) to compute (revenue - previous) / previous.
24

What is a CTE?

Save to ▾
Show answer
A named temporary result introduced with WITH that makes long queries readable and reusable in the same statement.
25

How do you find the top 5 customers by revenue?

Save to ▾
Show answer
SELECT customer_id, SUM(amount) AS revenue FROM orders GROUP BY customer_id ORDER BY revenue DESC LIMIT 5;
26

How do you handle NULL values when calculating averages?

Save to ▾
Show answer
AVG ignores NULLs; use COALESCE(col, 0) if NULLs should count as zero, depending on the business meaning.
27

What are window functions in SQL and how are they different from GROUP BY?

Save to ▾
Show answer
A window function calculates across a set of related rows using OVER(), but it keeps every original row instead of collapsing them. GROUP BY returns one row per group. For example, SUM(amount) OVER (PARTITION BY region) shows each order next to its region total, which helps compute share of total, rankings, running totals and month-over-month change. Common window functions are ROW_NUMBER, RANK, LAG, LEAD and SUM.
28

What is the difference between ROW_NUMBER, RANK and DENSE_RANK?

Save to ▾
Show answer
All three number rows within a window ordered by a column. ROW_NUMBER gives a unique sequence 1, 2, 3 even for ties. RANK gives ties the same number and then skips, so 1, 1, 3. DENSE_RANK gives ties the same number without gaps, so 1, 1, 2. Use ROW_NUMBER to pick exactly one row per group, such as the latest order, and DENSE_RANK for 'top 3 distinct salary levels' type questions.
29

After joining orders to a payments table, your total revenue doubled. What went wrong?

Save to ▾
Show answer
The join created a fan-out: some orders have more than one payment row, such as split payments or retries, so each order amount is repeated. Check the grain by counting rows per order_id on both sides. Fix it by aggregating payments to one row per order in a CTE before joining, joining on the full key, or summing from the table whose grain matches the metric. Always compare row counts before and after a join.
30

Two analysts report different monthly active user numbers from the same database. How do you resolve it?

Save to ▾
Show answer
Put both queries side by side and compare definitions: which events count as active, time zone, calendar versus rolling month, test and internal accounts, deleted users, and DISTINCT on user ID versus device ID. Run both on a single day to find where they diverge. Agree on one definition with the metric owner, document it, and publish it as a shared view or metric so everyone queries the same logic.
31

Write a SQL query for the top 3 products by revenue in each category.

Save to ▾
Show answer
WITH ranked AS ( SELECT category, product_id, SUM(amount) AS revenue, DENSE_RANK() OVER (PARTITION BY category ORDER BY SUM(amount) DESC) AS rk FROM sales GROUP BY category, product_id ) SELECT category, product_id, revenue FROM ranked WHERE rk <= 3 ORDER BY category, rk;
32

Write a SQL query to calculate the repeat purchase rate.

Save to ▾
Show answer
WITH per_customer AS ( SELECT customer_id, COUNT(DISTINCT order_id) AS orders FROM orders GROUP BY customer_id ) SELECT ROUND(100.0 * SUM(CASE WHEN orders >= 2 THEN 1 ELSE 0 END) / COUNT(*), 2) AS repeat_rate_pct FROM per_customer;
33

Write a SQL query to get each customer's first order date and first order amount.

Save to ▾
Show answer
WITH ordered AS ( SELECT customer_id, order_date, amount, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date, order_id) AS rn FROM orders ) SELECT customer_id, order_date AS first_order_date, amount AS first_order_amount FROM ordered WHERE rn = 1;
34

Write a SQL query to show cumulative revenue by month.

Save to ▾
Show answer
WITH monthly AS ( SELECT DATE_TRUNC('month', order_date) AS month, SUM(amount) AS revenue FROM orders GROUP BY 1 ) SELECT month, revenue, SUM(revenue) OVER (ORDER BY month) AS cumulative_revenue FROM monthly ORDER BY month; -- add PARTITION BY EXTRACT(YEAR FROM month) to reset each year
35

Write a SQL query that shows revenue per region with one column per quarter.

Save to ▾
Show answer
SELECT region, SUM(CASE WHEN EXTRACT(QUARTER FROM order_date) = 1 THEN amount ELSE 0 END) AS q1, SUM(CASE WHEN EXTRACT(QUARTER FROM order_date) = 2 THEN amount ELSE 0 END) AS q2, SUM(CASE WHEN EXTRACT(QUARTER FROM order_date) = 3 THEN amount ELSE 0 END) AS q3, SUM(CASE WHEN EXTRACT(QUARTER FROM order_date) = 4 THEN amount ELSE 0 END) AS q4 FROM orders WHERE EXTRACT(YEAR FROM order_date) = 2026 GROUP BY region;
36

Tell me about a time a SQL query you wrote gave wrong results. How did you catch it?

Save to ▾
Show answer
Situation: my weekly sales report showed a 12 percent jump that no one could explain. Task: verify the number before it reached leadership. Action: I compared row counts before and after each join and found a new promotions table had multiple rows per order, duplicating revenue. I aggregated promotions per order first and added a test comparing totals with the source. Result: the corrected report went out the same day, and I now check grain on every new join.
37

What is a self join? Give a business example.

Save to ▾
Show answer
A self join joins a table to itself using two aliases, so you can compare rows within the same table. A classic business example is an employees table with a manager_id column: joining employees e to employees m on e.manager_id = m.employee_id lists each employee with their manager's name. Another example is finding customers who placed two orders on the same day by joining orders to orders on customer_id and date with different order IDs.
38

Write a SQL query to find the percentage of orders delivered late in each city.

Save to ▾
Show answer
-- late = delivered after the promised date SELECT city, COUNT(*) AS total_orders, SUM(CASE WHEN delivered_at > promised_at THEN 1 ELSE 0 END) AS late_orders, ROUND(100.0 * SUM(CASE WHEN delivered_at > promised_at THEN 1 ELSE 0 END) / COUNT(*), 2) AS late_pct FROM orders WHERE status = 'DELIVERED' GROUP BY city ORDER BY late_pct DESC;
39

SQL: month-over-month revenue growth.

Save to ▾
Show answer
WITH m AS (SELECT month, SUM(amount) AS rev FROM orders GROUP BY month) SELECT month, rev, ROUND(100.0 * (rev - LAG(rev) OVER (ORDER BY month)) / LAG(rev) OVER (ORDER BY month), 1) AS mom_pct FROM m;
40

What is normalisation, and why are analytics tables often denormalised?

Save to ▾
Show answer
Normalisation splits data into related tables to remove redundancy and update anomalies, which suits transactional systems that write often. Analytics workloads read far more than they write, so warehouses often denormalise into wide fact and dimension tables or star schemas. That reduces joins, makes queries simpler for analysts and speeds up aggregation, at the cost of extra storage and the need for controlled loading pipelines.
41

What is the logical order in which a SQL SELECT query is executed?

Save to ▾
Show answer
Logically the database processes FROM and JOIN first, then WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY and finally LIMIT or TOP. This explains common errors: you cannot use a SELECT alias in WHERE because WHERE runs earlier, aggregate conditions must go in HAVING, and window functions cannot be filtered in WHERE, so you wrap them in a CTE or subquery. The optimiser may physically reorder steps, but the result follows this order.
42

A report query on a 200 million row table takes 15 minutes. How do you optimise it?

Save to ▾
Show answer
Read the execution plan to find full scans and expensive joins. Filter early on partitioned or indexed columns such as date, select only needed columns instead of SELECT *, and avoid functions on filter columns. Pre-aggregate in a CTE before joining, replace correlated subqueries with joins or window functions, and check join keys have matching types. For repeated use, build a summary table or materialised view refreshed daily.
43

Your LEFT JOIN report should list all customers, but customers without orders are missing. Why?

Save to ▾
Show answer
A condition on the right table was probably placed in the WHERE clause, for example WHERE o.order_date >= '2026-01-01'. For customers with no orders, o.order_date is NULL, so WHERE removes them and the query behaves like an INNER JOIN. Move the condition into the ON clause, as in LEFT JOIN orders o ON o.customer_id = c.id AND o.order_date >= '2026-01-01', or explicitly allow NULLs.
44

Write a SQL query for a 7-day rolling average of daily revenue.

Save to ▾
Show answer
WITH daily AS ( SELECT order_date, SUM(amount) AS revenue FROM orders GROUP BY order_date ) SELECT order_date, revenue, AVG(revenue) OVER (ORDER BY order_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS rolling_7d FROM daily ORDER BY order_date; -- fill missing dates from a calendar table first, or gaps skew the average
45

Write a SQL query for a signup to purchase funnel from an events table.

Save to ▾
Show answer
WITH steps AS ( SELECT user_id, MAX(CASE WHEN event = 'signup' THEN 1 ELSE 0 END) AS s1, MAX(CASE WHEN event = 'add_to_cart' THEN 1 ELSE 0 END) AS s2, MAX(CASE WHEN event = 'purchase' THEN 1 ELSE 0 END) AS s3 FROM events WHERE event_date >= '2026-10-01' GROUP BY user_id ) SELECT SUM(s1) AS signups, SUM(s1 * s2) AS carts, SUM(s1 * s2 * s3) AS purchases, ROUND(100.0 * SUM(s1 * s2 * s3) / NULLIF(SUM(s1), 0), 2) AS conv_pct FROM steps;
46

A query using NOT IN to find customers without orders returns zero rows, although you know such customers exist. Why?

Save to ▾
Show answer
The subquery probably returns at least one NULL, for example SELECT customer_id FROM orders where some orders have a NULL customer_id. With NOT IN, comparing any value to NULL gives UNKNOWN, so no row passes the condition. Fix it by using NOT EXISTS with a correlated subquery, or a LEFT JOIN with WHERE o.customer_id IS NULL, or by adding WHERE customer_id IS NOT NULL inside the subquery. NOT EXISTS is the safest habit.

Frequently asked questions

How important is SQL for Business Analysis in Business Analytics interviews?

SQL rounds check whether you can pull and validate your own data. Expect joins, GROUP BY and HAVING, subqueries, CTEs and window functions such as ROW_NUMBER, LAG and running totals. Business-flavoured problems like monthly revenue, top customers per segment, retention and funnels are common, along with debugging questions about duplicate rows and wrong totals.

How should I use the SQL for Business Analysis sheet?

Go top to bottom — easy to hard. Answer before opening each answer, tick what you solved and save misses to Last-Minute Revision.