0% found this document useful (0 votes)
105 views

2019 Final Experiment Test - Solution

The document describes creating and populating database tables for a banking example. It includes tables for branches, customers, accounts, loans, depositors, borrowers, and employees. It provides the SQL commands to: 1. Create the database and tables 2. Insert data into the tables 3. Perform queries on the tables including finding low balance accounts, Brooklyn customers, high paid non-downtown employees, average employee salary, and customers per account.
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)
105 views

2019 Final Experiment Test - Solution

The document describes creating and populating database tables for a banking example. It includes tables for branches, customers, accounts, loans, depositors, borrowers, and employees. It provides the SQL commands to: 1. Create the database and tables 2. Insert data into the tables 3. Perform queries on the tables including finding low balance accounts, Brooklyn customers, high paid non-downtown employees, average employee salary, and customers per account.
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/ 4

Question

Banking Example (primary key is underlined)


branch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-city)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
employee (employee-name, branch-name, salary)

(1). Create database ‘bank’


(2). Create above tables in database ‘bank’
(3). Insert data into those tables
insert into account values('A-101', 'Downtown', 500);
insert into account values('A-102', 'Perryridge', 400);
insert into account values('A-201', 'Brighton', 900);
insert into account values('A-215', 'Mianus', 700);
insert into account values('A-217', 'Brighton', 750);
insert into account values('A-222', 'Redwood', 700);
insert into account values('A-305', 'Round Hill', 350);

insert into branch values('Brighton', 'Brooklyn', 7100000);


insert into branch values('Downtown', 'Brooklyn', 9000000);
insert into branch values('Mianus', 'Horseneck', 400000);
insert into branch values('North Town', 'Rye', 3700000);
insert into branch values('Perryridge', 'Horseneck', 1700000);
insert into branch values('Pownal', 'Bennington', 300000);
insert into branch values('Redwood', 'Palo Alto', 2100000);
insert into branch values('Round Hill', 'Horseneck', 8000000);

insert into customer values('Adams', 'Spring', 'Pittsfield');


insert into customer values('Brooks', 'Senator', 'Brooklyn');
insert into customer values('Curry', 'North', 'Rye');
insert into customer values('Glenn', 'Sand Hill', 'Woodside');
insert into customer values('Green', 'Walnut', 'Stamford');
insert into customer values('Hayes', 'Main', 'Harrison');
insert into customer values('Johnson', 'Alma', 'Palo Alto');
insert into customer values('Jones', 'Main', 'Harrison');
insert into customer values('Lindsay', 'Park', 'Pittsfield');
insert into customer values('Smith', 'North', 'Rye');
insert into customer values('Turner', 'Putnam', 'Stamford');
insert into customer values('Williams', 'Nassau', 'Princeton');

insert into depositor values('Hayes', 'A-102');


insert into depositor values('Johnson', 'A-102');
insert into depositor values('Johnson', 'A-201');
insert into depositor values('Jones', 'A-217');
insert into depositor values('Lindsay', 'A-222');
insert into depositor values('Smith', 'A-215');
insert into depositor values('Turner', 'A-305');

insert into loan values('L-11', 'Round Hill', 900);


insert into loan values('L-14', 'Downtown', 1500);
insert into loan values('L-15', 'Perryridge', 1500);
insert into loan values('L-16', 'Perryridge', 1300);
insert into loan values('L-17', 'Downtown', 1000);
insert into loan values('L-23', 'Redwood', 2000);
insert into loan values('l-93', 'Mianus', 500);

insert into borrower values('Adams', 'L-16');


insert into borrower values('Curry', 'L-93');
insert into borrower values('Hayes', 'L-15');
insert into borrower values('Jackson', 'L-14');
insert into borrower values('Jones', 'L-17');
insert into borrower values('Smith', 'L-11');
insert into borrower values('Smith', 'L-23');
insert into borrower values('Williams', 'L-17');

insert into employee values('Adams', 'Perryridge', 1500);


insert into employee values('Brown', 'Perryridge', 1300);
insert into employee values('Gopal', 'Perryridge', 5300);
insert into employee values('Johnson', 'Downtown', 1500);
insert into employee values('Loreena', 'Downtown', 1300);
insert into employee values('Peterson', 'Downtown', 2500);
insert into employee values('Rao', 'Austin', 1500);
insert into employee values('Sato', 'Austin', 1600);

(4). Perform queries on those tables


1. Find all account whose balance is smaller than 500.
2. Find all name of customers whose city is in Brooklyn
3. Find all employees whose salary is greater than 1400 and working branch is not
‘Downtown’
4. Calculate the average salary of all employees and show the average salary as
“avg_salary”
5. Calculate the number of customer for each account
Solution (MySQL)
(1). Create database ‘bank’
= CREATE DATABASE Banking_Example;

(2). Create above tables in database ‘bank’


=
CREATE TABLE branch(
`branch-name` VARCHAR(255) NOT NULL,
`branch-city` VARCHAR(255),
assets FLOAT,
PRIMARY KEY(branch-name)
);

CREATE TABLE customer(


`customer-name` VARCHAR(255) NOT NULL,
`customer-street` VARCHAR(255),
`customer-city` VARCHAR(255),
PRIMARY KEY (`customer-name`)
);

CREATE TABLE account(


`account-number` VARCHAR(255) NOT NULL,
`branch-name` VARCHAR(255),
balance FLOAT,
PRIMARY KEY (`account-number`),
FOREIGN KEY (`branch-name`) REFERENCES branch(`branch-name`)
);

CREATE TABLE loan(


`loan-number` VARCHAR(255) NOT NULL,
`branch-name` VARCHAR(255),
`amount` FLOAT,
PRIMARY KEY (`loan-number`),
FOREIGN KEY (`branch-name`) REFERENCES branch(`branch-name`)
);

CREATE TABLE depositor(


`customer-name` VARCHAR(255) NOT NULL,
`account-number` VARCHAR(255) NOT NULL,
FOREIGN KEY (`customer-name`) REFERENCES customer(`customer-name`),
FOREIGN KEY (`account-number`) REFERENCES account(`account-number`)
);

CREATE TABLE borrower (


`customer-name` VARCHAR(255) NOT NULL,
`loan-number` VARCHAR(255) NOT NULL,
FOREIGN KEY (`customer-name`) REFERENCES customer(`customer-name`),
FOREIGN KEY (`loan-number`) REFERENCES loan(`loan-number`)
);

CREATE TABLE employee(


`employee-name` VARCHAR(255) NOT NULL,
`branch-name` VARCHAR(255) NOT NULL,
salary FLOAT,
PRIMARY KEY(`employee-name`),
FOREIGN KEY (`branch-name`) REFERENCES branch(`branch-name`)
);

(3). Insert data into those tables


[No Solution for this section, just copy those queries and execute]

(4). Perform queries on those tables


1. Find all account whose balance is smaller than 500.
= SELECT * FROM `account` WHERE `Balance`<500

2. Find all name of customers whose city is in Brooklyn


= SELECT `customer-name` FROM `customer` WHERE `customer-
city`='Brooklyn'

3. Find all employees whose salary is greater than 1400 and working branch is not
‘Downtown’
= SELECT * FROM `employee` WHERE `salary`>1400 AND `branch-
name`!='Downtown'

4. Calculate the average salary of all employees and show the average salary as
“avg_salary”
= SELECT AVG(`salary`) AS `avg_salary` FROM `employee`

5. Calculate the number of customer for each account


= SELECT `account-number`, COUNT(`customer-name`) FROM `depositor`
GROUP BY `account-number`

You might also like