|
| 1 | +/* Welcome to the SQL mini project. For this project, you will use |
| 2 | +Springboard' online SQL platform, which you can log into through the |
| 3 | +following link: |
| 4 | +
|
| 5 | +https://sql.springboard.com/ |
| 6 | +Username: student |
| 7 | +Password: learn_sql@springboard |
| 8 | +
|
| 9 | +The data you need is in the "country_club" database. This database |
| 10 | +contains 3 tables: |
| 11 | + i) the "Bookings" table, |
| 12 | + ii) the "Facilities" table, and |
| 13 | + iii) the "Members" table. |
| 14 | +
|
| 15 | +Note that, if you need to, you can also download these tables locally. |
| 16 | +
|
| 17 | +In the mini project, you'll be asked a series of questions. You can |
| 18 | +solve them using the platform, but for the final deliverable, |
| 19 | +paste the code for each solution into this script, and upload it |
| 20 | +to your GitHub. |
| 21 | +
|
| 22 | +Before starting with the questions, feel free to take your time, |
| 23 | +exploring the data, and getting acquainted with the 3 tables. */ |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +/* Q1: Some of the facilities charge a fee to members, but some do not. |
| 28 | +Please list the names of the facilities that do. */ |
| 29 | + |
| 30 | +SELECT name |
| 31 | +FROM Facilities |
| 32 | +WHERE membercost !=0 |
| 33 | + |
| 34 | + |
| 35 | +/* Q2: How many facilities do not charge a fee to members? */ |
| 36 | + |
| 37 | +SELECT COUNT(*) FROM Facilities |
| 38 | +WHERE membercost != 0 |
| 39 | + |
| 40 | + |
| 41 | +/* Q3: How can you produce a list of facilities that charge a fee to members, |
| 42 | +where the fee is less than 20% of the facility's monthly maintenance cost? |
| 43 | +Return the facid, facility name, member cost, and monthly maintenance of the |
| 44 | +facilities in question. */ |
| 45 | + |
| 46 | +SELECT facid, name, membercost, monthlymaintenance FROM Facilities |
| 47 | +WHERE membercost < monthlymaintenance * 0.2 and membercost != 0 |
| 48 | + |
| 49 | +/* Q4: How can you retrieve the details of facilities with ID 1 and 5? |
| 50 | +Write the query without using the OR operator. */ |
| 51 | + |
| 52 | +SELECT * FROM Facilities |
| 53 | +WHERE facid IN (1,5) |
| 54 | + |
| 55 | +/* Q5: How can you produce a list of facilities, with each labelled as |
| 56 | +'cheap' or 'expensive', depending on if their monthly maintenance cost is |
| 57 | +more than $100? Return the name and monthly maintenance of the facilities |
| 58 | +in question. */ |
| 59 | + |
| 60 | +SELECT name, monthlymaintenance |
| 61 | + CASE WHEN monthlymaintenance > 100 THEN 'expensive' |
| 62 | + ELSE 'cheap' END AS price |
| 63 | + FROM Facilities |
| 64 | + |
| 65 | + |
| 66 | +/* Q6: You'd like to get the first and last name of the last member(s) |
| 67 | +who signed up. Do not use the LIMIT clause for your solution. */ |
| 68 | + |
| 69 | +SELECT firstname, surname, joindate FROM Members |
| 70 | + ORDER BY joindate DESC |
| 71 | + |
| 72 | + |
| 73 | +/* Q7: How can you produce a list of all members who have used a tennis court? |
| 74 | +Include in your output the name of the court, and the name of the member |
| 75 | +formatted as a single column. Ensure no duplicate data, and order by |
| 76 | +the member name. */ |
| 77 | + |
| 78 | +SELECT DISTINCT concat(firstname, ' ', surname) AS member_name, name FROM Bookings |
| 79 | +JOIN Members |
| 80 | +ON Bookings.memid = Members.memid |
| 81 | +JOIN Facilities |
| 82 | +ON Bookings.facid = Facilities.facid |
| 83 | +WHERE name LIKE "Tennis Court%" |
| 84 | +ORDER BY firstname |
| 85 | + |
| 86 | +/* Q8: How can you produce a list of bookings on the day of 2012-09-14 which |
| 87 | +will cost the member (or guest) more than $30? Remember that guests have |
| 88 | +different costs to members (the listed costs are per half-hour 'slot'), and |
| 89 | +the guest user's ID is always 0. Include in your output the name of the |
| 90 | +facility, the name of the member formatted as a single column, and the cost. |
| 91 | +Order by descending cost, and do not use any subqueries. */ |
| 92 | + |
| 93 | +SELECT DISTINCT firstname, CASE WHEN firstname = 'GUEST' THEN guestcost ELSE membercost END AS cost, |
| 94 | +name, concat(firstname, ' ', surname) AS member_name FROM Bookings |
| 95 | +JOIN Members |
| 96 | +ON Bookings.memid = Members.memid |
| 97 | +JOIN Facilities |
| 98 | +ON Bookings.facid = Facilities.facid |
| 99 | +WHERE starttime LIKE "2012-09-14%" |
| 100 | +ORDER BY cost DESC |
| 101 | + |
| 102 | +/* Q9: This time, produce the same result as in Q8, but using a subquery. */ |
| 103 | + |
| 104 | +SELECT firstname, CASE WHEN firstname = 'GUEST' THEN guestcost ELSE membercost END AS cost, |
| 105 | +name, concat(firstname, ' ', surname) AS member_name FROM ( |
| 106 | +SELECT * |
| 107 | +FROM Bookings |
| 108 | +WHERE starttime LIKE "2012-09-14%" |
| 109 | +) AS test |
| 110 | +JOIN Members |
| 111 | +ON test.memid = Members.memid |
| 112 | +JOIN Facilities |
| 113 | +ON test.facid = Facilities.facid |
| 114 | +ORDER BY cost DESC |
| 115 | + |
| 116 | + |
| 117 | +/* Q10: Produce a list of facilities with a total revenue less than 1000. |
| 118 | +The output of facility name and total revenue, sorted by revenue. Remember |
| 119 | +that there's a different cost for guests and members! */ |
| 120 | + |
| 121 | +SELECT * |
| 122 | +FROM ( |
| 123 | +SELECT name, SUM(CASE WHEN firstname = 'GUEST' THEN guestcost ELSE membercost END) AS cost |
| 124 | +FROM Bookings |
| 125 | +JOIN Members |
| 126 | +ON Bookings.memid = Members.memid |
| 127 | +JOIN Facilities |
| 128 | +ON Bookings.facid = Facilities.facid |
| 129 | +Group By name |
| 130 | +ORDER BY cost DESC |
| 131 | +) AS test |
| 132 | +Where cost < 1000 |
0 commit comments