0% found this document useful (0 votes)
43 views27 pages

Untitled Document

The document contains SQL statements to create tables for bank data including branches, accounts, loans, customers, and transactions. Six tables are created - branch, account, loan, depositor, customer, and borrower. Data is inserted into each table. Queries are also provided to retrieve data from the tables by joining them.

Uploaded by

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

Untitled Document

The document contains SQL statements to create tables for bank data including branches, accounts, loans, customers, and transactions. Six tables are created - branch, account, loan, depositor, customer, and borrower. Data is inserted into each table. Queries are also provided to retrieve data from the tables by joining them.

Uploaded by

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

NAME: Jesseman Devamirtham N

REG.NO: 22BRS1112

1. branch :
BRANCH_NAME BRANCH_CITY ASSETS
Perryridge Rye 5000000
Downtown Stamford 1000000
Brighton Paloalto 2500000
Redwood Harrison 1500000
Mianus Pitsfield 4500000
Roundhill Princeton 1500000

INSERT INTO branch_22brs1112 VALUES ('Perryridge', 'Rye', 5000000);


INSERT INTO branch_22brs1112 VALUES ('Downtown', 'Stamford', 1000000);
INSERT INTO branch_22brs1112 VALUES ('Brighton', 'Paloalto', 2500000);
INSERT INTO branch_22brs1112 VALUES ('Redwood', 'Harrison', 1500000);
INSERT INTO branch_22brs1112 VALUES ('Mianus', 'Pitsfield', 4500000);
INSERT INTO branch_22brs1112 VALUES ('Roundhill', 'Princeton', 1500000);
DESCRIBE branch_22brs1112;
2. account :
ACCOUNT_NO BRANCH_NAME BALANCE
019_28_3746 Perryridge 15000
182_73_6091 Downtown 23000
192_83_7465 Brighton 18000
321_12_3123 Redwood 5000
336_66_9999 Mianus 5000
963_96_3963 Roundhill 5000
376_66_9999 Mianus 9000
963_96_3964 Mianus 13000

INSERT INTO account_22brs1112 (account_no, branch_name, balance)


VALUES
('019_28_3746', 'Perryridge', 15000),
('182_73_6091', 'Downtown', 23000),
('192_83_7465', 'Brighton', 18000),
('321_12_3123', 'Redwood', 5000),
('336_66_9999', 'Mianus', 5000),
('963_96_3963', 'Roundhill', 5000),
('376_66_9999', 'Mianus', 9000),
('963_96_3964', 'Mianus', 13000);

3. loan :
LOAN BRANCH_NAME AMOUNT
1_11 Roundhill 9000
1_14 Downtown 15000
1_15 Perryridge 15000
1_16 Perryridge 13000
1_17 Downtown 10000
1_23 Redwood 20000
1_93 Mianus 500

INSERT INTO loan_22brs1112 VALUES ('1_11', 'Roundhill', 9000);


INSERT INTO loan_22brs1112 VALUES ('1_14', 'Downtown', 15000);
INSERT INTO loan_22brs1112 VALUES ('1_15', 'Perryridge', 15000);
INSERT INTO loan_22brs1112 VALUES ('1_16', 'Perryridge', 13000);
INSERT INTO loan_22brs1112 VALUES ('1_17', 'Downtown', 10000);
INSERT INTO loan_22brs1112 VALUES ('1_23', 'Redwood', 20000);
INSERT INTO loan_22brs1112 VALUES ('1_93', 'Mianus', 500);
DESCRIBE loan_22brs1112;
4. depositor
CUSTOMER_ID ACCOUNT_NO
c_08 182_73_6091
c_03 192_83_7465
c_05 321_12_3123
c_07 336_66_9999
c_08 963_96_3963
c_02 376_66_9999

INSERT INTO depositor_22brs1112 VALUES ('c_08', '182_73_6091');


INSERT INTO depositor_22brs1112 VALUES ('c_03', '192_83_7465');
INSERT INTO depositor_22brs1112 VALUES ('c_05', '321_12_3123');
INSERT INTO depositor_22brs1112 VALUES ('c_07', '336_66_9999');
INSERT INTO depositor_22brs1112 VALUES ('c_08', '963_96_3963');
INSERT INTO depositor_22brs1112 VALUES ('c_02', '376_66_9999');
DESCRIBE depositor_22brs1112;
5. customer
CUSTOMER_ID CUSTOMER_NAME CUSTOMER_STREET
CUSTOMER_CITY
c_01 smith north rye
c_02 turner putnam stamford
c_03 johnson alma palo alto
c_04 curry north rye
c_05 jones main harrisdon
c_06 adoms spring pittsfield
c_07 lindsay park pittsfield
c_08 hayes main harrison
c_09 williams nassau Princeton

