SQL Practice Questions Consider the following schema definitions: Branch Staff (branchNo, street, city, postcode) (staffNo

, fName,lName, position, sex, DOB, salary, branchNo) staffNo, branchNo) Client Viewing Registration (clientNo, fName, lName, telNo, prefType, maxRent) (clientNo, propertyNo, viewDate, comment) (clientNo, branchNo, staffNo, dateJoined) PrivateOwner (ownerNo, fName, lName, address, telNo)

PropertyforRent (propertyNo, street, city, postcode, type, rooms, rent, ownerNo,

An instance of the above schemas is given in the last page of the examination. (You may detach and use it if necessary) For each case below, fill in the blanks such that the SQL queries correspond to the English language queries stated. Each blank is worth 2 points. 1. List the address of all branch offices in London or Bristol. SELECT _______*_______ FROM ___branch______ WHERE city=’London’ _OR city=’bristol’______ 2. List the staff with a salary between $10000 and $30000. SELECT staff_No FROM Staff WHERE __salary between 10000 AND 30000________________

SELECT staff_No. SELECT _min(salary)____. Find the minimum. Find the number of different properties viewed in April 2004. _count(staffno)_. SELECT __count (distinct propert_no) FROM Viewing WHERE viewDate BETWEEN ‘1-Apr-04’ AND ’30-Apr-04’ 5.3. List the staff in descending order of salary. maximum and average staff salary. _avg(salary)_____ FROM Staff 6. find the number of staff working in each branch and the sum of their salaries. __sum(salary)___ FROM Staff GROUP BY branchNo HAVING __count(staffNo) >1 . salary FROM Staff ORDER BY __salary DESC__________________ 4. _max(salary)_. For each branch office with more than one member of staff. SELECT branchNo.

List the clients who have viewed a property. List the staff who work in the branch whose stree adress is ‘163 Main Street’ SELECT staffNo. including the city in which the branch is located and the properties that the staff manage. SELECT b. list the numbers and names of staff who manage properties. position.city. b. properyNo FROM Branch AS b. Find all staff whose salary is larger than the salary of every staff member at branch with branchNo B003. fName. fName. propertyNo.branchNo.branchNo AND _s.staffNo=p. lName. lName.7. viewDate FROM __client natural innerjoin viewing_____ . fName. lName.staffNo. fName. SELECT clientNo. For each branch. s. salary FROM Staff WHERE _salary > ALL__________(SELECT salary FROM __staff_______________ WHERE brancNo=’B003’) 9.staffno 10.branchNo = s. _propertyforRent p WHERE b. lName. SELECT staffNo. Staff AS s. FROM Staff WHERE _branchNo________ = (SELECT branchNo FROM _branch__________ WHERE _street=’163 Main str’_) 8.

Give all managers 5% increase to their salary UPDATE __staff_____________ SET __salary=salary*1. Delete all viewings that belong to property with property number PG4.05 WHERE position=’Manager’ 13. Find the list of all cities where there is both a branch office and a property (SELECT city FROM Branch) ___INTERSECT________ (SELECT city FROM _PropertyforRent__) 12. DELETE FROM __viewing__________ WHERE _propertyNo=’P64’__ .11.

address=c2. email. subject. C1. (7 pts) select C1. _C2.family_size=2 AND _c1.email 2. _c1.A. customer where _subject=’Happy New Year’ AND message. body) sent_to(message_id. name. family_size.address___ .address from _customer C1.name___ AND. fill in the blanks such that the SQL queries correspond to the English language queries stated. sent_to.message_id AND sent_to. customer C2 where _c1.email. senddate) customer (email. 1.family_size=2_______ AND C2.(21 points) Given the following schema definitions: message (message_id.name<>c2.Find the names and emails of all customers who have sent the message with subject “Happy New Year” (2 pts) select _customer.message_id=sent_to.email=customer. name___ from message.Find the names and common addresses of all pairs of customers who claim to have a famlily size 2 and and have the same address.name___. address) For each case below.name.

Find all customers each having a family size different from the total number of customers having the same address as him or her. count) select address. (6 pts) create view average_size (asize) as select _avg(family_size)______ from _customer___________ select name. Sort the results by family size. (6 pts) create view address_and_count (address.3.Find the names of all customers and their family size whose family size is at least 50% more than the average family size.name from_customer C. count * from _customer______________ group by _address______________ select C.address AND _family_size<>count____ .address=A. family_size from _customer_________average_size where _family_size >= 1.5* a_size ________________ order by _family_size___________ 4. address_and_count A where C.

flightDate) on which there are at least two customers. age. Names of all customers above the age of 10 SELECT _______________name FROM customer WHERE ________________ age>10 2.flightNo = f2.flightDate AND __________________ f1. startTime. as well as the number of passengers on the flights SELECT flightNo. gender) onFlight(id. except the first one.flightDate FROM onFlight as f1. flightDate HAVING _____________________ howMany>1 .id 3.flightDate=f2. Flights (flightNo. f1. customer(id. name. flightDate) flightInfo(flightNo.id <> f2.Consider the following relation schema for an airline database. which is worth 1 pt. flightDate. fromCity. onFlight as f2 WHERE f1. Fill in the missing slots in each ofd the queries below.flightNo AND f1. flightNo.flightNo. 1. duration) Assume all flights take place every day. toCity. flightDate) on which there are at least two customers SELECT f1. count(id) as howMany FROM onFlight GROUP BY _____________________ flightNo. Flights (flightNo. Each slot is worth 2 pts.

Names of customers who flew on the same flight as Mr.flightNo=”TK102” 5. onFlight WHERE ____________________ customer. Names of passengers who flew on flight “TK102” at least once SELECT name FROM customer.flightNo 7. Joe WITH joeFlight(flightNo) AS SELECT flightNo FROM ___________________customer natural inner join onFlight WHERE name = “Joe” SELECT name FROM customer. joeFlight WHERE ___________________ customer.id AND ____________________ onFlight. The number of passengers on flight “TK101” on “1/2/1999” SELECT _________________ count(id ) .4. Names of customers who never flew on any flight SELECT name FROM customer ____________________ left outer join flight WHERE _______________ flightNo = NULL 6.flightNo = joeFlight. onFlight.id=onFlight.id = onFlight.id AND _______________________ onFlight.

FROM onFlight WHERE flightNo= “TK101” AND flightDate=“1/2/1999” .

e.HowMany = mostTourist. only one of those visits should be counted. The most popular destination (i.8. How many passengers ever flew to Istanbul? If somebody travelled to Istanbul more than one time. count(*) FROM onFlight natural inner join flightInfo GROUP BY toCity WITH mostTourist(HowMany) AS SELECT ___________________ max(HowMany) FROM ___________________ city_tourists SELECT toCity FROM ______________________________ city_tourists. the city which received the most number of travellers) WITH city_tourists(toCity.HowMany) AS SELECT toCity. mostTourist WHERE _____________________________ city_tourists. SELECT ___________________count (distinct id) FROM onFlight natural inner join flightInfo WHERE to_city = “Istanbul” .HowMany 9.

Sign up to vote on this title
UsefulNot useful