3.9 KiB
3.9 KiB
S3 - SQL Queries (in MySQL)
Problem Statement: Consider following Relation Account (Acc_no, branch_name,balance) Branch(branch_name,branch_city,assets) Customer(cust_name,cust_street,cust_city) Depositor(cust_name,acc_no) Loan(loan_no,branch_name,amount) Borrower(cust_name,loan_no) Create above tables with appropriate constraints like primary key, foreign key, not null etc.
- Find the branches where average account balance > 15000.
- Find number of tuples in customer relation.
- Calculate total loan amount given by bank.
- Delete all loans with loan amount between 1300 and 1500.
- Find the average account balance at each branch
- Find name of Customer and city where customer name starts with Letter P.
Creating the database
CREATE DATABASE Bank3;
USE Bank3;
Creating tables:
CREATE TABLE Account (
acc_no INT,
branch_name VARCHAR(255),
balance INT,
PRIMARY KEY (acc_no)
);
CREATE TABLE Branch (
branch_name VARCHAR(255),
branch_city VARCHAR(255),
assets INT,
PRIMARY KEY (branch_name)
);
CREATE TABLE Customer (
cust_name VARCHAR(255),
cust_street VARCHAR(255),
cust_city VARCHAR(255),
PRIMARY KEY (cust_name)
);
CREATE TABLE Depositor (
cust_name VARCHAR(255),
acc_no INT
);
CREATE TABLE Loan (
loan_no INT,
branch_name VARCHAR(255),
amount INT,
PRIMARY KEY (loan_no)
);
CREATE TABLE Borrower (
cust_name VARCHAR(255),
loan_no INT
);
Declaring foreign keys
ALTER TABLE Account ADD FOREIGN KEY (branch_name) REFERENCES Branch (branch_name);
ALTER TABLE Depositor ADD FOREIGN KEY (cust_name) REFERENCES Customer (cust_name);
ALTER TABLE Depositor ADD FOREIGN KEY (acc_no) REFERENCES Account (acc_no);
ALTER TABLE Loan ADD FOREIGN KEY (branch_name) REFERENCES Branch (branch_name);
ALTER TABLE Borrower ADD FOREIGN KEY (cust_name) REFERENCES Customer (cust_name);
ALTER TABLE Borrower ADD FOREIGN KEY (loan_no) REFERENCES Loan (loan_no);
Inserting data
INSERT INTO Branch VALUES
('Wadia College', 'Pune', 50000),
('PES', 'Pune', 65000),
('Lohegaon', 'Pune', 350000),
('Viman Nagar', 'Pune', 850000);
INSERT INTO Customer VALUES
('Kalas', 'Street 12', 'Pune'),
('Himanshu', 'Street 15', 'Pune'),
('Mehul', 'Street 29', 'Pune'),
('Macho', 'Street 59', 'Mumbai'),
('Gundeti', 'Street 40', 'Mumbai'),
('Salvi', 'Street 8', 'Pune'),
('Pintu', 'Street 55', 'Ahemadnagar'),
('Piyush', 'Street 21', 'Assam');
INSERT INTO Account VALUES
(101, 'Lohegaon', 67000),
(102, 'PES', 4324),
(103, 'PES', 54670),
(104, 'Viman Nagar', 5433),
(105, 'Wadia College', 6462);
INSERT INTO Depositor VALUES
('Kalas', 101),
('Macho', 104),
('Gundeti', 105),
('Salvi', 105);
INSERT INTO Loan VALUES
(201, 'Wadia College', 1800),
(202, 'PES', 8500),
(203, 'PES', 15000),
(204, 'Wadia College', 5322),
(205, 'Viman Nagar', 1300),
(206, 'Lohegaon', 1450);
INSERT INTO Borrower VALUES
('Macho', 201),
('Mehul', 202),
('Himanshu', 203),
('Salvi', 204);
Queries
- Find the branches where average account balance > 15000.
SELECT branch_name FROM Account GROUP BY branch_name HAVING AVG(balance) > 15000 ;
- Find number of tuples in customer relation.
SELECT COUNT(*) FROM Customer;
- Calculate total loan amount given by bank.
SELECT SUM(amount) FROM Loan;
- Delete all loans with loan amount between 1300 and 1500.
- Delete the dependent records first(due to foreign key constraints):
DELETE FROM Borrower
WHERE loan_no IN (
SELECT loan_no
FROM Loan
WHERE amount BETWEEN 1300 AND 1500
);
- Now delete the Loan table Records:
DELETE FROM Loan WHERE amount BETWEEN 1300 AND 1500;
- Find the average account balance at each branch
SELECT branch_name, AVG(balance) FROM Account GROUP BY branch_name;
- Find name of Customer and city where customer name starts with letter P.
SELECT cust_name, cust_city FROM Customer WHERE cust_name LIKE "P%";