INSERT INTO customer_22brs1112 VALUES ('c_01', 'smith', 'north', 'rye');


INSERT INTO customer_22brs1112 VALUES ('c_02', 'turner', 'putnam', 'stamford');
INSERT INTO customer_22brs1112 VALUES ('c_03', 'johnson', 'alma', 'palo alto');
INSERT INTO customer_22brs1112 VALUES ('c_04', 'curry', 'north', 'rye');
INSERT INTO customer_22brs1112 VALUES ('c_05', 'jones', 'main', 'harrison');
INSERT INTO customer_22brs1112 VALUES ('c_06', 'adams', 'spring', 'pittsfield');
INSERT INTO customer_22brs1112 VALUES ('c_07', 'lindsay', 'park', 'pittsfield');
INSERT INTO customer_22brs1112 VALUES ('c_08', 'hayes', 'main', 'harrison');
INSERT INTO customer_22brs1112 VALUES ('c_09', 'williams', 'nassau', 'Princeton');

DESCRIBE customer_22brs1112;
6. borrower
CUSTOMER_ID LOAN_NO
c_01 1_11
c_01 1_23
c_03 1_93
c_05 1_17
c_03 1_16
c_05 1_14

INSERT INTO borrower_22brs1112 VALUES ('c_01', '1_11');


INSERT INTO borrower_22brs1112 VALUES ('c_01', '1_23');
INSERT INTO borrower_22brs1112 VALUES ('c_03', '1_93');
INSERT INTO borrower_22brs1112 VALUES ('c_05', '1_17');
INSERT INTO borrower_22brs1112 VALUES ('c_03', '1_16');
INSERT INTO borrower_22brs1112 VALUES ('c_05', '1_14');
DESCRIBE borrower_22brs1112;
Retrieval operation:
1. Find the names of all branches in the loan relation with
duplicates.

SELECT branch_name
FROM loan_22brs1112
GROUP BY branch_name
HAVING COUNT(branch_name) > 1;
2. Find branch names in the loan relation without duplicates.

SELECT DISTINCT branch_name


FROM loan_22brs1112;

3. Modify the balance attribute alone such that it decreases the


amount by 10% for the account table.

UPDATE account_22brs1112
SET balance = balance - (balance * 0.10);

SELECT * FROM account_22brs1112;


4. Display all the Customer names whose come from either pittsfield or
stamford.

SELECT customer_name
FROM customer_22brs1112
WHERE customer_city IN ('Pittsfield', 'Stamford');

5. Find all loan numbers for loans made at the ‘Perryridge’


branch with loan amount greater than 1200.
SELECT loan_no

FROM loan_22brs1112
WHERE branch_name = 'Perryridge' AND amount > 1200;

6. Find loan numbers of those loans with loan amount between


10000 and 20000.

SELECT loan_no

FROM loan_22brs1112

WHERE amount BETWEEN 10000 AND 20000;


7. Find the names of all customers whose street address includes
the substring ‘Main’.
SELECT customer_name

FROM customer_22brs1112

WHERE customer_street LIKE '%Main%';


8. To list the entire loan relation in descending order of amount.
SELECT *

FROM loan_22brs1112

ORDER BY amount DESC;

9. Find the names of the customer whose second letter with ‘u’.
SELECT customer_name

FROM customer_22brs1112

WHERE SUBSTRING(customer_name, 2, 1) = 'u';


10. Find the names of all branches that have assets greater than at
least one branch located in Stamford.
SELECT branch_name

FROM branch_22brs1112

WHERE assets > (SELECT MAX(assets) FROM branch_22brs1112 WHERE branch_city =


'Stamford');

11. Display all the customer whose have account with more than10
yrs (add column account open date, if the column not present).
SELECT customer_name

FROM customer_22brs1112

WHERE DATEDIFF(NOW(), account_open_date) > 3652;


12. Display a date with following format” on 7th January 2001 at
5:30p.m”.
SELECT DATE_FORMAT(NOW(), 'on %D %M %Y at %l:%i%p');

