0% found this document useful (0 votes)
9 views

Project

The document contains 10 SQL queries to analyze transaction data from an online banking database. The queries return information such as the number of unique customers, transactions by region and area, total amounts by transaction type, customers making deposits and withdrawals each month, closing balances by customer and month, recent withdrawal amounts, total deposits over weekly periods, and comparing weekly deposit totals.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Project

The document contains 10 SQL queries to analyze transaction data from an online banking database. The queries return information such as the number of unique customers, transactions by region and area, total amounts by transaction type, customers making deposits and withdrawals each month, closing balances by customer and month, recent withdrawal amounts, total deposits over weekly periods, and comparing weekly deposit totals.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

CREATE DATABASE whonline_banking;

-- 1. How many unique customers are there?


SELECT COUNT(DISTINCT customer_id) AS unique_customers
FROM customer_transactions;

-- 2. How many unique customers are coming from each region?


SELECT r.region_name, COUNT(DISTINCT c.customer_id) AS unique_customers
FROM regions r
JOIN customer_joining_info c ON r.region_id = c.region_id
GROUP BY r.region_name
ORDER BY unique_customers DESC;

-- 3. How many unique customers are coming from each area?


SELECT a.name, COUNT(DISTINCT c.customer_id) AS unique_customers
FROM area a
JOIN customer_joining_info c ON a.area_id = c.area_id
GROUP BY a.name
ORDER BY unique_customers DESC;

-- 4.What is the total amount for each transaction type?


SELECT txn_type, SUM(txn_amount) AS total_amount
FROM customer_transactions
GROUP BY txn_type;

-- 5. For each month - how many customers make more than 1 deposit and 1 withdrawal
in a single month?

SELECT month_id, month, COUNT(*) AS total_customers


FROM(
SELECT customer_id,
MONTH(txn_date) AS month_id,
MONTHNAME(txn_date) AS month,
SUM(CASE WHEN txn_type = 'deposit' THEN txn_amount ELSE txn_amount END)
AS deposit_count,
SUM(CASE WHEN txn_type ='withdrawal' THEN txn_amount ELSE txn_amount END)
AS withdrawal_count
FROM customer_transactions
GROUP BY customer_id, month_id, month)
AS t
WHERE
deposit_count > 1 AND withdrawal_count > 1
GROUP BY month_id, month
ORDER BY month_id;

-- 6. What is closing balance for each customer?

SELECT
customer_id,
SUM(CASE WHEN txn_type = 'deposit' THEN txn_amount ELSE txn_amount END) AS
total_amount
FROM customer_transactions
GROUP BY customer_id;

SELECT
c.customer_id,
COALESCE(SUM(txn_amount)) AS closing_balance
FROM customer_transactions c
GROUP BY c.customer_id
ORDER BY c.customer_id;

-- 7. What is the closing balance for each customer at the end of the month?

SELECT
customer_id,
EXTRACT(MONTH FROM txn_date) AS transaction_month,
SUM(CASE WHEN txn_type = 'deposit' THEN txn_amount ELSE txn_amount END) AS
total_amount
FROM customer_transactions
GROUP BY customer_id, transaction_month;

SELECT
c.customer_id,
EXTRACT(MONTH FROM txn_date) AS month,
COALESCE(SUM(txn_amount),0) AS closing_balance
FROM customer_transactions c
GROUP BY c.customer_id, month
ORDER BY c.customer_id, month;

-- 8. Please show the latest 5 days total withdraw amount.


SELECT DISTINCT DATE(txn_date) AS transaction_day,
SUM(CASE WHEN txn_type = 'withdrawal' THEN txn_amount ELSE 0 END) AS
total_withdrawal_amount,
COUNT(customer_id)AS total_customers
FROM customer_transactions
GROUP BY transaction_day
ORDER BY transaction_day DESC
LIMIT 5;

-- 9. Find out the total deposit amount for every five days consecutive series. You
can assume 1 week = 5 days.Please show the result week wise total amount.

SELECT CEILING(d/5) AS week,


SUM(amount) AS total_deposit
FROM(
SELECT DISTINCT txn_date,
SUM(CASE WHEN txn_type = 'deposit' THEN txn_amount
ELSE 0 END) AS amount,
DENSE_RANK() OVER (ORDER BY txn_date DESC) AS d
FROM customer_transactions
GROUP BY txn_date) AS t
GROUP BY week;

-- 10. Plase compare every weeks total deposit amount by the following previous
week.

SELECT week, total_deposit, week+1 AS next_week, next_total_deposit,


COALESCE(ABS(next_total_deposit-total_deposit),total_deposit) AS difference
FROM
(SELECT CEILING(d/5) AS week,
SUM(amount) AS total_deposit,
LEAD (SUM(amount)) OVER () AS next_total_deposit
FROM(
SELECT DISTINCT txn_date,
SUM(CASE WHEN txn_type = 'deposit' THEN txn_amount
ELSE 0 END ) AS amount,
DENSE_RANK() OVER (ORDER BY txn_date desc) AS d
FROM customer_transactions
GROUP BY txn_date) AS t
GROUP BY week) AS m;

You might also like