2. CAR HIRE DATABASE EXAMPLE
• The following SQL query designs will relate to a car hire database which
consists of 4 tables:
Customer Booking Car Depot
places contain
s
is stored at
3. CAR HIRE DATABASE EXAMPLE
• The following SQL query designs will relate to a car hire database which
consists of 4 tables:
Customer
customerID
forename
surname
telephone
houseNum
postcode
Booking
bookingRef
customerID *
registration *
daysBooked
startDate
Car
registration
make
model
colour
depotID *
automatic
dailyPrice
mileage
Depot
depotID
depotName
depotCity
onlineBookin
g
depotHours
employees
4. QUERY DESIGN
Field(s) and calculation(s)
Table(s) and query
Search criteria
Grouping
Sort order
• Query designs should include the information shown below
• Depending on the query, not all elements need to be filled
5. EXAMPLE 1
Field(s) and calculation(s)
Table(s) and query
Search criteria
Grouping
Sort order
• Design a query to show the make, model and mileage of a car in a depot
which is open all day, and where the make of car starts with ‘M’.
6. EXAMPLE 1
Field(s) and calculation(s) make, model, mileage, depotName, depotHours
Table(s) and query car, depot
Search criteria depotHours = ‘All Day’ and model like ‘M%’
Grouping
Sort order
• Design a query to show the make, model, mileage and depot of any car
in a depot that is open all day and where the make of car starts with ‘M’.
7. EXAMPLE 2
Field(s) and calculation(s)
Table(s) and query
Search criteria
Grouping
Sort order
• Design a query to show a customer’s full name, booking ID, car make and
model, and depot name for all customers who have ‘e’ as the 5th letter of
their surname
• List the details in alphabetical order of the customer surname
8. EXAMPLE 2
Field(s) and calculation(s) forename, surname, bookingRef, make, model,
depotName
Table(s) and query customer, booking, car, depot
Search criteria surname LIKE = ‘_ _ _ _ e %’
Grouping
Sort order surname ASC
• Design a query to show a customer’s full name, booking ID, car make and
model, and depot name for all customers who have ‘e’ as the 5th letter of
their surname
• List the details in alphabetical order of the customer surname
9. EXAMPLE 3
Field(s) and calculation(s)
Table(s) and query
Search criteria
Grouping
Sort order
• Design a query to display the average mileage of all cars in the car table.
10. EXAMPLE 3
Field(s) and calculation(s) AVG(mileage)
Table(s) and query car
Search criteria
Grouping
Sort order
• Design a query to display the average mileage of all cars in the car table.
11. EXAMPLE 4
Field(s) and calculation(s)
Table(s) and query
Search criteria
Grouping
Sort order
• Design a query that uses a readable heading to display the lowest and
highest mileages of the cars in the car table.
12. EXAMPLE 4
Field(s) and calculation(s) Lowest Mileage = MIN(mileage),
Highest Mileage = MAX(mileage)
Table(s) and query car
Search criteria
Grouping
Sort order
• Design a query that uses a readable heading to display the lowest and
highest mileages of the cars in the car table.
13. EXAMPLE 5
Field(s) and calculation(s)
Table(s) and query
Search criteria
Grouping
Sort order
• Design a query to display the cities that have depot, together with the
number of depots in each city.
14. EXAMPLE 5
Field(s) and calculation(s) depotCity, COUNT(*)
Table(s) and query depot
Search criteria
Grouping depotCity
Sort order
• Design a query to display the cities that have depot, together with the
number of depots in each city.
15. EXAMPLE 6
Field(s) and calculation(s)
Table(s) and query
Search criteria
Grouping
Sort order
• Design a query to display the number of employees that are based in
depots in Edinburgh or Glasgow. Use ‘Edinburgh/Glasgow Employees’ as
a readable heading.
16. EXAMPLE 6
Field(s) and calculation(s) Edinburgh/Glasgow Employees = SUM(staff)
Table(s) and query depot
Search criteria depotCity = ‘Edinburgh’ OR depotCity = ‘Glasgow’
Grouping
Sort order
• Design a query to display the number of employees that are based in
depots in Edinburgh or Glasgow. Use ‘Edinburgh/Glasgow Employees’ as
a readable heading.
17. EXAMPLE 7
Field(s) and calculation(s)
Table(s) and query
Search criteria
Grouping
Sort order
• Design a query to display a list of each car colour, together with the
number of bookings made for cars of those colours.
• List the details from the least popular colour to the most popular colour.
18. EXAMPLE 7
Field(s) and calculation(s) colour, COUNT(*)
Table(s) and query car, booking
Search criteria
Grouping colour
Sort order COUNT(*) ASC
• Design a query to display a list of each car colour, together with the
number of bookings made for cars of those colours.
• List the details from the least popular colour to the most popular colour.
19. EXAMPLE 8
Field(s) and calculation(s)
Table(s) and query
Search criteria
Grouping
Sort order
• Design a query that uses a readable heading to display the total number
of days cars have been booked out during May.
20. EXAMPLE 8
Field(s) and calculation(s) Days cars booked in May = SUM(daysBooked)
Table(s) and query booking
Search criteria startDate LIKE ‘%/05/%’
Grouping
Sort order
• Design a query that uses a readable heading to display the total number
of days cars have been booked out during May.
21. EXAMPLE 9
Field(s) and calculation(s)
Table(s) and query
Search criteria
Grouping
Sort order
• Design a query to display the car registration and increased price, if all
the cars from the Inverness depot increase their daily price by £5.
22. EXAMPLE 9
Field(s) and calculation(s) registration, Increased Price = dailyPrice + 5
Table(s) and query car, depot
Search criteria depotCity = ‘Inverness’
Grouping
Sort order
• Design a query to display the car registration and increased price, if all
the cars from the Inverness depot increase their daily price by £5.
23. EXAMPLE 10
Field(s) and calculation(s)
Table(s) and query
Search criteria
Grouping
Sort order
• Design a query to display the surname, booking reference, number of
days booked, daily price, and a calculated total cost of each booking
(with a readable column heading).
• Display the most expensive booking first.
24. EXAMPLE 10
Field(s) and calculation(s) surname, bookingRef, daysBooked, dailyPrice,
Total Cost = daysBooked * dailyPrice
Table(s) and query customer, booking, car
Search criteria
Grouping
Sort order daysBooked * dailyPrice DESC
• Design a query to display the surname, booking reference, number of
days booked, daily price, and a calculated total cost of each booking
(with a readable column heading).
• Display the most expensive booking first.
25. EXAMPLE 11
• Design a query to display the name of depots that have cars with the
cheapest daily price together with the price (use a readable heading to
display the cheapest price).
• Since it is not possible to use an aggregate function in a WHERE clause,
this solution requires two separate queries.
• Query 1: a simple query to generate a single value (the cheapest daily price)
• Query 2: uses Query 1 to find the depots with the cheapest daily price
26. EXAMPLE 11
Query 1 – Find Minimum Daily Price
Field(s) and calculation(s) Cheapest Daily Price = MIN(dailyPrice)
Table(s) and query car
Search criteria
Grouping
Sort order
• Design a query to display the name of depots that have cars with the
cheapest daily price together with the price (use a readable heading to
display the cheapest price).
27. EXAMPLE 11
Query 2 – Display names of depots with cars at cheapest daily price
Field(s) and calculation(s) depotName, Cheapest Daily Price
Table(s) and query car, depot, Find Minimum Daily Price
Search criteria dailyPrice = Cheapest Daily Price
Grouping
Sort order
• Design a query to display the name of depots that have cars with the
cheapest daily price together with the price (use a readable heading to
display the cheapest price).
28. EXAMPLE 12
• Design a query to display the details of any automatic car with a daily
price that is above the average daily price. The query should display the
car make, model, colour, automatic and daily price.
• The most expensive cars should be listed first.
• This solution requires two separate queries.
• Query 1: a simple query to generate a single value (the average daily price)
• Query 2: uses Query 1 to find the automatic cars that are above the average
daily price
29. EXAMPLE 12
Query 1 – Find Average Daily Price
Field(s) and calculation(s) Average Daily Price = AVG(dailyPrice)
Table(s) and query car
Search criteria
Grouping
Sort order
• Design a query to display the details of any automatic car with a daily
price that is above the average daily price. The query should display the
car make, model, colour, automatic and daily price.
• The most expensive cars should be listed first.
30. EXAMPLE 12
Query 2 – Display automatic cars with daily price above average daily price
Field(s) and calculation(s) make, model, colour, automatic, dailyPrice,
Average Daily Price
Table(s) and query car, Find Average Daily Price
Search criteria dailyPrice > Average Daily Price and automatic =
TRUE
Grouping
Sort order dailyPrice DESC
• Design a query to display the details of any automatic car with a daily
price that is above the average daily price. The query should display the
car make, model, colour, automatic and daily price.
• The most expensive cars should be listed first.