…
…
Calculate durations in twelve weeks of city bike rental records, weed out broken records, and report hour, day and week patterns with date functions.
Harbor Pedal is a fictional system in a harbor city where people rent bikes from stations. The operations team gives you the records of twelve weeks, from September 7 to November 29, 2026, and asks: how many minutes do rentals last, which records are broken, at which hours of the day are bikes busiest, how does weekend use change, which weeks were quiet, and which bikes should go in for maintenance? Every number in the report must be computed from the date and time columns.
Data: two tables, fictional sample data.
bike: id, kind ('classic' or 'electric'), in_service_on (the day the bike went into service; because it was moved from an old system, it is text in the form '03/14/2019', month/day/year) (20 bikes)rental: id, bike_id, member_type ('subscriber' or 'one-time'), started_at (the moment the rental started), ended_at (the moment the bike locked into a station; empty if the lock never closed) (258 rentals)The started_at and ended_at columns are of type TIMESTAMP: the type you met in lessons 25 and 27, which stores the time along with the date (2026-09-07 08:15:00). The tools from lesson 27 work the same way on this type; in this project you will use three small additions:
EXTRACT(HOUR FROM started_at) returns the hour (0–23). Like YEAR, MONTH, DAY and ISODOW, HOUR is a part of EXTRACT.TIMESTAMP from another, the result is an interval, not a number of days (like 00:42:00; the same type AGE returns). To turn the interval into minutes, write EXTRACT(EPOCH FROM ended_at - started_at) / 60: EPOCH gives the total seconds of the interval.DATE_TRUNC('week', started_at) rounds a moment down to the start of its week, that is, that week's Monday; 'month' works the same way. started_at::date drops the time and keeps only the day.Lessons used: grouping and aggregation (11–13), joins (15–16), CASE (8), type, text and date conversions (25–27). The cells contain only comment lines and ___: you will write the query. The first line of every cell starts with "Why this query:"; complete it with one sentence before writing the query (it is not checked, it just helps you think). All steps only read data; you can rerun the cells as often as you like.
What you'll do
bike.sqlDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Before you start the report, get to know the data. The team wants three things: how many rentals there are in total, how many of them have a recorded end time, and which days the records span.
Write a one-row summary: rentals (total rows), returned (rentals with an end time), first_day and last_day (the earliest and latest start, date only, no time).
The gap between rentals and returned is the number of rentals where the bike never locked into a station. You will come back to them in step 3.
This step's code cell is for members.Sign up free
The team's first question is how many minutes rentals last. The duration is not stored in the table; you have to compute it from the start and end times. The EPOCH route from the introduction turns an interval into minutes.
List the 5 longest rentals: id, bike_id, started_at
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
ended_atduration_minidWatch out: if a rental has no recorded end, its duration is unknown too. Remember from lesson 6 where NULLs land in a descending sort; unknown durations must not lead the list.
This step's code cell is for members.Sign up free
Some records will distort the averages as you compute durations. There are two kinds of problems: rentals with no recorded end because the bike never locked into a station, and rentals whose end appears to be before their start. The second is a clock error: computing the duration of those rows gives a negative number.
Gather these records in one list so the team can fix them: id, an issue column holding the name of the problem ('unclosed' or 'negative duration'), started_at and ended_at. Show them in id order.
Compare with the numbers from step 1: how many unclosed rentals did you expect?
This step's code cell is for members.Sign up free
The team is planning at which hours to restock bikes at the stations. For that it is enough to look at which hour of the day rentals start; broken records count in this question, because each one still marks a moment when a bike left a station.
Count the rentals by start hour and show the 5 busiest hours: hour (0–23) and rentals. The busiest hour comes first; when counts tie, the earlier hour comes first.
Do you see two separate rushes, morning and evening? Which user habit could explain them? The next step will test that question.
This step's code cell is for members.Sign up free
The 8 a.m. and 6 p.m. rushes suggest people commuting to work and school. The team wants to check this: how long do rentals last on weekdays and weekends, for subscribers and for one-time users?
Label each rental in a day_type column as 'weekday' or 'weekend'. For each label and member type, show the number of rentals (rentals) and the average duration in minutes, rounded to one decimal (avg_min). This time the broken records from step 3 must stay out. Order: day_type, then member_type.
By how much do the four rows fall short of the total in step 1, and why?
This step's code cell is for members.Sign up free
The business wants to compare the weeks when rentals dropped sharply with the weather records. In a normal week there are between 20 and 27 rentals; the team asks about weeks that fell below 15.
Group the rentals by week. Show the week as that week's Monday (week, date only), add the number of rentals (rentals), and keep only the weeks with fewer than 15 rentals. Sort from oldest to newest. Every rental counts in this question.
The data starts on a Monday and ends on a Sunday, so all twelve weeks are full weeks; if the first and last weeks were partial, they could also look "quiet" by mistake.
This step's code cell is for members.Sign up free
The maintenance team wants to put every bike that has turned 4 years old as of December 1, 2026 in for a full service, and also wants to see how many times each was rented in this period. The catch: the in-service date is stored in the bike table as text like '03/14/2019'. With this format you can neither calculate an age nor sort correctly; you have to convert it to a date first.
Compute the age in whole years. Taking the year difference (2026 minus the in-service year) is a shortcut, but it makes a bike whose anniversary has not arrived yet one year too old. There is such a bike in the fleet, and the shortcut wrongly lets it into the list.
Show: id, kind, in_service (the in-service date as a date), age (whole years) and rentals (the number of rentals in this period). Only bikes aged 4 and over; the oldest bike first.
This step's code cell is for members.Sign up free
A teammate wrote the query below, which gives the daily rental counts for the week of October 28 to November 3 with the date shown as text (Oct 28, 2026). The numbers are right, but the manager objects: "The report starts with Nov 1, and Oct 28 is stuck at the bottom."
Run the query and find out why the rows come in this order. Then fix it: keep the same columns (day as text in Mon DD, YYYY format, and rentals), but make the days come in calendar order.
This step's code cell is for members.Sign up free