Tested all running queries and fixed the ones that weren't working.

This commit is contained in:
K 2024-07-31 21:37:14 +05:30
parent 2c647c95b8
commit 0c4e665809
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F

View File

@ -63,7 +63,7 @@ INSERT INTO Loan (loanNum, branchName, amount) VALUES
(155, "Dhole Patil", 500),
(156, "Nagarwala", 250),
(157, "Peachtree", 600),
(158, "Bishops", 9000),
(158, "Bishops", 1400),
(159, "Amanora", 25000);
INSERT INTO Borrower VALUES
@ -110,118 +110,99 @@ SELECT Borrower.loanNum FROM Borrower INNER JOIN Loan ON Borrower.loanNum = Loan
```
==TESTING PENDING==
4. List all customers in alphabetical order who have loan from Akurdi branch.
```sql
SELECT customerName
FROM Borrower
WHERE loanNum IN (SELECT loanNum FROM Loan WHERE branchName = 'Akurdi')
ORDER BY customerName;
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 DISTINCT customerName
FROM (SELECT customerName FROM Account UNION SELECT customerName FROM Borrower) AS Customers;
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 Account
WHERE customerName IN (SELECT customerName FROM Borrower);
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 Account
WHERE customerName NOT IN (SELECT customerName FROM Borrower);
SELECT customerName FROM Depositor WHERE customerName NOT IN (SELECT customerName FROM Borrower);
```
8. Find the average account balance at each branch
```sql
SELECT branchName, AVG(balance) AS avg_balance
FROM Account
GROUP BY branchName;
SELECT AVG(amount) FROM Loan;
```
9. Find no. of depositors at each branch.
```sql
SELECT branchName, COUNT(DISTINCT customerName) AS num_depositors
FROM Account
GROUP BY branchName;
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 P.
10. Find name of Customer and city where customer name starts with Letter K.
```sql
SELECT customerName, city
FROM Customer
WHERE customerName LIKE 'P%';
SELECT customerName, customerCity from Customer where customerName like "K%";
```
11. Display distinct cities of branch.
```sql
SELECT DISTINCT city
FROM Branch;
SELECT DISTINCT branchName, branchCity FROM Branch;
```
12. Find the branches where average account balance > 12000
12. Find the branches where average account balance > 1200
```sql
SELECT branchName
FROM Account
GROUP BY branchName
HAVING AVG(balance) > 12000;
SELECT branchName FROM Account GROUP BY branchName HAVING AVG(balance) > 1200;
```
13. Find number of tuples in customer relation.
```sql
SELECT COUNT(*) AS num_tuples
FROM Customer;
SELECT COUNT(*) FROM Customer;
```
14. Calculate total loan amount given by bank.
```sql
SELECT SUM(amount) AS total_loan_amount
FROM Loan;
SELECT SUM(amount) AS amount FROM Loan;
```
15. Delete all loans with loan amount between 1300 and 1500.
```sql
DELETE FROM Loan
WHERE 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;
```
16. Delete all tuples at every branch located in Nigdi.
```sql
DELETE FROM Account
WHERE branchName IN (SELECT branchName FROM Branch WHERE city = '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”;
```