2024-07-25 14:31:34 +05:30
# Database Queries for Assignment-A2
2024-07-25 12:41:40 +05:30
2024-07-25 14:31:34 +05:30
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 )
2024-07-25 12:41:40 +05:30
## 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),
2024-07-31 21:37:14 +05:30
(158, "Bishops", 1400),
2024-07-25 14:31:34 +05:30
(159, "Amanora", 25000);
2024-07-25 12:41:40 +05:30
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
2024-07-25 14:31:34 +05:30
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
2024-07-31 21:37:14 +05:30
SELECT customerName FROM Borrower INNER JOIN Loan ON Borrower.loanNum = Loan.loanNum WHERE branchName = "Akurdi" ORDER BY customerName;
2024-07-25 14:31:34 +05:30
```
5. Find all customers who have an account or loan or both at bank.
```sql
2024-07-31 21:37:14 +05:30
SELECT customerName FROM Depositor UNION SELECT customerName FROM Borrower;
2024-07-25 14:31:34 +05:30
```
6. Find all customers who have both account and loan at bank.
```sql
2024-07-31 21:37:14 +05:30
SELECT customerName FROM Depositor INTERSECT SELECT customerName FROM Borrower;
2024-07-25 14:31:34 +05:30
```
7. Find all customers who have account but no loan at the bank.
```sql
2024-07-31 21:37:14 +05:30
SELECT customerName FROM Depositor WHERE customerName NOT IN (SELECT customerName FROM Borrower);
2024-07-25 14:31:34 +05:30
```
8. Find the average account balance at each branch
```sql
2024-07-31 21:37:14 +05:30
SELECT AVG(amount) FROM Loan;
2024-07-25 14:31:34 +05:30
```
9. Find no. of depositors at each branch.
```sql
2024-07-31 21:37:14 +05:30
SELECT branchName, COUNT(*) AS noOfDepositors FROM Account JOIN Depositor ON Account.accountNum = Depositor.accountNum GROUP BY branchName;
2024-07-25 14:31:34 +05:30
```
2024-07-31 21:37:14 +05:30
10. Find name of Customer and city where customer name starts with Letter K.
2024-07-25 14:31:34 +05:30
```sql
2024-07-31 21:37:14 +05:30
SELECT customerName, customerCity from Customer where customerName like "K%";
2024-07-25 14:31:34 +05:30
```
11. Display distinct cities of branch.
```sql
2024-07-31 21:37:14 +05:30
SELECT DISTINCT branchName, branchCity FROM Branch;
2024-07-25 14:31:34 +05:30
```
2024-07-31 21:37:14 +05:30
12. Find the branches where average account balance > 1200
2024-07-25 14:31:34 +05:30
```sql
2024-07-31 21:37:14 +05:30
SELECT branchName FROM Account GROUP BY branchName HAVING AVG(balance) > 1200;
2024-07-25 14:31:34 +05:30
```
13. Find number of tuples in customer relation.
```sql
2024-07-31 21:37:14 +05:30
SELECT COUNT(*) FROM Customer;
2024-07-25 14:31:34 +05:30
```
14. Calculate total loan amount given by bank.
```sql
2024-07-31 21:37:14 +05:30
SELECT SUM(amount) AS amount FROM Loan;
2024-07-25 14:31:34 +05:30
```
15. Delete all loans with loan amount between 1300 and 1500.
```sql
2024-07-31 21:37:14 +05:30
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 ;
2024-07-25 14:31:34 +05:30
```
16. Delete all tuples at every branch located in Nigdi.
```sql
2024-07-31 21:37:14 +05:30
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”;
2024-07-25 14:31:34 +05:30
```
---