Вы находитесь на странице: 1из 5

MIS 235 Sircar

SQL Query Practice Exercises 11-20 Professor Sumit Sircar


A relational database consists of a number of relations. The relation schemas for the DreamHome rental database are: Branch Staff Property_for_Rent Renter Owner Viewing BRANCH Bno Street B5 22 Deer Rd B7 16 Argyll St B3 163 Main St B4 32 Manse Rd B2 56 Clover Dr STAFF
Sno SL21 SG3 7 SG1 4 SA9 SG5 SL41 FName John Ann David Mary Susan Julie LName White Beech Ford Howe Brand Lee Address 19 Taylor St, Cranford, London 81 George St. Glasgow PA1 2JR 63 Ashby St, Partick, Glasgow G11 2 Elm Pl, Aberdeen AB2 3SU 5 Gt Western Rd, Glasgow G12 28 Malvern St, Kilburn Nw2 Tel_No 01718845112 01418483345 01413392177 01413342001 01815443541 Position Manager Snr Asst Deputy Assistant Manager Assistant S DOB e x M 1-Oct-45 F 10-Nov-60 Salary 30000 12000 18000 9000 24000 90000 NIN WK442011B WL432514C WL220658D WM532187D WK588932E WA290573K Bno B5 B3 B3 B7 B3 B5

(Bno, Street, Area, City, Pcode, Tel_No, Fax_No) (Sno, FName, LName, Address, Tel_No, Position, Sex, DOB, Salary, NIN, Bno) (Pno, Street, Area, City, Pcode, Type, Rooms, Rent, Ono, Sno, Bno) (Rno, FName, LName, Address, Tel_No, Pref_Type, Max_Rent) (Ono, FName, LName, Address, Tel_No) (Rno, Pno, Date, Comment) Area Sidcup Dyce Partick Leigh City London Aberdeen Glasgow Bristol London Pcode SW1 4EH AB2 3SU G11 9QX BS99 1NZ NW10 6EU Tel_No 0171-886-1212 01224-67125 0141-339-2178 011-916-01170 0181-963-1030 Fax_No 0171-886-1214 1224-671111 0141-339-4439 0117-776-1114 0181-453-7992

M 24-Mar-58 F F F 19-Feb-70 3-Jun-40 13-Jun-65

PROPERTY_FOR_RENT Pno Street PA14 16 Holhead PL94 6 Argyll St PG4 6 Lawrence St PG36 2 Manor Rd PG21 18 Dale Rd PG16 5 Novar Dr RENTER Rno FName CR76 John CR56 Aline CR74 Mike CR62 Mary OWNER Ono CO46 CO87 CO40 CO93 FName Joe Carol Tina Tony LName Kay Stewart Ritchie Tregear LName Kay Stewart Ritchie Tregear

Area Dee Kilburn Partick Hyndland Hyndland

City Aberdeen London Glasgow Glasgow Glasgow Glasgow

Pcode AB7 5SU NW2 G11 9QX G32 4QX G12 G12 9AX

Type House Flat Flat Flat House Flat

Rooms 6 4 3 3 5 4

Rent 650 400 350 375 600 450

Ono CO46 CO87 CO40 CO93 CO87 CO93 Pref_Type Flat Flat House Flat

Sno SA9 SL41 SG14 SG37 SG37 SG14

Bno B7 B5 B3 B3 B3 B3

Address 56 High St, Putney, London SW1 4EH 64 Fern Dr, Pollock, Glasgow G42 0BL 18 Tain St, Gourock PA1G 1YQ 5 Tarbot Rd, Kildary, Aberdeen AB9 3ST Address 2 Fergus Dr, Banchory, Aberdeen AB2 7SX 6 Achray St, Glasgow G32 9DX 63 Well St, Shawlands, Glasgow G42 12 Park Pl, Hillhead, Glasgow G4 0QR Comment Too small Too remote No dining room

