You are on page 1of 10

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________________

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

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

11. 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. Give all managers 5% increase to their salary UPDATE __staff_____________ SET __salary=salary*1.05 WHERE position=Manager 13. Delete all viewings that belong to property with property number PG4. DELETE FROM __viewing__________ WHERE _propertyNo=P64__

A- (21 points) Given the following schema definitions: message (message_id, subject, body) sent_to(message_id, email, senddate) customer (email, name, family_size, address) For each case below, fill in the blanks such that the SQL queries correspond to the English language queries stated. 1- Find the names and emails of all customers who have sent the message with subject Happy New Year (2 pts) select _customer.email, name___ from message, sent_to, customer where _subject=Happy New Year AND message.message_id=sent_to.message_id AND sent_to.email=customer.email 2- 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. (7 pts) select C1.name, _C2.name___, C1.address from _customer C1, customer C2 where _c1.name<>c2.name___ AND, _c1.family_size=2_______ AND C2.family_size=2 AND _c1.address=c2.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. Sort the results by family size. (6 pts) create view average_size (asize) as select _avg(family_size)______ from _customer___________ select name, family_size from _customer_________average_size where _family_size >= 1.5* a_size ________________ order by _family_size___________ 4- Find all customers each having a family size different from the total number of customers having the same address as him or her. (6 pts) create view address_and_count (address, count) select address, count * from _customer______________ group by _address______________ select C.name from_customer C, address_and_count A where C.address=A.address AND _family_size<>count____

Consider the following relation schema for an airline database. customer(id, name, age, gender) onFlight(id, flightNo, flightDate) flightInfo(flightNo, fromCity, toCity, startTime, duration) Assume all flights take place every day. Fill in the missing slots in each ofd the queries below. Each slot is worth 2 pts, except the first one, which is worth 1 pt. 1. Names of all customers above the age of 10 SELECT _______________name FROM customer WHERE ________________ age>10

2. Flights (flightNo, flightDate) on which there are at least two customers SELECT f1.flightNo, f1.flightDate FROM onFlight as f1, onFlight as f2 WHERE f1.flightNo = f2.flightNo AND f1.flightDate=f2.flightDate AND __________________ f1.id <> f2.id

3. Flights (flightNo, flightDate) on which there are at least two customers, as well as the number of passengers on the flights SELECT flightNo, flightDate, count(id) as howMany FROM onFlight GROUP BY _____________________ flightNo, flightDate HAVING _____________________ howMany>1

4. Names of passengers who flew on flight TK102 at least once SELECT name FROM customer, onFlight WHERE ____________________ customer.id=onFlight.id AND

____________________ onFlight.flightNo=TK102

5. Names of customers who never flew on any flight SELECT name FROM customer ____________________ left outer join flight WHERE _______________ flightNo = NULL

6. Names of customers who flew on the same flight as Mr. Joe WITH joeFlight(flightNo) AS SELECT flightNo FROM ___________________customer natural inner join onFlight WHERE name = Joe SELECT name FROM customer, onFlight, joeFlight WHERE ___________________ customer.id = onFlight.id AND

_______________________ onFlight.flightNo = joeFlight.flightNo

7. The number of passengers on flight TK101 on 1/2/1999 SELECT _________________ count(id )

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

8. The most popular destination (i.e. the city which received the most number of travellers) WITH city_tourists(toCity,HowMany) AS SELECT toCity, 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, mostTourist WHERE _____________________________ city_tourists.HowMany =

mostTourist.HowMany

9. How many passengers ever flew to Istanbul? If somebody travelled to Istanbul more than one time, only one of those visits should be counted. SELECT ___________________count (distinct id) FROM onFlight natural inner join flightInfo WHERE to_city = Istanbul

You might also like