SQL Mini-Assignment โ Banking Dataset
The final SQL test: answer ten real business questions on a banking-style dataset, combining everything from Sessions 02โ08.
The Scenario
You are a data analyst in the analytics team of a bank. The business stakeholders have asked you to answer a series of questions about customers, their accounts, and their transactions. Write one SQL query for each business question below.
The Dataset
customers
| customer_id | customer_name | city | segment | join_date |
|---|---|---|---|---|
| 1 | Aarav Sharma | Mumbai | Retail | 2023-01-15 |
| 2 | Priya Verma | Delhi | Premium | 2022-06-30 |
| 3 | Rahul Mehta | Mumbai | Retail | 2024-03-10 |
| 4 | Sneha Iyer | Bengaluru | Premium | 2021-11-22 |
| 5 | Vikram Singh | Delhi | Retail | 2023-09-05 |
| 6 | Ananya Das | Chennai | Retail | 2022-02-14 |
accounts
| account_id | customer_id | account_type | balance | opened_date |
|---|---|---|---|---|
| 1001 | 1 | Savings | 25000 | 2023-01-15 |
| 1002 | 1 | Current | 40000 | 2023-01-15 |
| 1003 | 2 | Savings | 120000 | 2022-06-30 |
| 1004 | 3 | Savings | 8000 | 2024-03-10 |
| 1005 | 4 | Salary | 95000 | 2021-11-22 |
| 1006 | 5 | Current | 45000 | 2023-09-05 |
| 1007 | 6 | Savings | 15000 | 2022-02-14 |
transactions
| txn_id | account_id | txn_type | amount | txn_date |
|---|---|---|---|---|
| 2001 | 1001 | Credit | 500 | 2024-01-10 |
| 2002 | 1002 | Debit | 200 | 2024-01-15 |
| 2003 | 1003 | Credit | 1200 | 2024-01-20 |
| 2004 | 1004 | Credit | 300 | 2024-02-05 |
| 2005 | 1001 | Credit | 950 | 2024-02-10 |
| 2006 | 1005 | Debit | 1500 | 2024-02-15 |
| 2007 | 1003 | Credit | 600 | 2024-03-01 |
| 2008 | 1002 | Credit | 700 | 2024-03-05 |
The Assignment โ 10 Business Questions
Q1. List each customer with their total balance across all their accounts. Medium
SELECT c.customer_name, SUM(a.balance) AS total_balance
FROM customers c
JOIN accounts a ON c.customer_id = a.customer_id
GROUP BY c.customer_name
ORDER BY total_balance DESC;Q2. Find customers who have never made a transaction. Hard
SELECT c.customer_name
FROM customers c
LEFT JOIN accounts a ON c.customer_id = a.customer_id
LEFT JOIN transactions t ON a.account_id = t.account_id
WHERE t.txn_id IS NULL;
-- โ Vikram Singh, Ananya DasQ3. Find each customer's net cash flow (total credits minus total debits). Hard
SELECT c.customer_name,
SUM(CASE WHEN t.txn_type = 'Credit' THEN t.amount
ELSE -t.amount END) AS net_flow
FROM customers c
JOIN accounts a ON c.customer_id = a.customer_id
JOIN transactions t ON a.account_id = t.account_id
GROUP BY c.customer_name;Q4. Find the top 3 customers by total transaction value. Medium
SELECT c.customer_name, SUM(t.amount) AS total_value
FROM customers c
JOIN accounts a ON c.customer_id = a.customer_id
JOIN transactions t ON a.account_id = t.account_id
GROUP BY c.customer_name
ORDER BY total_value DESC
LIMIT 3;Q5. Find the average account balance for each account type. Medium
SELECT account_type, ROUND(AVG(balance), 0) AS avg_balance
FROM accounts
GROUP BY account_type;Q6. Find customers who joined in 2023 or later AND have made at least one transaction. Hard
SELECT DISTINCT c.customer_name
FROM customers c
JOIN accounts a ON c.customer_id = a.customer_id
JOIN transactions t ON a.account_id = t.account_id
WHERE c.join_date >= '2023-01-01';Q7. Find month-over-month growth in total transaction volume. Hard
WITH monthly AS (
SELECT DATE_TRUNC('month', txn_date) AS month, SUM(amount) AS volume
FROM transactions GROUP BY DATE_TRUNC('month', txn_date)
)
SELECT month, volume,
LAG(volume) OVER (ORDER BY month) AS prev_volume,
volume - LAG(volume) OVER (ORDER BY month) AS change
FROM monthly ORDER BY month;Q8. Segment each customer as High / Medium / Low by total balance (โฅ100k / โฅ30k / else). Medium
SELECT c.customer_name, SUM(a.balance) AS total_balance,
CASE
WHEN SUM(a.balance) >= 100000 THEN 'High'
WHEN SUM(a.balance) >= 30000 THEN 'Medium'
ELSE 'Low'
END AS segment
FROM customers c
JOIN accounts a ON c.customer_id = a.customer_id
GROUP BY c.customer_name;Q9. Find the second-highest account balance. Hard
SELECT MAX(balance) AS second_highest
FROM accounts
WHERE balance < (SELECT MAX(balance) FROM accounts);
-- โ 95000Q10. Rank customers by total balance within each city (top customer per city). Hard
SELECT city, customer_name, total_balance
FROM (
SELECT c.city, c.customer_name, SUM(a.balance) AS total_balance,
ROW_NUMBER() OVER (PARTITION BY c.city ORDER BY SUM(a.balance) DESC) AS rn
FROM customers c
JOIN accounts a ON c.customer_id = a.customer_id
GROUP BY c.city, c.customer_name
) t
WHERE rn = 1;Interview Questions โ Scenario & Banking Cases
These are the scenario-based SQL questions that test whether you can think like an analyst, not just write syntax. Self-test before revealing.
IQ1. Transaction volume dropped last month. What SQL analysis would you perform?
Model answer: "I wouldn't just look at the total. I'd confirm the drop is real (not missing data or a filter change), then break it down โ volume by month, region, product, customer segment, and account type. I'd check whether it's fewer transactions, lower average value, or a specific region."
IQ2. A dashboard number doesn't match the database. What do you check?
Model answer: "I'd check the data source, filters, refresh date, joins, duplicate records, calculation logic, and whether cancelled/reversed transactions are included. Then I'd validate with a direct SQL query and compare it to the dashboard measure."
IQ3. How would you prepare data for a dashboard using SQL?
Model answer: "First I'd understand the KPIs, then write SQL to produce clean, summarized, dashboard-ready tables โ date, region, segment, and the key metrics. I'd check duplicates, missing values, and date formats before connecting it to the dashboard tool."
IQ4. How would you find potentially suspicious or anomalous transactions?
Model answer: "I'd look for outliers โ transactions far above a customer's typical amount, unusual timing, or many transactions in a short window. A starting query might compare each transaction to the customer's average using a window function or subquery."
IQ5. How would you calculate customer lifetime value using SQL?
Model answer: "At its simplest, I'd sum completed transaction value per customer:
SELECT customer_id, SUM(amount) ... GROUP BY customer_id. A fuller definition would also account
for costs, margins, and repeat-purchase behavior."
IQ6. How would you identify inactive or churned customers?
Model answer: "I'd find customers whose most recent transaction is older than a threshold โ
e.g., no activity in the last 90 days โ using MAX(txn_date) with GROUP BY and
HAVING, or a LEFT JOIN to find customers with no transactions at all."
IQ7. How would you find the top 3 customers by transaction value?
Model answer: "I'd join customers to transactions, sum the amount per customer, order by total descending, and limit to 3 โ or use ROW_NUMBER/RANK if I needed the top 3 per region."
IQ8. How would you validate a report's number against the source data?
Model answer: "I'd write a direct SQL query for the metric, check the filters and joins match the report's definition, confirm the data is up to date, and look for duplicates or excluded statuses that could change the count. The goal is one source of truth."
Key Takeaways
Real analysis combines joins, aggregations, CASE, subqueries, and window functions in one query.
For scenario questions, always break a total down by dimension before concluding.
Validate every number against source data โ one source of truth.
State the business recommendation, not just the query result.
You've now covered the full SQL stack โ you're ready for any fresher SQL round.
Objective Questions โ Test Your Understanding
Q1. To find customers who have made NO transactions, which pattern do you use?
Q2. To rank customers by transaction value within each region, useโฆ
Q3. Which expression correctly computes net flow (credits minus debits)?
Q4. "Transaction volume dropped last month" โ what is the FIRST analytical step?
Q5. Which function compares each month's value to the previous month's value?