Simplified queries for A2:

- Creating tables on multiple lines for better understanding
- Changed names of columns to match names in assignment handout
- Updated some values so that each query in implementation gives valid output
- Lastly, simplified some queries and changed column names
This commit is contained in:
K 2024-11-01 02:22:03 +05:30
parent fa91c4f08f
commit 0c887415b3
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F

171
Practical/Assignment-A2/Queries-A2.md Executable file → Normal file
View File

@ -13,74 +13,100 @@ USE Database_A2;
## Creating tables and specify primary keys: ## Creating tables and specify primary keys:
```sql ```sql
CREATE TABLE Account(accountNum INT, branchName VARCHAR(50), balance INT, PRIMARY KEY (accountNum)); CREATE TABLE Account (
CREATE TABLE Branch(branchName VARCHAR(50), branchCity VARCHAR(50), assets INT, PRIMARY KEY (branchName)); acc_no INT,
CREATE TABLE Customer (customerName VARCHAR(50), customerStreet VARCHAR(50), customerCity VARCHAR(50), PRIMARY KEY (customerName)); branch_name VARCHAR(255),
CREATE TABLE Depositor (customerName VARCHAR(50), accountNum INT); balance INT,
CREATE TABLE Loan (loanNum INT, branchName VARCHAR(50), amount INT, PRIMARY KEY (loanNum)); PRIMARY KEY (acc_no)
CREATE TABLE Borrower (customerName VARCHAR(50), loanNum INT); );
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
);
``` ```
## Altering tables to specify foreign keys ## Altering tables to specify foreign keys
```sql ```sql
ALTER TABLE Account ADD FOREIGN KEY (branchName) REFERENCES Branch(branchName); ALTER TABLE Account ADD FOREIGN KEY (branch_name) REFERENCES Branch (branch_name);
ALTER TABLE Depositor ADD FOREIGN KEY (customerName) REFERENCES Customer (customerName); ALTER TABLE Depositor ADD FOREIGN KEY (cust_name) REFERENCES Customer (cust_name);
ALTER TABLE Depositor ADD FOREIGN KEY (accountNum) REFERENCES Account (accountNum); ALTER TABLE Depositor ADD FOREIGN KEY (acc_no) REFERENCES Account (acc_no);
ALTER TABLE Loan ADD FOREIGN KEY (branchName) REFERENCES Branch (branchName); ALTER TABLE Loan ADD FOREIGN KEY (branch_name) REFERENCES Branch (branch_name);
ALTER TABLE Borrower ADD FOREIGN KEY (customerName) REFERENCES Customer (customerName); ALTER TABLE Borrower ADD FOREIGN KEY (cust_name) REFERENCES Customer (cust_name);
ALTER TABLE Borrower ADD FOREIGN KEY (loanNum) REFERENCES Loan (loanNum); ALTER TABLE Borrower ADD FOREIGN KEY (loan_no) REFERENCES Loan (loan_no);
``` ```
## Adding data ## Adding data
```sql ```sql
INSERT INTO Branch (branchName, branchCity, assets) VALUES INSERT INTO Branch (branch_name, branch_city, assets) VALUES
("Dhole Patil", "Kharadi", 50000), ('Akurdi', 'Pune', 5000000),
("Nagarwala", "Akurdi", 20000), ('Nigdi', 'Pune', 3000000),
("Peachtree", "Wakad", 35000), ('Kharadi', 'Pune', 4000000),
("Bishops", "Nigdi", 10000), ('Hadapsar', 'Pune', 3500000),
("Amanora", "Hadapsar", 60000); ('Viman Nagar', 'Pune', 4500000);
INSERT INTO Customer (customerName, customerStreet, customerCity) VALUES INSERT INTO Account (acc_no, branch_name, balance) VALUES
("Kalas", "Airport Road", "Pune"), (101, 'Akurdi', 15000),
("Mehul", "Shahdha", "Nandurbar"), (102, 'Nigdi', 8000),
("Tanmay", "Porwal Road", "Pune"), (103, 'Kharadi', 20000),
("Kshitij", "Hadapasar", "Pune"), (104, 'Hadapsar', 12000),
("Aditya", "Mira RD", "Mumbai"), (105, 'Viman Nagar', 5000);
("Himanshu", "Smart City", "Nandurbar");
INSERT INTO Account (accountNum, branchName, balance) VALUES INSERT INTO Customer (cust_name, cust_street, cust_city) VALUES
(2501, "Dhole Patil", 5000), ('Kalas', 'Street 1', 'Pune'),
(2511, "Nagarwala", 1500), ('Mehul', 'Street 2', 'Pune'),
(2521, "Peachtree", 2000), ('Tanmay', 'Street 3', 'Pune'),
(2512, "Bishops", 5000), ('Kshitij', 'Street 4', 'Pune'),
(2531, "Amanora", 1000); ('Aditya', 'Street 5', 'Pune');
INSERT INTO Loan (loanNum, branchName, amount) VALUES INSERT INTO Depositor (cust_name, acc_no) VALUES
(155, "Dhole Patil", 500), ('Kalas', 101),
(156, "Nagarwala", 250), ('Mehul', 102),
(157, "Peachtree", 600), ('Tanmay', 103),
(158, "Bishops", 1400), ('Kshitij', 101),
(159, "Amanora", 25000); ('Aditya', 104);
INSERT INTO Borrower VALUES INSERT INTO Loan (loan_no, branch_name, amount) VALUES
("Kalas", 156), (201, 'Akurdi', 15000),
("Mehul", 158), (202, 'Nigdi', 13000),
("Tanmay", 155), (203, 'Kharadi', 25000),
("Kshitij", 157), (204, 'Hadapsar', 18000),
("Aditya", 159), (205, 'Akurdi', 1400);
("Himanshu", 158);
INSERT INTO Depositor VALUES INSERT INTO Borrower (cust_name, loan_no) VALUES
("Kalas", 2511), ('Mehul', 202),
("Mehul", 2512), ('Tanmay', 203),
("Tanmay", 2501), ('Aditya', 205);
("Kshitij", 2521),
("Aditya", 2531),
("Himanshu", 2512);
``` ```
@ -91,14 +117,14 @@ INSERT INTO Depositor VALUES
1. Find the names of all branches in loan relation. 1. Find the names of all branches in loan relation.
```sql ```sql
SELECT branchName FROM Loan; SELECT DISTINCT branch_name FROM Loan;
``` ```
2. Find all loan numbers for loans made at Akurdi Branch with loan amount > 12000. 2. Find all loan numbers for loans made at Akurdi Branch with loan amount > 12000.
```sql ```sql
SELECT loanNum FROM Loan WHERE amount>12000; SELECT loan_no FROM Loan WHERE branch_name = 'Akurdi' AND amount > 12000;
``` ```
@ -106,70 +132,73 @@ SELECT loanNum FROM Loan WHERE amount>12000;
```sql ```sql
SELECT * from Borrower; SELECT * from Borrower;
SELECT Borrower.loanNum FROM Borrower INNER JOIN Loan ON Borrower.loanNum = Loan.loanNum; SELECT Borrower.cust_name, Borrower.loan_no, Loan.amount FROM Borrower INNER JOIN Loan ON Borrower.loan_no = Loan.loan_no;
``` ```
4. List all customers in alphabetical order who have loan from Akurdi branch. 4. List all customers in alphabetical order who have loan from Akurdi branch.
```sql ```sql
SELECT customerName FROM Borrower INNER JOIN Loan ON Borrower.loanNum = Loan.loanNum WHERE branchName = "Akurdi" ORDER BY customerName; SELECT cust_name FROM Borrower INNER JOIN Loan ON Borrower.loan_no = Loan.loan_no WHERE branch_name = 'Akurdi' ORDER BY cust_name;
``` ```
5. Find all customers who have an account or loan or both at bank. 5. Find all customers who have an account or loan or both at bank.
```sql ```sql
SELECT customerName FROM Depositor UNION SELECT customerName FROM Borrower; SELECT cust_name FROM Depositor UNION SELECT cust_name FROM Borrower;
``` ```
6. Find all customers who have both account and loan at bank. 6. Find all customers who have both account and loan at bank.
```sql ```sql
SELECT customerName FROM Depositor INTERSECT SELECT customerName FROM Borrower; SELECT cust_name FROM Depositor INTERSECT SELECT cust_name FROM Borrower;
``` ```
7. Find all customers who have account but no loan at the bank. 7. Find all customers who have account but no loan at the bank.
```sql ```sql
SELECT customerName FROM Depositor WHERE customerName NOT IN (SELECT customerName FROM Borrower); SELECT cust_name FROM Depositor WHERE cust_name NOT IN (SELECT cust_name FROM Borrower);
-- OR YOU CAN RUN THE BELOW COMMAND IF YOU ARE FEELING FANCY
-- SELECT Customer.cust_name FROM Customer INNER JOIN Depositor ON Customer.cust_name = Depositor.cust_name LEFT JOIN Borrower ON Customer.cust_name = Borrower.cust_name WHERE Borrower.cust_name IS NULL;
``` ```
8. Find the average account balance at each branch 8. Find the average account balance at each branch
```sql ```sql
SELECT AVG(amount) FROM Loan; SELECT branch_name, AVG(balance) FROM Account GROUP BY branch_name;
``` ```
9. Find no. of depositors at each branch. 9. Find no. of depositors at each branch.
```sql ```sql
SELECT branchName, COUNT(*) AS noOfDepositors FROM Account JOIN Depositor ON Account.accountNum = Depositor.accountNum GROUP BY branchName; SELECT branch_name, COUNT(*) FROM Account INNER JOIN Depositor ON Account.acc_no = Depositor.acc_no GROUP BY branch_name;
``` ```
10. Find name of Customer and city where customer name starts with Letter K. 10. Find name of Customer and city where customer name starts with Letter K.
```sql ```sql
SELECT customerName, customerCity from Customer where customerName like "K%"; SELECT cust_name, cust_city FROM Customer WHERE cust_name LIKE 'K%';
``` ```
11. Display distinct cities of branch. 11. Display distinct cities of branch.
```sql ```sql
SELECT DISTINCT branchName, branchCity FROM Branch; SELECT DISTINCT branch_city FROM Branch;
``` ```
12. Find the branches where average account balance > 1200 12. Find the branches where average account balance > 1200
```sql ```sql
SELECT branchName FROM Account GROUP BY branchName HAVING AVG(balance) > 1200; SELECT branch_name FROM Account GROUP BY branch_name HAVING AVG(balance) > 12000;
``` ```
@ -183,26 +212,26 @@ SELECT COUNT(*) FROM Customer;
14. Calculate total loan amount given by bank. 14. Calculate total loan amount given by bank.
```sql ```sql
SELECT SUM(amount) AS amount FROM Loan; SELECT SUM(amount) FROM Loan;
``` ```
15. Delete all loans with loan amount between 1300 and 1500. 15. Delete all loans with loan amount between 1300 and 1500.
```sql ```sql
DELETE FROM Borrower WHERE loanNum IN (SELECT loanNum FROM Loan WHERE amount > 1300 AND amount < 1500); DELETE FROM Borrower WHERE loan_no IN (SELECT loan_no FROM Loan WHERE amount BETWEEN 1300 AND 1500);
DELETE FROM Loan WHERE amount > 1300 AND amount < 1500; DELETE FROM Loan WHERE amount BETWEEN 1300 AND 1500;
``` ```
16. Delete all tuples at every branch located in Nigdi. 16. Delete all tuples at every branch located in Nigdi.
```sql ```sql
DELETE FROM Borrower WHERE loanNum IN (SELECT loanNum FROM Loan WHERE branchName IN (SELECT branchName FROM Branch WHERE branchCity = "Nigdi")); DELETE FROM Borrower WHERE loan_no IN (SELECT loan_no FROM Loan WHERE branch_name = 'Nigdi');
DELETE FROM Loan WHERE branchName = (SELECT branchName FROM Branch WHERE branchCity = "Nigdi"); DELETE FROM Loan WHERE branch_name = 'Nigdi';
DELETE FROM Depositor WHERE accountNum IN (SELECT accountNum FROM Account WHERE branchName IN (SELECT branchName FROM Branch WHERE branchCity = "Nigdi")); DELETE FROM Depositor WHERE acc_no IN (SELECT acc_no FROM Account WHERE branch_name = 'Nigdi');
DELETE FROM Account WHERE branchName = (SELECT branchName FROM Branch WHERE branchCity = "Nigdi"); DELETE FROM Account WHERE branch_name = 'Nigdi';
DELETE FROM Branch WHERE branchName = “Nigdi”; DELETE FROM Branch WHERE branch_name = 'Nigdi';
``` ```