SQL Practice Questions
Consider the following schema definitions:
Branch (branchNo, street, city, postcode)
Staff (staffNo, fName,lName, position, sex, DOB, salary, branchNo)
PropertyforRent (propertyNo, street, city, postcode, type, rooms, rent, ownerNo,
staffNo, branchNo)
Client (clientNo, fName, lName, telNo, prefType, maxRent)
PrivateOwner (ownerNo, fName, lName, address, telNo)
Viewing (clientNo, propertyNo, viewDate, comment)
Registration (clientNo, branchNo, staffNo, dateJoined)
1. List the address of all branch offices in London or Bristol.
2. List the staff with a salary between $10000 and $30000.
3. List the staff in descending order of salary.
4. Find the number of different properties viewed in April 2004.
5. Find the minimum, maximum and average staff salary.
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.
7. List the staff who work in the branch whose stree adress is ‘163 Main Street’
8. Find all staff whose salary is larger than the salary of every staff member at branch
with branchNo 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.
10. List the clients who have viewed a property.
11. Find the list of all cities where there is both a branch office and a property
12. Give all managers 5% increase to their salary
13. Delete all viewings that belong to property with property number PG4.
Given the following schema definitions:
message (message_id, subject, body)
sent_to(message_id, email, senddate)
customer (email, name, family_size, address)
1. Find the names and emails of all customers who have sent the message with
subject “Happy New Year”
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.
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.
4. Find all customers each having a family size different from the total number of
customers having the same address as him or her.
Consider the following relation schema for an airline database.
customer(id, name, age, gender)
onFlight(id, flightNo, flightDate)
flightInfo(flightNo, fromCity, toCity, startTime, duration)
1. Names of all customers above the age of 10.
2. Flights (flightNo, flightDate) on which there are at least two customers
3. Flights (flightNo, flightDate) on which there are at least two customers, as well as
the number of passengers on the flights
4. Names of passengers who flew on flight “TK102” at least once
5. Names of customers who never flew on any flight
6. Names of customers who flew on the same flight as Mr. Joe
7. The number of passengers on flight “TK101” on “1/2/1999”
8. The most popular destination (i.e. the city which received the most number of
travellers)
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.