0% found this document useful (0 votes)
200 views14 pages

2

The document describes various SQL queries that can be used to analyze data from tables representing bank data. Some example queries include finding the 4th highest ranked student, using aggregate functions like COUNT and AVG, joining tables to find customers with loans and accounts, using HAVING to filter groups, and subqueries. The tables defined include branches, customers, loans, accounts, and the relationships between them.

Uploaded by

romeoremo13
Copyright
© Attribution Non-Commercial (BY-NC)
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)
200 views14 pages

2

The document describes various SQL queries that can be used to analyze data from tables representing bank data. Some example queries include finding the 4th highest ranked student, using aggregate functions like COUNT and AVG, joining tables to find customers with loans and accounts, using HAVING to filter groups, and subqueries. The tables defined include branches, customers, loans, accounts, and the relationships between them.

Uploaded by

romeoremo13
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 14

WEEK 6 2) Queries (along with subqueries) using ANY, ALL, IN, EXISTS, NOT EXISTS, UNIQUE, INTERSECT, Constraints.

Example: select the rollno and name of the student who secured 4th rank in the class. 3) Queries using Aggregate functions (COUNT, SUM, AVG, MAX and MIN), GROUP BY, HAVING and Creation and Dropping of Views. 4) Queries using Conversions, functions (to_char, to_num, and to_date), string function (Conactenation, lpad, rpad, ltrim, rtrim, lower, upper, initcap, length, substr, and instr), date functions (sysdate, next_day, add_months, last_day, months_between, least, greatest, trunk, round, to_char, to_date).

TABLE DEFINITIONS Branch Schema <branch-name, branch-city, assets> Customer Schema <customer-name, customer-street, customer-city> Loan Schema <loan-number, branch-name, amount> Borrower Schema <customer-name, loan-number> Account Scheme <account-number, branch-name, balance> Depositor Scheme <customer-name, account-number>

BRANCH TABLE Branch Name Brighton Downtown Mianus North Town Perryridge Pownal Redwood Round Hill Branch City Brooklyn Brooklyn Horseneck Rye Horseneck Bennington Palo Alto Horseneck Assets 7100000 9000000 400000 3700000 1700000 300000 2100000 800000

CUSTOMER TABLE Customer Name Adams Brooks Curry Glenn Green Hayes Johnson Jones Lindsay Smith Turner Williams LOAN TABLE Loan Number L-11 L-14 L-15 L-16 L-17 L-23 L-93 Branch Name Round Hill Downtown Perryridge Perryridge Downtown Redwood Mianus Amount 900 1500 1500 1300 1000 2000 500 Customer Street Spring Senator North Sand Hill Walnut Main Alma Main Park North Putnam Nassau Customer City Pittsfield Brooklyn Rye Woodside Stamford Harrison Palo Alto Harrison Pittsfield Rye Stamford Princeton

BORROWER TABLE Customer Name Adams Curry Hayes Loan Number l-16 L-93 L-15

Jackson Jones Smith Smith Williams ACCOUNT TABLE Account Number A-101 A-102 A-201 A-215 A-217 A-222 A-305 DEPOSITOR TABLE Customer Name Hayes Johnson Johnson Jones Lindsay Smith Turner QUERIES

L-14 L-17 L-11 L-23 L-17

Branch Name Downtown Perryridge Brighton Mianus Brighton Redwood Round Hill

Balance 500 400 900 700 750 700 350

Account Number A102 A-101 A-201 A-217 A-222 A-215 A-305

1) To list all the fields from the table Customer. SELECT branch_name FROM Loan; 2) To list rows after eliminating duplicates. SELECT distinct branch_name FROM Loan;