Tel_No 0171-774-5632 0141-848-1825 0147-392178 01224-196720 Tel_No 01224-861212 0141-357-7419 0141-943-1728 0141-225-7025

Max_Rent 425 350 750 600

VIEWING Rno Pno CR56 PA14 CR76 PG4 CR56 PG4 CR62 PA14

Date 24-May-95 20-Apr-95 26-May-95 14-May-95

MIS 235 Sircar


CR56 PG36 28-Apr-95

Structured Query Language Data Manipulation


Use the DreamHome Rental Database for the following questions (assume annual salary is in $). Exercise #11 Read the tables: How many different (unique) properties were viewed in May, 1995? Write the SQL to produce the equivalent resulting table. Answer: Result Table: Count 2 SQL: Exercise #12 Read the tables: Find the total number of managers and the sum of their salaries. Write the SQL to produce the equivalent resulting table. Answer: Result Table: Count Sum 2 54000 SQL: Exercise #13 Read the tables: Find the minimum, maximum and average salary. Write the SQL to produce the equivalent resulting table. Answer: Result Table: Min Max 9000 30000 SQL: Exercise #14 Read the tables: Find the number of staff working in each branch and the total of their salaries. Write the SQL to produce the equivalent resulting table. Answer: Result Table: Bno Count B3 3

Avg 17000

Sum 54000

MIS 235 Sircar

B5 B7 SQL:

2 1

39000 9000

Exercise #15 Read the tables: For each branch office with more than 1 staff member, find the number of staff working in each branch and the total of their salaries. Write the SQL to produce the equivalent resulting table. Answer: Result Table: Bno Count B3 3 B5 2 SQL:

Sum 54000 39000

Exercise #16 Read the tables: Using a subquery, list the staff (number, name, position) who work in the branch at 163 Main St. Write the SQL to produce the equivalent resulting table. Answer: Result Table: Sno FName SG37 Ann SG14 David SG5 Susan SQL: Exercise #17 Read the tables: List the staff (number, name, position, salary) whose salary is greater than the average salary. Write the SQL to produce the equivalent resulting table.

LName Beech Ford Brand

Position Snr Asst Deputy Manager

Answer: Result Table: Sno FName SL21 John SG14 David SG5 Susan

LName White Ford Brand

Position Manager Deputy Manager

Salary 30000 18000 24000

MIS 235 Sircar

SQL: Exercise #18 Read the tables: List the names of all renters who have viewed a property along with any comment supplied. Write the SQL to produce the equivalent resulting table. Answer: Result Table: Rno FName CR56 Aline CR56 Aline CR56 Aline CR62 Mary CR76 John SQL: Exercise #19 Read the tables: For each branch office, list the names of the staff who manage properties and the properties they manage. Order appropriately. Write the SQL to produce the equivalent resulting table. Answer: Result Table: Bno Sno B3 SG14 B3 SG14 B3 SG37 B3 SG37 B5 SL41 B7 SA9 SQL:

LName Stewart Stewart Stewart Tregear Kay

Pno PG36 PA14 PG4 PA14 PG4

Comment Too small No dinning room Too remote

FName David David Ann Ann Julie Mary

LName Ford Ford Beech Beech Lee Howe

Pno PG4 PG16 PG21 PG36 PL94 PA14

MIS 235 Sircar

Exercise #20 Read the tables: For each branch, list the staff who manage properties, including the city in which the branch is located and the properties they manage. Write the SQL to produce the equivalent resulting table. Answer: Result Table: Bno City B3 Glasgow B3 Glasgow B3 Glasgow B3 Glasgow B5 London B7 Glasgow SQL:

Sno SG14 SG14 SG37 SG37 SL41 SA9

FName David David Ann Ann Julie Mary

LName Ford Ford Beech Beech Lee Howe

Pno PG4 PG16 PG21 PG36 PL94 PA14

Вам также может понравиться