0% found this document useful (0 votes)
8 views9 pages

LAB05

CSE302 LAB05

Uploaded by

irfatjahan05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

LAB05

CSE302 LAB05

Uploaded by

irfatjahan05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

without using subqueries:

SELECT DISTINCT customer.*


FROM customer
JOIN accounts ON customer.customer_name = accounts.account_number
JOIN branch ON accounts.branch_name = branch.branch_name
WHERE customer.customer_city = branch.branch_city;

with using subqueries:

SELECT DISTINCT *
FROM customer
WHERE customer_city IN (
SELECT branch_city
FROM accounts
JOIN branch ON accounts.branch_name = branch.branch_name
WHERE customer.customer_name = accounts.account_number
);

2.
without using subqueries:

SELECT DISTINCT customer.*


FROM customer
JOIN borrower ON customer.customer_name = borrower.customer_name
JOIN loan ON borrower.loan_number = loan.loan_number
JOIN branch ON loan.branch_name = branch.branch_name
WHERE customer.customer_city = branch.branch_city;

with using subqueries:

SELECT DISTINCT *
FROM customer
WHERE customer_city IN (
SELECT branch_city
FROM loan
JOIN branch ON loan.branch_name = branch.branch_name
WHERE customer.customer_name IN (
SELECT customer_name FROM borrower
)
);

3.
with using ‘having’ clause:

SELECT branch_city, AVG(balance) AS avg_balance


FROM accounts
JOIN branch ON accounts.branch_name = branch.branch_name
WHERE branch_city IN (
SELECT branch_city
FROM branch
GROUP BY branch_city
HAVING SUM(balance) >= 1000
)
GROUP BY branch_city;

without using ‘having’ clause:

SELECT branch_city, AVG(balance) AS avg_balance


FROM accounts
JOIN branch ON accounts.branch_name = branch.branch_name
GROUP BY branch_city
HAVING SUM(balance) >= 1000;

4.
with using ‘having’ clause:

SELECT branch_city, AVG(amount) AS avg_loan


FROM loan
JOIN branch ON loan.branch_name = branch.branch_name
WHERE branch_city IN (
SELECT branch_city
FROM branch
GROUP BY branch_city
HAVING AVG(amount) >= 1500
)
GROUP BY branch_city;

without using ‘having’ clause:

SELECT branch_city, AVG(amount) AS avg_loan


FROM loan
JOIN branch ON loan.branch_name = branch.branch_name
GROUP BY branch_city
HAVING AVG(amount) >= 1500;

5.
without using all keyword:

SELECT c.customer_name, c.customer_street, c.customer_city


FROM customer c
JOIN depositor d ON c.customer_name = d.customer_name
WHERE d.account_number = (
SELECT account_number
FROM (
SELECT a.account_number, a.balance
FROM accounts a
ORDER BY a.balance DESC
)
WHERE ROWNUM = 1
);

With using all keyword:

SELECT c.customer_name, c.customer_street, c.customer_city


FROM customer c
JOIN depositor d ON c.customer_name = d.customer_name
JOIN accounts a ON d.account_number = a.account_number
WHERE a.balance >= ALL (
SELECT a2.balance
FROM accounts a2
);

6.
with using all keyword:

SELECT c.customer_name, c.customer_street, c.customer_city


FROM customer c
JOIN borrower b ON c.customer_name = b.customer_name
WHERE b.loan_number = (
SELECT loan_number
FROM (
SELECT loan_number, amount
FROM loan
ORDER BY amount ASC
)
WHERE ROWNUM = 1
);

without using all keyword:

SELECT c.customer_name, c.customer_street, c.customer_city


FROM customer c
JOIN borrower b ON c.customer_name = b.customer_name
JOIN loan l ON b.loan_number = l.loan_number
WHERE l.amount <= ALL (
SELECT l2.amount
FROM loan l2
);

7.
using in keyword:

SELECT DISTINCT branch_name, branch_city


FROM branch
WHERE branch_name IN (
SELECT branch_name FROM accounts
)
AND branch_name IN (
SELECT branch_name FROM loan
);
using exists keyword:

SELECT DISTINCT branch_name, branch_city


FROM branch B
WHERE EXISTS (
SELECT 1
FROM accounts A
WHERE A.branch_name = B.branch_name
)
AND EXISTS (
SELECT 1
FROM loan L
WHERE L.branch_name = B.branch_name
);

8.
using not in exists keyword:

SELECT DISTINCT customer_name, customer_city


FROM customer
WHERE customer_name IN (
SELECT customer_name FROM depositor
)
AND customer_name NOT IN (
SELECT customer_name FROM borrower
);

using not exists keyword:

SELECT DISTINCT customer_name, customer_city


FROM customer C
WHERE EXISTS (
SELECT 1
FROM depositor D
WHERE D.customer_name = C.customer_name
)
AND NOT EXISTS (
SELECT 1
FROM borrower B
WHERE B.customer_name = C.customer_name
);

9.
without using with clause:

SELECT branch_name
FROM branch
WHERE (
SELECT SUM(amount)
FROM loan
WHERE loan.branch_name = branch.branch_name
)<(
SELECT AVG(total_amount)
FROM (
SELECT SUM(amount) AS total_amount
FROM loan
GROUP BY branch_name
)
);

With using with clause:

WITH AvgBranchBalance AS (
SELECT AVG(SUM(balance)) AS avg_balance
FROM accounts
GROUP BY branch_name
)
SELECT branch_name
FROM branch
WHERE (
SELECT SUM(balance)
FROM accounts
WHERE accounts.branch_name = branch.branch_name
) > (SELECT avg_balance FROM AvgBranchBalance);

10.
without using with clause:

SELECT branch_name
FROM branch
WHERE (
SELECT SUM(amount)
FROM loan
WHERE loan.branch_name = branch.branch_name
)<(
SELECT AVG(total_amount)
FROM (
SELECT SUM(amount) AS total_amount
FROM loan
GROUP BY branch_name
)
);

with using with clause:

WITH AvgBranchLoan AS (
SELECT AVG(SUM(amount)) AS avg_loan
FROM loan
GROUP BY branch_name
)
SELECT branch_name
FROM branch
WHERE (
SELECT SUM(amount)
FROM loan
WHERE loan.branch_name = branch.branch_name
) < (SELECT avg_loan FROM AvgBranchLoan);

You might also like