3) To explicitly list rows, including duplicates. SELECT all branch_name FROM Loan; 4) To list fields after applying arithmetic operations. SELECT loan_number, branch_name, amount *100 FROM Loan; 5) Find all loan numbers for loans made at the Perryridge branch with loan amounts greater than Rs1200. SELECT loan_number FROM Loan WHERE branch_name = Perryridge AND amount > 1200; 6) Find all loan numbers for loans with loan amounts between Rs90,000 and Rs100,000. SELECT loan_number FROM Loan WHERE amount BETWEEN 90000 AND 100000; Or SELECT loan_number FROM Loan WHERE amount <= 100000 AND amount >= 90000; 7) Find all loan numbers for loans with loan amounts not between Rs90,000 and Rs100,000. SELECT loan_number FROM Loan WHERE amount NOT BETWEEN 90000 AND 100000;

8) For all customers who have a loan from the bank, find their names, loan numbers and loan amounts. SELECT customer_name, Borrower.loan_number, amount FROM Borrower, Loan WHERE Borrower.loan_number = Loan.loan_number; Or SELECT customer_name, Borrower.loan_number AS loan_id, amount FROM Borrower, Loan WHERE Borrower.loan_number = Loan.loan_number; 9) Find the customer names, loan numbers and loan amounts for all loans at the Perryridge branch. SELECT customer_name, Borrower.loan_number, amount FROM Borrower, Loan WHERE Borrower.loan_number = Loan.loan_number AND branch_name = Perryridge; Or

SELECT customer_name, T.loan_number, S.amount FROM Borrower AS T, Loan AS S WHERE T.loan_number = S.loan_number AND branch_name = Perryridge; 10) Find the names of all branches that have assets greater than atleast one branch located in Brooklyn. SELECT DISTINCT T.branch_name FROM Branch as T, Branch as S WHERE T.assets > S.assets AND S.branch_city = Brooklyn;

11) Find the names of all customers whose street address includes the substring Main. SELECT customer_name FROM Customer WHERE customer_street LIKE %Main%; 12) To list in alphabetic order all customers who have a loan at the Perryridge branch. SELECT DISTINCT customer_name FROM Borrower B, Loan L WHERE B.loan_number = L.loan_number AND branch_name = Perryridge ORDER BY customer_name;

13) To list the entire loan info in descending order of amont. SELECT * FROM Loan ORDER BY amount DESC, loan_number ASC; 14) To find all customers having a loan, an account or both at the bank, without duplicates. (SELECT customer_name FROM Depositor) UNION (SELECT customer_name FROM Borrower);

15) To find all customers having a loan, an account or both at the bank, with duplicates. (SELECT customer_name FROM Depositor) UNION ALL (SELECT customer_name FROM Borrower); 16) To find all customers having both a loan and an account at the bank, without duplicates. (SELECT customer_name FROM Depositor) INTERSECT (SELECT customer_name FROM Borrower); 17) To find all customers having a loan, an account or both at the bank, with duplicates. (SELECT customer_name FROM Depositor) INTERSECT ALL (SELECT customer_name FROM Borrower);

18) To find all customers who have an account but no loan at the bank, without duplicates. (SELECT DISTINCT customer_name FROM Depositor) EXCEPT (SELECT customer_name FROM Borrower);

19) To find all customers who have an account but no loan at the bank, with duplicates. (SELECT DISTINCT customer_name FROM Depositor) EXCEPT ALL (SELECT customer_name FROM Borrower);

20) Find the average account balance at the Perryridge branch SELECT branch_name, AVG(balance) FROM Account WHERE branch_name = Perryridge; 21) Find the average account balance at the each branch SELECT AVG(balance) FROM Account GROUP BY branch_name; 22) Find the number of depositors for each branch . SELECT branch_name, COUNT(DISTINCT customer_name) FROM Depositor D, Account A WHERE D.account_number = A.account_number GROUP BY branch_name; 23) Find the number of depositors for each branch where average account balance is more than Rs 1200. SELECT branch_name, COUNT(DISTINCT customer_name) FROM Depositor D, Account A WHERE D.account_number = A.account_number GROUP BY branch_name HAVING AVG(balance) > 1200;

