0% found this document useful (0 votes)
71 views26 pages

BANK

The document defines database tables for a bank with customer, account, branch and transaction details. It provides SQL queries to insert sample data and retrieve data from the tables including customer details, accounts, transactions sorted based on various conditions.

Uploaded by

Puravi Samal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views26 pages

BANK

The document defines database tables for a bank with customer, account, branch and transaction details. It provides SQL queries to insert sample data and retrieve data from the tables including customer details, accounts, transactions sorted based on various conditions.

Uploaded by

Puravi Samal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

BANK

create database bank;


use bank;

CREATE TABLE customer_master(


CUSTOMER_NUMBER VARCHAR(6),
FIRSTNAME VARCHAR(30),
middlename VARCHAR(30),
lastname VARCHAR(30),
CUSTOMER_CITY VARCHAR(15),
CUSTOMER_CONTACT_NO VARCHAR(10),
occupation VARCHAR(10),
CUSTOMER_DATE_OF_BIRTH DATE,
CONSTRAINT customer_custid_pk PRIMARY KEY
(CUSTOMER_NUMBER));

CREATE TABLE branch_master(


branch_id VARCHAR(6),
branch_name VARCHAR(30),
branch_city VARCHAR(30),
CONSTRAINT branch_bid_pk PRIMARY KEY (branch_id));

CREATE TABLE account_master


(account_number VARCHAR(255),
customer_number VARCHAR(255),
branch_id VARCHAR(255),
opening_balance INT(20),
account_opening_date DATE,
account_type VARCHAR(10),
account_status VARCHAR(10),
PRIMARY KEY (account_number),
FOREIGN KEY (customer_number) references
customer_master(customer_number),
FOREIGN KEY (branch_id) references branch_master(branch_id));

CREATE TABLE transaction_details(


transaction_number VARCHAR(6),
account_number VARCHAR(6),
date_of_transaction DATE,
medium_of_transaction VARCHAR(20),
transaction_type VARCHAR(20),
transaction_amount INT(7),
CONSTRAINT transaction_details_tnumber_pk PRIMARY KEY
(transaction_number),
CONSTRAINT transaction_details_acnumber_fk FOREIGN KEY
(account_number)
REFERENCES account_master (account_number));

CREATE TABLE loan_details


(customer_number varchar(255),
branch_id varchar(255),
loan_amount bigint(20),
foreign key(customer_number) references
customer_master(customer_number));

