-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.3_CaseStudy_SQLTasks_Tier_2.sql
More file actions
252 lines (180 loc) · 7.25 KB
/
Copy path8.3_CaseStudy_SQLTasks_Tier_2.sql
File metadata and controls
252 lines (180 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* Welcome to the SQL mini project. You will carry out this project partly in
the PHPMyAdmin interface, and partly in Jupyter via a Python connection.
This is Tier 2 of the case study, which means that there'll be less guidance for you about how to setup
your local SQLite connection in PART 2 of the case study. This will make the case study more challenging for you:
you might need to do some digging, aand revise the Working with Relational Databases in Python chapter in the previous resource.
Otherwise, the questions in the case study are exactly the same as with Tier 1.
PART 1: PHPMyAdmin
You will complete questions 1-9 below in the PHPMyAdmin interface.
Log in by pasting the following URL into your browser, and
using the following Username and Password:
URL: https://sql.springboard.com/
Username: student
Password: learn_sql@springboard
The data you need is in the "country_club" database. This database
contains 3 tables:
i) the "Bookings" table,
ii) the "Facilities" table, and
iii) the "Members" table.
In this case study, you'll be asked a series of questions. You can
solve them using the platform, but for the final deliverable,
paste the code for each solution into this script, and upload it
to your GitHub.
Before starting with the questions, feel free to take your time,
exploring the data, and getting acquainted with the 3 tables. */
/* QUESTIONS
/* Q1: Some of the facilities charge a fee to members, but some do not.
Write a SQL query to produce a list of the names of the facilities that do. */
use country_club;
select distinct name, membercost
from Facilities
where membercost > 0
/* Q2: How many facilities do not charge a fee to members? */
use country_club;
select count(distinct name)
from Facilities
where membercost = 0
/* Q3: Write an SQL query to show a list of facilities that charge a fee to members,
where the fee is less than 20% of the facility's monthly maintenance cost.
Return the facid, facility name, member cost, and monthly maintenance of the
facilities in question. */
use country_club;
select distinct facid, name, membercost, monthlymaintenance
from Facilities
where membercost < 0.2*monthlymaintenance
and membercost >0
/* Q4: Write an SQL query to retrieve the details of facilities with ID 1 and 5.
Try writing the query without using the OR operator. */
use country_club;
select *
from Facilities
where facid in (1,5)
/* Q5: Produce a list of facilities, with each labelled as
'cheap' or 'expensive', depending on if their monthly maintenance cost is
more than $100. Return the name and monthly maintenance of the facilities
in question. */
use country_club;
select name, monthlymaintenance,
case when monthlymaintenance>100 then 'expensive'
else 'cheap' end as label
from Facilities
/* Q6: You'd like to get the first and last name of the last member(s)
who signed up. Try not to use the LIMIT clause for your solution. */
use country_club;
select joindate, firstname, surname
from Members
where joindate = (select max(joindate) from Members)
/* Q7: Produce 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 member name. */
use country_club;
select distinct concat(m.firstname, ' ' , m.surname) as membername, f.name
from Members as m
left join Bookings as b
on m.memid = b.memid
left join Facilities as f
on b.facid = f.facid
where f.name like 'Tennis%'
order by membername
/* Q8: Produce a list of bookings on the day of 2012-09-14 which
will cost the member (or guest) more than $30. Remember that guests have
different costs to members (the listed costs are per half-hour 'slot'), and
the guest user's ID is always 0. Include in your output the name of the
facility, the name of the member formatted as a single column, and the cost.
Order by descending cost, and do not use any subqueries. */
use country_club;
select distinct concat(m.firstname, ' ' , m.surname) as membername, f.name,
case when b.memid = 0 then guestcost*slots
else membercost*slots end as cost
from Members as m
left join Bookings as b
on m.memid = b.memid
left join Facilities as f
on b.facid = f.facid
where extract(year from starttime) = 2012
and extract(month from starttime) = 09
and extract(day from starttime) = 14
and case when b.memid = 0 then guestcost*slots
else membercost*slots end > 30
order by cost desc
/* Q9: This time, produce the same result as in Q8, but using a subquery. */
use country_club;
select *
from (select distinct concat(m.firstname, ' ' , m.surname) as membername, f.name,
case when b.memid = 0 then guestcost*slots
else membercost*slots end as cost
from Members as m
left join Bookings as b
on m.memid = b.memid
left join Facilities as f
on b.facid = f.facid
where extract(year from starttime) = 2012
and extract(month from starttime) = 09
and extract(day from starttime) = 14) as x
where cost> 30
order by cost desc
/* PART 2: SQLite
Export the country club data from PHPMyAdmin, and connect to a local SQLite instance from Jupyter notebook
for the following questions.
QUESTIONS:
/* Q10: Produce a list of facilities with a total revenue less than 1000.
The output of facility name and total revenue, sorted by revenue. Remember
that there's a different cost for guests and members! */
import sqlite3
conn = sqlite3.connect('sqlite_db_pythonsqlite.db')
c = conn.cursor()
c.execute("""select f.name,
sum(case when b.memid = 0 then guestcost*slots
else membercost*slots end) as revenue
from Members as m
left join Bookings as b
on m.memid = b.memid
left join Facilities as f
on b.facid = f.facid
group by f.name
having revenue < 1000
order by revenue desc""")
print(c.fetchall())
conn.commit()
conn.close()
/* Q11: Produce a report of members and who recommended them in alphabetic surname,firstname order */
import sqlite3
conn = sqlite3.connect('sqlite_db_pythonsqlite.db')
c = conn.cursor()
c.execute("""select m1.firstname, m1.surname, m1.recommendedby, m2.firstname, m2.surname
from Members as m1
left join Members as m2
on m1.recommendedby = m2.memid
order by m1.surname, m1.firstname, m2.surname, m2.firstname""")
print(c.fetchall())
conn.commit()
conn.close()
/* Q12: Find the facilities with their usage by member, but not guests */
import sqlite3
conn = sqlite3.connect('sqlite_db_pythonsqlite.db')
c = conn.cursor()
c.execute("""select b.memid, sum(b.slots) as total_usage
from Facilities as f
left join Bookings as b
on f.facid = b.facid
where b.memid > 0
group by b.memid
order by total_usage desc""")
print(c.fetchall())
conn.commit()
conn.close()
/* Q13: Find the facilities usage by month, but not guests */
import sqlite3
conn = sqlite3.connect('sqlite_db_pythonsqlite.db')
c = conn.cursor()
c.execute("""select extract(month from b.starttime) as month, sum(b.slots) as total_usage
from Facilities as f
left join Bookings as b
on f.facid = b.facid
where b.memid > 0
group by extract(month from b.starttime)
order by total_usage desc""")
print(c.fetchall())
conn.commit()
conn.close()