BANK
BANK
BRANCH MASTER
LOAN DETAILS
TRANSACTION DETAILS
QUERIES
SELECT c.customer_number,c.firstname,a.account_number
FROMaccount_master a JOIN customer_master c
ON c.customer_number=a.customer_number
WHERE a.account_status='Terminated'
ORDER BY c.customer_number,a.account_number;
13. Write a query to display the account number who has done the
highest transaction. For example the account A00023 has done 5
transactions i.e. suppose 3 withdrawal and 2 deposits. Whereas the
account A00024 has done 3 transactions i.e. suppose 2 withdrawals
and 1 deposit. So account number of A00023 should be displayed. In
case of multiple records, display the records sorted in ascending order
based on account number.
SELECT account_number FROM transaction_details
GROUP BY account_number
HAVING count(transaction_number)>=ALL
(SELECT count(transaction_number) FROM transaction_details
GROUP BY account_number) ORDER BY account_number;
14. Write a query to show the branch name,branch city where we
have the maximum customers. For example the branch B00019 has 3
customers, B00020 has 7 and B00021 has 10. So branch id B00021 is
having maximum customers. If B00021 is Koramangla branch
Bangalore, Koramangla branch should be displayed along with city
name Bangalore. In case of multiple records, display the records
sorted in ascending order based on branch name.
SELECT b.branch_name,b.branch_city FROM
Branch_master b JOIN account a ON a.branch_id=b.branch_id
GROUP BY a.branch_id HAVING count(a.customer_number)>=ALL
(SELECT count(customer_number) FROM
Account_master GROUP BY branch_id)
ORDER BY b.branch_name;
16. Write a query to show the balance amount for account number
that ends with 001. Note: Balance amount includes account opening
balance also. Give alias name as Balance_Amount. For example
A00015 is having an opening balance of 1000. A00015 has deposited
2000 on 2012-06-12 and deposited 3000 on 2012-07-13. The same
account has drawn money of 500 on 2012-08-12 , 500 on 2012-09-15,
1000 on 2012-12-17. So balance amount is 4000 i.e (1000 (opening
balance)+2000+3000 ) – (500+500+1000).
SELECT ifnull((SUM(CASE WHEN transaction_type='Deposit'
THEN transaction_amount END)) -
(SUM(CASE WHEN transaction_type='Withdrawal'
THEN transaction_amount END))+(select opening_balance
from account_master where account_number like '%001'),(SUM(CASE
WHEN transaction_type='Deposit'
THEN transaction_amount END))+(select opening_balance
from account_master where account_number like '%001')) AS
Balance_Amount
FROM transaction_details where account_number like '%001';
(or)
SELECT ifnull(t1.account_number,t2.account_number)
account_number,
t2.d-ifnull(t1.w,0) Balance_Amount FROM
(SELECT account_number,transaction_type,sum(transaction_amount)
w from transaction_details
WHERE transaction_type='Withdrawal' GROUP BY account_number) t1
RIGHT JOIN
(SELECT
a.account_number,a.opening_balance+sum(t.transaction_amount) d
FROM account a JOIN transaction_details t ON
a.account_number=t.account_number
WHERE t.transaction_type='Deposit'GROUP BY t.account_number) t2
ON t1.account_number=t2.account_number
WHERE ifnull(t1.account_number,t2.account_number) LIKE '%001'
ORDER BY account_number;
25. Write a query to display the customers firstname, city and account
number whose occupation are not into Business, Service or Student.
Display the records sorted in ascending order based on customer first
name and then by account number.
SELECT c.firstname,c.customer_city,a.account_number FROM
Customer_master c JOIN account_master a ON
a.customer_number=c.customer_number
WHERE c.occupation NOT IN ('Service','Student','Business')
ORDER BY c.firstname,a.account_number;
AIRLINES