insert into customer_master values('C00001', 'RAMESH',


'CHANDRA', 'SHARMA', 'DELHI', '9543198345', 'SERVICE'
,'1976-12-06');
insert into customer_master values('C00002', 'AVINASH', 'SUNDER',
'MINHA', 'DELHI', '9876532109' ,'SERVICE', '1974-10-16');
insert into customer_master values('C00003', 'RAHUL', 'NULL',
'RASTOGI', 'DELHI', '9765178901', 'STUDENT', '1981-09-
26');
insert into customer_master values('C00004', 'PARUL', 'NULL',
'GANDHI', 'DELHI', '9876532109' ,'HOUSEWIFE','1976-11-
03');
insert into customer_master values('C00005', 'NAVEEN'
,'CHANDRA', 'AEDEKAR', 'MUMBAI', '8976523190',
'SERVICE' ,'1976-09-19');
insert into customer_master values('C00006', 'CHITRESH',
'NULL', 'BARWE', 'MUMBAI', '7651298321', 'STUDENT'
,'1992-11-06');
insert into customer_master values('C00007', 'AMIT' ,'KUMAR',
'BORKAR', 'MUMBAI', '9875189761', 'STUDENT', '1981-09-
06');
insert into customer_master values('C00008', 'NISHA', NULL,
'DAMLE', 'MUMBAI', '7954198761', 'SERVICE', '1975-12-03');
insert into customer_master values('C00009', 'ABHISHEK',
NULL, 'DUTTA', 'KOLKATA' ,'9856198761', 'SERVICE'
,'1973-05-22');
insert into customer_master values('C00010','SHANKAR' ,NULL,
'NAIR', 'CHENNAI', '8765489076', 'SERVICE', '1976-07-12');

insert into branch_master values('B00001', 'ASAF ALI ROAD','DELHI');


insert into branch_master values('B00002','NEW DELHI MAIN
BRANCH','DELHI');
insert into branch_master values('B00003' ,'DELHI CANTT', 'DELHI');
insert into branch_master values('B00004' ,'JASOLA', 'DELHI');
insert into branch_master values('B00005' ,'MAHIM' ,'MUMBAI');
insert into branch_master values('B00006' ,'VILE PARLE',
'MUMBAI');
insert into branch_master values('B00007', 'MANDVI' ,'MUMBAI');
insert into branch_master values('B00008' ,'JADAVPUR',
'KOLKATA');
insert into branch_master values('B00009' ,'KODAMBAKKAM',
'CHENNAI');

insert into account_master values('A00001' ,'C00001','B00001',1000


,'2012-12-15', 'SAVING', 'ACTIVE');
insert into account_master values('A00002' ,'C00002','B00001',1000
,'2012-06-12' ,'SAVING', 'ACTIVE');
insert into account_master values('A00003' ,'C00003', 'B00002', 1000
,'2012-05-17' ,'SAVING', 'ACTIVE');
insert into account_master values('A00004' ,'C00002', 'B00005', 1000
,'2013-01-27' ,'SAVING ','ACTIVE');
insert into account_master values('A00005' ,'C00006', 'B00006', 1000
,'2012-12-17' ,'SAVING','ACTIVE');
insert into account_master values('A00006' ,'C00007', 'B00007', 1000
,'2010-08-12' ,'SAVING ','SUSPENDED');
insert into account_master values('A00007' ,'C00007', 'B00001', 1000
,'2012-10-02' ,'SAVING ','ACTIVE');
insert into account_master values('A00008' ,'C00001','B00003', 1000
,'2009-11-09' ,'SAVING ','TERMINATED');
insert into account_master values('A00009' ,'C00003', 'B00007', 1000
,'2008-11-30' ,'SAVING', 'TERMINATED');
insert into account_master values('A00010' ,'C00004', 'B00002', 1000
,'2013-03-01' ,'SAVING', 'ACTIVE');

insert into transaction_details values('T00001', 'A00001', '2013-01-


01', 'CHEQUE', 'DEPOSIT', 2000);
insert into transaction_details values('T00002' ,'A00001' ,'2013-02-
01' ,'CASH' ,'WITHDRAWAL', 1000);
insert into transaction_details values('T00003', 'A00002 ', '2013-
01-01', 'CASH' ,'DEPOSIT', 2000);
insert into transaction_details values('T00004', 'A00002', '2013-02-
01' , 'CASH' ,'DEPOSIT', 3000);
insert into transaction_details values('T00005', 'A00007', '2013-01-
11', 'CASH' ,'DEPOSIT', 7000);
insert into transaction_details values('T00006', 'A00007', '2013-01-
13', 'CASH' ,'DEPOSIT', 9000);
insert into transaction_details values('T00007', 'A00001', '2013-03-
13', 'CASH' ,'DEPOSIT' ,4000);
insert into transaction_details values('T00008', 'A00001', '2013-03-
14', 'CHEQUE' ,'DEPOSIT' ,3000);
insert into transaction_details values('T00009', 'A00001', '2013-03-
21', 'CASH' ,'WITHDRAWAL' ,9000);
insert into transaction_details values('T00010', 'A00001', '2013-03-
22', 'CASH' ,'WITHDRAWAL' ,2000);
insert into transaction_details values('T00011', 'A00002', '2013-03-
25', 'CASH' ,'WITHDRAWAL' ,7000);
insert into transaction_details values('T00012', 'A00007', '2013-03-
26', 'CASH' ,'WITHDRAWAL' ,2000);

insert into Loan_details values('C00001', 'B00001', 100000);


insert into Loan_details values('C00002', 'B00002', 200000);
insert into Loan_details values('C00009', 'B00008', 400000);
insert into Loan_details values('C00010', 'B00009', 500000);
insert into Loan_details values('C00001', 'B00003', 600000);
insert into Loan_details values('C00002', 'B00001', 600000);
CUSTOMER MASTER
ACCOUNT MASTER

BRANCH MASTER

LOAN DETAILS
TRANSACTION DETAILS

QUERIES

1. Write a query to display account number, customer’s number,


customer’s firstname, lastname, account opening date. Display the
records sorted in ascending order based on account number.
SELECT
a.account_number,c.customer_number,c.firstname,c.lastname,a.acco
unt_number
FROM customer_master c JOIN account_master a ON
c.customer_number=a.customer_number
ORDER BY a.account_number;

2. Write a query to display the number of customer’s from Delhi. Give


the count an alias name of Cust_Count.

SELECT count(customer_number) Cust_Count FROM customer_master


WHERE customer_city='Delhi';
3. Write a query to display the customer number, customer firstname,
account number for the customer’s whose accounts were created
after 15th of any month. Display the records sorted in ascending order
based on customer number and then by account number.

SELECT c.customer_number,c.firstname,a.account_number FROM


account_master a join customer_master c ON
c.customer_number=a.customer_number WHERE
day(a.account_opening_date)>'15' ORDER BY
c.customer_number,a.account_number;

4. Write a query to display customer number, customer's first name,


account number where the account status is terminated. Display the
records sorted in ascending order based on customer number and
then by account number.

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;

5. Write a query to display the total number of withdrawals and total


number of deposits being done by customer whose customer number
ends with 001. The query should display transaction type and the
number of transactions. Give an alias name as Trans_Count for
number of transactions. Display the records sorted in ascending order
based on transaction type.

SELECT transaction_type,count(transaction_number) Trans_Count


FROM account_master am JOIN transaction_details td
ON am.account_number=td.account_number
WHERE customer_number like '%001'
GROUP BY transaction_type
ORDER BY transaction_type;
6. Write a query to display the number of customers who have
registration but no account in the bank. Give the alias name as
Count_Customer for number of customers.
SELECT count(customer_number) Count_Customer FROM
customer_master
WHERE customer_number NOT IN (SELECT customer_number FROM
account_master);

7. Write a query to display account number and total amount


deposited by each account holder (Including the opening balance).
Give the total amount deposited an alias name of Deposit_Amount.
Display the records in sorted order based on account number.
SELECT
a.account_number,a.opening_balance+sum(t.transaction_amount)
FROM account_master a JOIN transaction_details t ON
a.account_number=t.account_number
WHERE t.transaction_type='Deposit' GROUP BY t.account_number;
8. Write a query to display the number of accounts opened in each
city .The Query should display Branch City and number of accounts as
No_of_Accounts.For the branch city where we don’t have any
accounts opened display 0. Display the records in sorted order based
on branch city.
SELECT branch.branch_city, count(account.account_number)
No_of_Accounts
FROM branch_master LEFT JOIN account_master
ON account.branch_id=branch.branch_id
GROUP BY branch.branch_city ORDER BY branch_city;

9. Write a query to display the firstname of the customers who have


more than 1 account. Display the records in sorted order based on
firstname.
SELECT c.firstname FROM
customer_master c JOIN account_master a ON
a.customer_number=c.customer_number
GROUP BY a.customer_number HAVING count(a.account_number)>1;
10. Write a query to display the customer number, customer
firstname, customer lastname who has taken loan from more than 1
branch. Display the records sorted in order based on customer
number.
SELECT c.customer_number,c.firstname,c.lastname FROM
customer_master c JOIN loan_details l ON
c.customer_number=l.customer_number
GROUP BY l.customer_number HAVING count(l.branch_id)>1
ORDER BY c.customer_number;

11. Write a query to display the customer’s number, customer’s


firstname, customer’s city and branch city where the city of the
customer and city of the branch is different. Display the records
sorted in ascending order based on customer number.
SELECT c.customer_number,c.firstname,c.customer_city,b.branch_city
FROM
Customer_master c JOIN Account_master a ON
c.customer_number=a.customer_number
JOIN Branch_master b ON b.branch_id=a.branch_id
WHERE b.branch_city<>c.customer_city
ORDER BY c.customer_number;
12. Write a query to display the number of clients who have asked for
loans but they don’t have any account in the bank though they are
registered customers. Give the count an alias name of Count.
SELECT count(c.customer_number)Count FROM customer_master c
JOIN loan_details l
ON c.customer_number=l.customer_number
WHERE c.customer_number NOT IN (SELECT customer_number FROM
account_master);

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;

15. Write a query to display all those account number, deposit,


withdrawal where withdrawal is more than deposit amount. Hint:
Deposit should include opening balance as well. For example A00011
account opened with Opening Balance 1000 and A00011 deposited
2000 rupees on 2012-12-01 and 3000 rupees on 2012-12-02. The same
account i.e A00011 withdrawn 3000 rupees on 2013-01-01 and 7000
rupees on 2013-01-03. So the total deposited amount is 6000 and
total withdrawal amount is 10000. So withdrawal amount is more
than deposited amount for account number A00011. Display the
records sorted in ascending order based on account number.
SELECT td.account_number,
sum(CASE WHEN transaction_type='Deposit' THEN transaction_amount
END)
+(SELECT opening_balance
FROM account_master where account_number=td.account_number)
Deposit,
sum(CASE WHEN transaction_type='Withdrawal' THEN
transaction_amount END) Withdrawal
FROM transaction_details td
GROUP BY td.account_number
HAVING Withdrawal > Deposit
ORDER BY td.account_number;
(or)
SELECT ifnull(t1.account_number,t2.account_number)
account_number,
t2.d Deposit,ifnull(t1.w,0) Withdrawal 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_master 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.w,0)>t2.d
ORDER BY account_number;

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;

17. Display the customer number, customer's first name, account


number and number of transactions being made by the customers
from each account. Give the alias name for number of transactions as
Count_Trans. Display the records sorted in ascending order based on
customer number and then by account number.
SELECT c.customer_number,c.firstname,t.account_number,
count(t.account_number) Count_Trans
FROM transaction_details t JOIN account_master a ON
a.account_number=t.account_number
JOIN customer c ON c.customer_number=a.customer_number
GROUP BY t.account_number ORDER BY c.customer_number,
a.account_number;

18. Write a query to display the customer’s firstname who have


multiple accounts (atleast 2 accounts). Display the records sorted in
ascending order based on customer's firstname.
SELECT c.firstname FROM
Customer_master c JOIN account_master a ON
c.customer_number=a.customer_number
GROUP BY a.customer_number HAVING count(a.account_number)>1
ORDER BY c.firstname;

19. Write a query to display the customer number, firstname,


lastname for those client where total loan amount taken is maximum
and at least taken from 2 branches. For example the customer C00012
took a loan of 100000 from bank branch with id B00009 and C00012
Took a loan of 500000 from bank branch with id B00010. So total loan
amount for customer C00012 is 600000. C00013 took a loan of 100000
from bank branch B00009 and 200000 from bank branch B00011. So
total loan taken is 300000. So loan taken by C00012 is more then
C00013.
SELECT ld.customer_number, firstname, lastname
FROM customer_master cm JOIN loan_details ld
ON cm.customer_number=ld.customer_number
GROUP BY customer_number
HAVING count(branch_id)>=2 AND sum(loan_amount)>=
ALL(SELECT sum(loan_amount) FROM loan GROUP BY
customer_number);

20. Write a query to display the customer’s number, customer’s


firstname, branch id and loan amount for people who have taken
loans. Display the records sorted in ascending order based on
customer number and then by branch id and then by loan amount.
SELECT c.customer_number,c.firstname,l.branch_id,l.loan_amount
FROM
Customer_master c JOIN loan_details l ON
c.customer_number=l.customer_number
ORDER BY c.customer_number,l.branch_id,l.loan_amount;
21. Write a query to display city name and count of branches in that
city. Give the count of branches an alias name of Count_Branch.
Display the records sorted in ascending order based on city name.
SELECT branch_city,count(branch_id) Count_Branch FROM
Branch_master GROUP BY branch_city
ORDER BY branch_city;

22. Write a query to display account id, customer’s firstname,


customer’s lastname for the customer’s whose account is Active.
Display the records sorted in ascending order based on account id
/account number.
SELECT a.account_number,c.firstname,c.lastname FROM
Customer_master c JOIN account_master a ON
c.customer_number=a.customer_number and
a.account_status='Active'
ORDER BY a.account_number;
23. Write a query to display customer’s number, first name and
middle name. For the customers who don’t have middle name,
display their last name as middle name. Give the alias name as
Middle_Name. Display the records sorted in ascending order based
on customer number.
SELECT customer_number,firstname,ifnull(middlename,lastname)
Middle_name FROM
Customer_master ORDER BY customer_number;

24. Write a query to display the customer number , firstname,


customer’s date of birth . Display the records sorted in ascending
order of date of birth year and within that sort by firstname in
ascending order.
SELECT customer_number,firstname,customer_date_of_birth FROM
Customer_master ORDER BY
year(customer_date_of_birth),customer_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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy