# Database Queries for Assignment-A2 This file contains all the queries required to build the [database](https://git.kska.io/sppu-te-comp-content/DatabaseManagementSystems/src/branch/main/Practical/Assignment-A2/Database_A2.sql) ## Creating database ```sql CREATE DATABASE Database_A2; USE Database_A2; ``` ## Creating tables and specify primary keys: ```sql 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 ```sql 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 ```sql 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 1. Find the names of all branches in loan relation. ```sql SELECT branchName FROM Loan; ``` 2. Find all loan numbers for loans made at Akurdi Branch with loan amount > 12000. ```sql SELECT loanNum FROM Loan WHERE amount>12000; ``` 3. Find all customers who have a loan from bank. Find their names,loan_no and loan amount. ```sql SELECT * from Borrower; SELECT Borrower.loanNum FROM Borrower INNER JOIN Loan ON Borrower.loanNum = Loan.loanNum; ``` 4. List all customers in alphabetical order who have loan from Akurdi branch. ```sql SELECT customerName FROM Borrower INNER JOIN Loan ON Borrower.loanNum = Loan.loanNum WHERE branchName = "Akurdi" ORDER BY customerName; ``` 5. Find all customers who have an account or loan or both at bank. ```sql SELECT customerName FROM Depositor UNION SELECT customerName FROM Borrower; ``` 6. Find all customers who have both account and loan at bank. ```sql SELECT customerName FROM Depositor INTERSECT SELECT customerName FROM Borrower; ``` 7. Find all customers who have account but no loan at the bank. ```sql SELECT customerName FROM Depositor WHERE customerName NOT IN (SELECT customerName FROM Borrower); ``` 8. Find the average account balance at each branch ```sql SELECT AVG(amount) FROM Loan; ``` 9. Find no. of depositors at each branch. ```sql SELECT branchName, COUNT(*) AS noOfDepositors FROM Account JOIN Depositor ON Account.accountNum = Depositor.accountNum GROUP BY branchName; ``` 10. Find name of Customer and city where customer name starts with Letter K. ```sql SELECT customerName, customerCity from Customer where customerName like "K%"; ``` 11. Display distinct cities of branch. ```sql SELECT DISTINCT branchName, branchCity FROM Branch; ``` 12. Find the branches where average account balance > 1200 ```sql SELECT branchName FROM Account GROUP BY branchName HAVING AVG(balance) > 1200; ``` 13. Find number of tuples in customer relation. ```sql SELECT COUNT(*) FROM Customer; ``` 14. Calculate total loan amount given by bank. ```sql SELECT SUM(amount) AS amount FROM Loan; ``` 15. Delete all loans with loan amount between 1300 and 1500. ```sql 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; ``` 16. Delete all tuples at every branch located in Nigdi. ```sql 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”; ``` ---