5.5 KiB
Executable File
5.5 KiB
Executable File
Database Queries for Assignment-A2
This file contains all the queries required to build the database
Creating database
CREATE DATABASE Database_A2;
USE Database_A2;
Creating tables and specify primary keys:
CREATE TABLE Account(accountNum INT, branchName VARCHAR(50), balance INT, PRIMARY KEY (accountNum));
CREATE TABLE Branch(branchName VARCHAR(50), branchCity VARCHAR(50), assets INT, PRIMARY KEY (branchName));
CREATE TABLE Customer (customerName VARCHAR(50), customerStreet VARCHAR(50), customerCity VARCHAR(50), PRIMARY KEY (customerName));
CREATE TABLE Depositor (customerName VARCHAR(50), accountNum INT);
CREATE TABLE Loan (loanNum INT, branchName VARCHAR(50), amount INT, PRIMARY KEY (loanNum));
CREATE TABLE Borrower (customerName VARCHAR(50), loanNum INT);
Altering tables to specify foreign keys
ALTER TABLE Account ADD FOREIGN KEY (branchName) REFERENCES Branch(branchName);
ALTER TABLE Depositor ADD FOREIGN KEY (customerName) REFERENCES Customer (customerName);
ALTER TABLE Depositor ADD FOREIGN KEY (accountNum) REFERENCES Account (accountNum);
ALTER TABLE Loan ADD FOREIGN KEY (branchName) REFERENCES Branch (branchName);
ALTER TABLE Borrower ADD FOREIGN KEY (customerName) REFERENCES Customer (customerName);
ALTER TABLE Borrower ADD FOREIGN KEY (loanNum) REFERENCES Loan (loanNum);
Adding data
INSERT INTO Branch (branchName, branchCity, assets) VALUES
("Dhole Patil", "Kharadi", 50000),
("Nagarwala", "Akurdi", 20000),
("Peachtree", "Wakad", 35000),
("Bishops", "Nigdi", 10000),
("Amanora", "Hadapsar", 60000);
INSERT INTO Customer (customerName, customerStreet, customerCity) VALUES
("Kalas", "Airport Road", "Pune"),
("Mehul", "Shahdha", "Nandurbar"),
("Tanmay", "Porwal Road", "Pune"),
("Kshitij", "Hadapasar", "Pune"),
("Aditya", "Mira RD", "Mumbai"),
("Himanshu", "Smart City", "Nandurbar");
INSERT INTO Account (accountNum, branchName, balance) VALUES
(2501, "Dhole Patil", 5000),
(2511, "Nagarwala", 1500),
(2521, "Peachtree", 2000),
(2512, "Bishops", 5000),
(2531, "Amanora", 1000);
INSERT INTO Loan (loanNum, branchName, amount) VALUES
(155, "Dhole Patil", 500),
(156, "Nagarwala", 250),
(157, "Peachtree", 600),
(158, "Bishops", 1400),
(159, "Amanora", 25000);
INSERT INTO Borrower VALUES
("Kalas", 156),
("Mehul", 158),
("Tanmay", 155),
("Kshitij", 157),
("Aditya", 159),
("Himanshu", 158);
INSERT INTO Depositor VALUES
("Kalas", 2511),
("Mehul", 2512),
("Tanmay", 2501),
("Kshitij", 2521),
("Aditya", 2531),
("Himanshu", 2512);
Running queries
- Find the names of all branches in loan relation.
SELECT branchName FROM Loan;
- Find all loan numbers for loans made at Akurdi Branch with loan amount > 12000.
SELECT loanNum FROM Loan WHERE amount>12000;
- Find all customers who have a loan from bank. Find their names,loan_no and loan amount.
SELECT * from Borrower;
SELECT Borrower.loanNum FROM Borrower INNER JOIN Loan ON Borrower.loanNum = Loan.loanNum;
- List all customers in alphabetical order who have loan from Akurdi branch.
SELECT customerName FROM Borrower INNER JOIN Loan ON Borrower.loanNum = Loan.loanNum WHERE branchName = "Akurdi" ORDER BY customerName;
- Find all customers who have an account or loan or both at bank.
SELECT customerName FROM Depositor UNION SELECT customerName FROM Borrower;
- Find all customers who have both account and loan at bank.
SELECT customerName FROM Depositor INTERSECT SELECT customerName FROM Borrower;
- Find all customers who have account but no loan at the bank.
SELECT customerName FROM Depositor WHERE customerName NOT IN (SELECT customerName FROM Borrower);
- Find the average account balance at each branch
SELECT AVG(amount) FROM Loan;
- Find no. of depositors at each branch.
SELECT branchName, COUNT(*) AS noOfDepositors FROM Account JOIN Depositor ON Account.accountNum = Depositor.accountNum GROUP BY branchName;
- Find name of Customer and city where customer name starts with Letter K.
SELECT customerName, customerCity from Customer where customerName like "K%";
- Display distinct cities of branch.
SELECT DISTINCT branchName, branchCity FROM Branch;
- Find the branches where average account balance > 1200
SELECT branchName FROM Account GROUP BY branchName HAVING AVG(balance) > 1200;
- Find number of tuples in customer relation.
SELECT COUNT(*) FROM Customer;
- Calculate total loan amount given by bank.
SELECT SUM(amount) AS amount FROM Loan;
- Delete all loans with loan amount between 1300 and 1500.
DELETE FROM Borrower WHERE loanNum IN (SELECT loanNum FROM Loan WHERE amount > 1300 AND amount < 1500);
DELETE FROM Loan WHERE amount > 1300 AND amount < 1500;
- Delete all tuples at every branch located in Nigdi.
DELETE FROM Borrower WHERE loanNum IN (SELECT loanNum FROM Loan WHERE branchName IN (SELECT branchName FROM Branch WHERE branchCity = "Nigdi"));
DELETE FROM Loan WHERE branchName = (SELECT branchName FROM Branch WHERE branchCity = "Nigdi");
DELETE FROM Depositor WHERE accountNum IN (SELECT accountNum FROM Account WHERE branchName IN (SELECT branchName FROM Branch WHERE branchCity = "Nigdi"));
DELETE FROM Account WHERE branchName = (SELECT branchName FROM Branch WHERE branchCity = "Nigdi");
DELETE FROM Branch WHERE branchName = “Nigdi”;