13. Delete all the account tuples in the ‘Redwood’ branch.


DELETE FROM account_22brs1112

WHERE branch_name = 'Redwood';


14. Delete all loans with loan amounts between 15000 to 20000.
DELETE FROM loan_22brs1112

WHERE amount BETWEEN 15000 AND 20000;

15. Pay 5% interest an account whose balance is greater than


average.
UPDATE account_22brs1112
SET balance = balance * 1.05
WHERE balance > (SELECT AVG(sub.balance) FROM (SELECT balance FROM
account_22brs1112) AS sub);
Built-In Functions:
1. Find the average account balance at each branch.
SELECT branch_name, AVG(balance) AS average_balance

FROM account_22brs1112

GROUP BY branch_name;

2. Select the customer city, which has more than 4 customers.


SELECT customer_city

FROM customer_22brs1112

GROUP BY customer_city

HAVING COUNT(*) > 4;


3. Retrieve the 3rd maximum amount in the Adyar branch.
SELECT MAX(balance) AS third_max_amount

FROM account_22brs1112

WHERE branch_name = 'Adyar'

AND balance < (SELECT MAX(balance) FROM account_22brs1112 WHERE branch_name


= 'Adyar')

ORDER BY balance DESC

LIMIT 1;
4. Use replace function and change the word adyar to
Vadapalani.
SELECT REPLACE('adyar', 'adyar', 'Vadapalani') AS replaced_word;

5. Find the highest balance, lowest balance and difference between


both.
SELECT MAX(balance) AS highest_balance, MIN(balance) AS lowest_balance,

MAX(balance) - MIN(balance) AS balance_difference

FROM account_22brs1112;

6. Display the day of next Saturday.


select next_day(sysdate, 'saturday') as next_saturday from dual;
7. Display the last date of Feb 2006.

SELECT LAST_DAY('2006-02-01') AS last_date_of_feb_2006;

8. Display the word as “ ****welcome”.

SELECT CONCAT('****', 'welcome') AS formatted_word;

9. Display the customer names with first letter in capital.

SELECT CONCAT(UPPER(SUBSTRING(customer_name, 1, 1)),


LOWER(SUBSTRING(customer_name, 2))) AS capital_name

FROM customer_22brs1112;
10. Count the number of days present between today and Sunday.
SELECT DATEDIFF(DATE_ADD(NOW(), INTERVAL (7 - WEEKDAY(NOW())) DAY),
NOW()) AS days_to_sunday;

11. Change the name of employee “AAA” to “BBB”.


UPDATE employee_22brs1112

SET emp_name = 'BBB'

WHERE emp_name = 'AAA';


12. Using LIKE operator list the enames starting with B and third
character with A.
SELECT emp_name

FROM employee_22brs1112

WHERE emp_name LIKE 'B_A%';


13. List the customer who are 24 years and above and those who are born in the month of
jan or feb or mar.

SELECT *

FROM customer_22brs1112

WHERE TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 24

AND MONTH(birthdate) IN (1, 2, 3);

14. Display all the employees who are born in the month of August.

SELECT *
FROM employee_22brs1112
WHERE MONTH(birthdate) = 8;

15. List all employees who are born between 1st May and 31st Dec of any year.

SELECT *
FROM employee_22brs1112
WHERE MONTH(birthdate) BETWEEN 5 AND 12;

16. Delete all the employees whose age is above 20 yrs.

DELETE FROM employee_22brs1112


WHERE TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) > 20;

17. Display the Names of student whose grade is NOT NULL.

SELECT emp_name
FROM employee_22brs1112
WHERE grade IS NOT NULL;
Group by clause-

1. Find the number of the depositors for each branch.

select branch_name, count(*) from account_22brs1112 group by branch_name;

2. Find the total salary of a department.

3. Find the average salary of an employee for each department numberwise.


SELECT department_number, AVG(salary) AS avg_salary

FROM employee_22brs1112

GROUP BY department_number;

4. Find the total number of persons working from the employee table and also group by
deptnumberwise.

SELECT department_number, COUNT(*) AS total_persons

FROM employee_22brs1112

GROUP BY department_number;

Order By clause-

1. Display the customer name in alphabetical order.

SELECT customer_name

FROM customer_22brs1112
ORDER BY customer_name;

2. Display all the customer names ordered by customer city

SELECT customer_name
FROM customer_22brs1112
ORDER BY customer_city;

You might also like