Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

The primary keys for each relation are:

Branch: bname (assuming that each branch name is unique within the organization)
Customer: ID (assuming that each customer has a unique ID)
Loan: loan_no (assuming that each loan has a unique loan number)
Borrower: (ID, loan_no) (assuming that each customer can have only one loan at a
time and vice versa)
Account: acc_no (assuming that each account has a unique account number)
Depositor: (ID, acc_no) (assuming that each customer has only one account at a time
and vice versa)

The foreign keys in the schema are:

In the Loan relation:


bname is a foreign key referencing the Branch relation's bname attribute.
In the Borrower relation:
loan_no is a foreign key referencing the Loan relation's loan_no attribute.
In the Depositor relation:
ID is a foreign key referencing the Customer relation's ID attribute.
acc_no is a foreign key referencing the Account relation's acc_no attribute.

Find the name of each branch located in "Chicago":


σ(bcity = "Chicago")(Branch)
This expression uses the selection (σ) operator to select the rows from the Branch
relation where the bcity attribute is equal
to "Chicago".

2. Find the ID of each borrower who has a loan in branch "Downtown":

π(ID)(σ(bname = "Downtown")(borrower))
This expression uses the selection (σ) operator to select the rows from the
borrower relation where the bname attribute is
equal to "Downtown". Then it uses the projection (π) operator to select the ID
attribute from the resulting rows.

3.Find each loan number with a loan amount greater than $10000:

π(loan_no)(σ(amount > 10000)(loan))


This expression uses the selection (σ) operator to select the rows from the loan
relation where the amount attribute is
greater than $10000. Then it uses the projection (π) operator to select the loan_no
attribute from the resulting rows.

4. Find the ID of each depositor who has an account with a balance greater than
$6000:

π(ID)(σ(balance > 6000)(account))


This expression uses the selection (σ) operator to select the rows from the account
relation where the balance attribute
is greater than $6000. Then it uses the projection (π) operator to select the ID
attribute from the resulting rows.

5. Find the ID of each depositor who has an account with a balance greater than
$6000 at the "Uptown" branch:

π(ID)(σ(bname = "Uptown" ∧ balance > 6000)(depositor ⨝ ID = depositor.ID acc_no =


account.acc_no)(account))

This expression uses the natural join (⨝) operator to join the depositor and
account relations on the ID and acc_no
attributes, respectively. Then it uses the selection (σ) operator to select the
rows where the bname attribute is equal to
"Uptown" and the balance attribute is greater than $6000. Finally, it uses the
projection (π) operator to select the ID
attribute from the resulting rows.

This SQL statement uses the SUM aggregate function to calculate the total sum of
all loan amounts.

Here are the corrected SQL statements for the given requirements:

Find the ID of each customer who has an account but not a loan:

SELECT DISTINCT c.ID


FROM customer c
WHERE NOT EXISTS (
SELECT 1
FROM borrower b
JOIN loan l ON b.ID = l.ID
WHERE c.ID = b.ID
);
Find the details of all customers of the bank who have an account but not a loan:

SELECT DISTINCT c.ID, c.cname, c.street, c.city


FROM customer c
WHERE NOT EXISTS (
SELECT 1
FROM borrower b
JOIN loan l ON b.ID = l.ID
WHERE c.ID = b.ID
);
Find the ID of each customer who lives on the same street and in the same city as a
customer with ID '12345':

SELECT DISTINCT c.ID


FROM customer c
WHERE EXISTS (
SELECT 1
FROM customer c1
WHERE c.street = c1.street AND c.city = c1.city
AND c1.ID = '12345'
);
Find the name of each branch that has at least one customer who has an account in
the bank and who lives in "Harrison":

SELECT DISTINCT b.bname


FROM Branch b
WHERE EXISTS (
SELECT 1
FROM customer c
JOIN account a ON c.ID = a.ID
WHERE c.city = 'Harrison'
AND b.bname = a.bname
);
Find each customer who has an account at every branch located in "Brooklyn":

SELECT DISTINCT c.ID


FROM customer c
WHERE NOT EXISTS (
SELECT 1
FROM Branch b
WHERE b.bcity = 'Brooklyn'
AND NOT EXISTS (
SELECT 1
FROM account a
WHERE c.ID = a.ID
AND a.bname = b.bname
)
);

Find the total sum of all loan amounts in the bank:

SELECT SUM(loan.amount)
FROM loan;

Find the names of all branches that have assets greater than those of at least one
branch located in "Brooklyn":

SELECT DISTINCT b.bname


FROM Branch b
WHERE NOT EXISTS (
SELECT 1
FROM Branch b2
WHERE b2.bcity = 'Brooklyn'
AND b2.assets > b.assets
);

Create a view for the details of customers who live in "Logdon":

CREATE VIEW customer_logdon AS


SELECT DISTINCT c.ID, c.cname, c.street, c.city
FROM customer c
WHERE c.city = 'Logdon';

Modify the employee whose ID is '12345' to now live in 'Newton':

UPDATE employee
SET city = 'Newton'
WHERE ID = '12345';

You might also like