Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to...

13
Practice I SQL

Transcript of Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to...

Page 1: Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.

Practice I

SQL

Page 2: Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.

Step 1: Create Database

• Create a database named Bank in SQL Server 2005 according to the following schema diagram

Page 3: Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.

Step 2: Populate Relations (1)

• Insert the following data into the relation customer– ('Jones', 'Main', 'Harrison') (‘Smith’, 'Main', 'Rye');– (‘Hayes’, ‘Main’, 'Harrison‘) (‘Curry’, 'North‘, 'Rye');– (‘Lindsay’, 'Park‘, 'Pittsfield‘) ('Turner‘, 'Putnam‘,

'Stamford');– (‘Williams’, ‘Nassau’, 'Princeton‘) (‘Adams’, ‘Spring’, 'Pittsfield');– (‘Johnson’, ‘Alma’, 'Palo Alto‘) (‘Glenn’, ‘Sand Hill’, 'Woodside');– (‘Brooks’, 'Senator', 'Brooklyn‘) (‘Green’, ‘Walnut’,

'Stamford');– (‘Jackson’, ‘University’, 'Salt Lake‘) (‘Majeris’, ‘First’, 'Rye');– (‘McBride’, ‘Safety’, 'Rye')

Page 4: Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.

Step 2: Populate Relations (2)

• Insert the following data into the relation branch– (‘Downtown’, ‘Brooklyn’, 900000)– (‘Redwood’, ‘Palo Alto’, 2100000)– (‘Perryridge’, ‘Horseneck’, 1700000)– (‘Mianus’, ‘Horseneck’, 400200)– (‘Round Hill’, ‘Horseneck’, 8000000)– (‘Pownal’, ‘Bennington’, 400000)– (‘North Town’, ‘Rye’, 3700000)– (‘Brighton’, ‘Brooklyn’, 7000000)– (‘Central’, ‘Rye’, 400280)

Page 5: Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.

Step 2: Populate Relations (3)

• Insert the following data into the relation account– (‘A-101’, ‘Downtown’, 500)– (‘A-215’, ‘Mianus’, 700)– (‘A-102’, ‘Perryridge’, 400)– (‘A-305’, 'Round Hill', 350)– (‘A-201’, ‘Perryridge’, 900)– (‘A-222’, ‘Redwood’, 700)– (‘A-217’, ‘Brighton’, 750)– (‘A-333’, ‘Central’, 850)– (‘A-444’, ‘North Town’, 625)

Page 6: Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.

Step 2: Populate Relations (4)

• Insert the following data into the relation depositor– (‘Johnson’,‘A-101‘) (’Smith’, 'A-215')– (‘Hayes’, ‘A-102‘) (’Hayes’, 'A-101')– (‘Turner’, 'A-305‘) ('Johnson','A-201')– ('Jones', 'A-217‘) ('Lindsay','A-222')– ('Majeris','A-333')– ('Smith', 'A-444')

Page 7: Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.

Step 2: Populate Relations (5)

• Insert the following data into the relation loan– (‘L-17’, ‘Downtown’, 1000)– (‘L-23’, ‘Redwood’, 2000)– (‘L-15’, ‘Perryridge’, 1500)– (‘L-14’, ‘Downtown’, 1500)– (‘L-93’, ‘Mianus’, 500)– (‘L-11’, ‘Round Hill’, 900)– (‘L-16’, ‘Perryridge’, 1300)– (‘L-20’, ‘North Town’, 7500)– (‘L-21’, 'Central', 570)

Page 8: Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.

Step 2: Populate Relations (6)

• Insert the following data into the relation borrower– (‘Jones’, ‘L-17‘) (’Smith’, 'L-23')– (‘Hayes’, ‘L-15‘) (’Jackson’, 'L-14')– (‘Curry’, ‘L-93‘) (’Smith’, 'L-11')– (‘Williams’, ‘L-17‘) (’Adams’, 'L-16')– (‘McBride’, 'L-20')– ('Smith', 'L-21')

Page 9: Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.

Step 3: Retrieving Data (1)

• List all information stored in the database;• List the names and cities of all borrowers;• List the set of names and cities of customers who have a loan

at Perryridge branch;• List the numbers of accounts with balances between 700 and

900; • List the names of customers on streets with names ending in

‘Hill’;• List the names of customers with both accounts and loans at

Perryridge branch;

Page 10: Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.

Step 3: Retrieving Data (2)

• List the names of customers with an account but not a loan at Perryridge branch;

• List the names and cities of all borrowers;• List the set of names of customers with accounts at a branch

where Hayes has;• List the set of names of branches whose assets are greater

than the assets of some branch in Brooklyn;• List the set of names of branches whose assets are greater

than the assets of all branches in Brooklyn;• List the names of customers with both accounts and loans at

Perryridge branch (using ‘exists’)

Page 11: Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.

Step 3: Retrieving Data (3)

• List the names of customers with an account but not a loan at Perryridge branch (using ‘exists’)

• List the set of names of customers at Perryridge branch, in alphabetical order;

• Show the loan data, ordered by decreasing amounts, then increasing loan numbers;

• List the names of branches having at least one account, with average account balances;

• List the names of branches having at least one account, with the size of set of customers having at least one account at that branch;

Page 12: Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.

Step 3: Retrieving Data (4)

• Calculate the average balance of all accounts;• List the names of branches having at least one account, with

average balances of accounts at each branch, if that average is above 700;

• List the name(s) of branch(es) having largest average balance;• Calculate the number of customers;• List the average balance of all customers in Harrison having at

least 2 accounts;

Page 13: Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.

Report Guideline

• Writing in english• pdf version are preferred• Pay more attention to the format: font, size,

color etc.• Record the detail of your practice– SQL Server Command, SQL statement, error

information …

• Don’t forget your name and student ID