24) Find the average balance for all accounts. SELECT AVG(balance) FROM Account; 25) Find the number of tuples in the customer relation. SELECT COUNT(*) FROM Customer; 26) Find the average balance for each customer who lives in Harrision and has at least three accounts. SELECT D.customer_name, AVG(balance) FROM Depositor D, Account A, Customer C WHERE D.account_number = A.account_number AND D.customer_name = C.customer_name AND C.customer_city = Harrison GROUP BY D.customer_name HAVING COUNT(DISTINCT D.account_number) >= 3; 27) Find all the loan number that appear in loan relation with null amount values. SELECT loan_number FROM Loan WHERE amount IS NULL; 28) Find all customers who have both a loan and an account at the bank. SELECT customer_name FROM Borrower WHERE customer_street IN (SELECT customer_name FROM Depositor);

29) Find all customers who have both an account and a loan at the Perryridge branch SELECT DISTINCT B.customer_name FROM Borrower B, Loan L WHERE B.loan_number L.loan_number AND branch_name = Perryridge AND (branch_name, customer_name) IN (SELECT branch_name, customer_name FROM Depositor D, Account A WHERE D.account_number = A.account_number); or SELECT customer_name FROM Borrower B WHERE EXISTS (SELECT * FROM Depositor D WHERE D.customer_name = B.customer_name); 30) Find all customers who do not have a loan at the bank, but do not have an account the bank. SELECT DISTINCT customer_name FROM Borrower WHERE customer_name NOT IN (SELECT customer_name FROM Depositor);

31) Find the names of customers who do have a loan at the bank, and whose names are neither Smith nor Jones. SELECT DISTINCT customer_name FROM Borrower WHERE customer_name NOT IN (Smith, Jones);

32) Find the names of all branches that have assets greater than those of at least one branch located in Brooklyn. SELECT DISTINCT T.branch_name FROM Branch AS T, Branch AS S WHERE T.assets > S.assets AND S.branch_city = Brooklyn; 33) Find the names of all branches that have assets greater than that of each branch located in Brooklyn. SELECT branch_name FROM Account GROUP BY branch_name HAVING AVG(balance) >= ALL (SELECT AVG(balance) FROM Account GROUP BY branch_name);

34) Find all customers who have an account at all the branches located in Brooklyn. SELECT DISTINCT S.customer_name FROM Depositor AS D WHERE NOT EXISTS ((SELECT branch_name FROM Branch WHERE branch_city = Brroklyn) EXCEPT (SELECT R.branch_name FROM Depositor AS T, Account AS R WHERE T.account_number = R.account_number AND D.customer_name = t.customer_name));

35) Find all customers who have at most one account at the Perryridge branch. SELECT T.customer_name FROM Depositor AS T WHERE UNIQUE (SELECT R.customer_name FROM Depositor AS R, Account AS A WHERE T.customer_name = R.customer_name AND R.account_number = A.account_number AND A.branch_name = Perryridge); 36) Find all customers who have at least two accounts at the Perryridge branch. SELECT DISTINCT T.customer_name FROM Depositor AS T WHERE NOT UNIQUE (SELECT R.customer_name FROM Depositor AS R, Account AS A WHERE T.customer_name = R.customer_name AND R.account_number = A.account_number AND A.branch_name = Perryridge); 37) Find the average account balance of those branches where the average account balance is greater than 1200. SELECT branch_name, avg_balance FROM (SELECT branch_name, AVG(balance) FROM Account GROUP BY branch_name) AS Branch_avg(branch_name, avg_balance) WHERE avg_balance > 1200;

38) Find the maximum across all branches of the total balance at each branch.

SELECT MAX(tot_balance) FROM (SELECT branch_name, SUM(balance) FROM Account GROUP BY branch_name) AS Branch_total(branch_name, tot_balance); 39) Find the all customers who have an account but no loan at the bank. SELECT d-CN

FROM (Depositor LEFT OUTER JOIN Borrower ON Depositor.customer_name = Borrower.customer_name) AS db1(d-CN, account_number, b-CN, loan_number) WHERE b-CN is null; 40) Find the all customers who have either an account or a loan (but not both) at the bank. SELECT customer_name FROM (Depositor NATURAL FULL OUTER JOIN Borrower) WHERE account_number IS NULL OR loan_number IS NULL;

You might also like