CD - Members CD - Bookings CD - Facilities: Column Name Data Type Column Name Data Type Column Name

You might also like

Download as xlsx, pdf, or txt
Download as xlsx, pdf, or txt
You are on page 1of 6

The dataset for these exercises is for a newly created country club, with a set of members, facilities such

as tennis courts, and


booking history for those facilities. Amongst other things, the club wants to understand how they can use their information to
analyse facility usage/demand. Please note: this dataset is designed purely for supporting an interesting array of exercises, and the database schema
is flawed in several aspects - please don't take it as an example of good design.

cd.members cd.bookings cd.facilities


Column Name Data Type Column Name Data Type Column Name
memid integer memid integer facid
surname varchar(200) facid integer name
firstname varchar(200) no_of_slots_booked integer membercost
address varchar(300) starttime timestamp guestcost
zipcode integer initialcost
telephone varchar(200) monthlymaintenance
recommendedby integer
joindate timestamp

POINTS TO NOTE
Recommendedby is the memid of the person who recommended
in cd.bookings, a booking can be made by a guest or by a member. In case of guest, cd.bookings.memid = 0 otherwise it is = m
No of hours =( (sum(No of slots) + 10)/20)*10
If booking made by guest then revenue = no_of_Slots_booked * guestcost
If booking made by member then revenue = no_of_Slots_booked * membercost
Total Revenue of facilities = sum of revenue of all bookings made for that facility
Total Revenue on a date = sum of revenue of all bookings made on that date (refer to starttime column)
uch as tennis courts, and
use their information to
cises, and the database schema

cd.facilities
Data Type
integer
varchar(200)
numeric
numeric
numeric
numeric

mid = 0 otherwise it is = memid of cd.members


Q1 Create a list of start times for bookings made by member named "Darren Smith"
Create a list of all members who have recommended another member? Ensure that there are no
Q2 duplicates in the list, and that results are ordered by (surname, firstname).
Create a list of all members who have used a tennis court? Include in your output the name of the court,
and the name of the member formatted as a single column. Ensure no duplicate data, and order by the
Q3 member name followed by the facility name.

Count of the number of facilities that have a cost to guests of 9 or more.


Q4
Get the total number of slots booked per facility in the month of September 2012. Produce an output table
Q5 consisting of facility id and slots, sorted by the number of slots.

List each member's first booking after September 1st 2012


Q6
Output the facility id that has the highest number of slots booked. Ensure that in the event of a tie, all
Q7 tieing results get output.
Produce a list of members, along with the number of hours they've booked in facilities. Rank them by the
number of hours producing output of first name, surname, hours, rank. Sort by rank, surname, and first
Q8 name.
Produce a list of the top three revenue generating facilities (including ties). Output facility name and rank,
Q9 sorted by rank and facility name.
For each day in August 2012, calculate a rolling average of total revenue over the previous 15 days. Output
should contain date and revenue columns, sorted by the date. Remember to account for the possibility of
Q10 a day having zero revenue.
here are no

name of the court,


, and order by the

oduce an output table

event of a tie, all

s. Rank them by the


surname, and first

cility name and rank,

vious 15 days. Output


for the possibility of
1 Ans: - SELECT starttime FROM bookings
INNER JOIN members ON (bookings memid = members.memid)
WHERE surname = 'Smith' AND firstname = 'Darren';

2Ans: - SELECT DISTINCT m.firstname, m.surname


FROM members AS m2
INNER JOIN members AS (m.memid = m2.recommendedby)
ORDERE BY m.surname, m.firstname;

3Ans: - SELECT DISTINCT m.firstname||||' ''' || m.surname AS member, f.name AS facility


FROM members AS m
INNER JOIN bookings AS b ON (m.memid = b.memid)
INNER JOIN facilities AS f ON (b.facid = f.facid)
WHERE f.name LIKE 'Tennis%'
ORDERE BY member, facility;

4Ans: - SELECT COUNT(facid) FROM facilties WHERE guestcost >= 9

5Ans:- SELECT facid, date_part('month' , starttime), SUM(slots)


FROM bookings
WHERE date_part('year' , starttime) = 2012
GROUUP BY facid, date_part('month' , starttime)
ORDERE BY facid, date_part('month' , starttime)

6Ans: - SELECT m.surname, m.firstname, m.memid, min(b.starttime) as starttime


FROM members AS m
INNER JOIN bookings AS b ON b.memid = m.memid
WHERE starttime >= '2012-09-01'
GROUP BY m.surname, m.firstname, m.memid

7Ans: - SELECT facid, FROM (


SELECT facid, SUM(slots) AS total,
rank() OVER (order by SUM(slots) DESC) AS rank
FROM bookings
GROUP BY facid)
AS ranked WHERE rank = 1

8Ans: - SELECT firstname, surname,


((SUM(bks.slots)+10)/20*10 as hours,
rank() over (order by ((sum(bks.slots)+10)/20)*10desc) as rank
FROM members AS mems
INNER JOIN bookings AS bks ON mems.memid = bks.memid
GROUP BY mems.memid
ORDER BY rank, surname, firstname;

9Ans: - SELECT NAME, rank FROM(


SELECT f.name, RANK() OVER (ORDER BY SUM(
CASE WHEN memid = 0 THEN slots * f.guestcost
ELSE slots * f.membercost END) DESC) AS rank
FROM bookings
INNER JOIN facilities S f ON bookings.facid = f.facid
GROUP BY f.name) AS subq
ORDER BY rank;

You might also like