SQL Practice: Review and Back to the Practical Question (Bike Rides)
Review the whole toolbox of the SQL guide on the bike-share database, then answer the practical question: when does a bike-share system need capacity?
Last updated:
Before you start
This page is practice for the two parts of the practical SQL guide:
part 1 and part 2. It uses only what they taught, on the bike-share database:
the table daily_ridership and, for the last review question, the table review_queue.
First come five review questions; then the question that started the guide, when does a bike-share system need capacity, with five questions that answer it.
Each question has a check, and review question 5 prepares the state it needs by itself.
In this guide
ContentsReview practice
Review practice
Try each question before opening its answer. Each item includes one possible answer and two plausible mistakes, each shown separately with the reasoning that could lead to it and a short explanation.
1. Which five dates had the most rentals?
Show one possible answer
SELECT observation_date, total_rides
FROM daily_ridership
ORDER BY total_rides DESC
LIMIT 5;
Possible mistake: assuming LIMIT selects the largest values
The mistaken reasoning: perhaps LIMIT 5 means “the five largest.”
In fact, it only limits the number of rows; it does not select them by size.
SELECT observation_date, total_rides
FROM daily_ridership
LIMIT 5;
Possible mistake: sorting in ascending order
The mistaken reasoning: perhaps ascending order is the usual way to sort.
But ASC sorts from smallest to largest, so the first five rows are the quietest days.
SELECT observation_date, total_rides
FROM daily_ridership
ORDER BY total_rides ASC
LIMIT 5;
2. How many days had weather text containing rain?
Show one possible answer
SELECT COUNT(*) AS rainy_days
FROM daily_ridership
WHERE weather LIKE '%rain%';
Possible mistake: searching for rain only at the start of the text
The mistaken reasoning: perhaps the word only needs to appear at the start of the pattern, as when searching for a word in a description.
But rain% matches only values that start with the sequence rain.
SELECT COUNT(*) AS rainy_days
FROM daily_ridership
WHERE weather LIKE 'rain%';
Possible mistake: using a wildcard with =
The mistaken reasoning: perhaps % works as a wildcard with = too.
But = compares the literal text %rain%; only LIKE treats % as a wildcard.
SELECT COUNT(*) AS rainy_days
FROM daily_ridership
WHERE weather = '%rain%';
3. What was the average number of casual rentals on working and nonworking days?
To compare, show the day type (working day or not) and the average number of casual rentals, rounded to a whole number.
Show one possible answer
SELECT working_day,
ROUND(AVG(casual_rides), 0) AS average_casual
FROM daily_ridership
GROUP BY working_day;
Possible mistake: calculating one average without grouping
The mistaken reasoning: perhaps one average is enough because the question asks to compare day types.
Without GROUP BY working_day, the query returns a single average for the whole dataset, not a separate average for each day type.
SELECT ROUND(AVG(casual_rides), 0) AS average_casual
FROM daily_ridership;
Possible mistake: filtering to one group instead of comparing groups
The mistaken reasoning: perhaps filtering to working days produces both averages.
This condition keeps only working days, so it cannot calculate the average for the other days.
SELECT working_day,
ROUND(AVG(casual_rides), 0) AS average_casual
FROM daily_ridership
WHERE working_day = 1
GROUP BY working_day;
4. Which seasons had an average above 4,000 rentals per day?
Show the season and the average number of daily rentals, rounded to a whole number.
The 4,000 threshold is for this exercise; it is not an official service standard.
Show one possible answer
SELECT season, ROUND(AVG(total_rides), 0) AS average_rentals
FROM daily_ridership
GROUP BY season
HAVING AVG(total_rides) > 4000
ORDER BY average_rentals DESC;
Possible mistake: filtering rows by the threshold before averaging
The mistaken reasoning: perhaps the right first step is to keep days with more than 4,000 rentals, then calculate each season’s average.
That computes the average from only the days above the threshold, not from every day in the season.
SELECT season, ROUND(AVG(total_rides), 0) AS average_rentals
FROM daily_ridership
WHERE total_rides > 4000
GROUP BY season;
Possible mistake: checking an average in WHERE
The mistaken reasoning: perhaps WHERE can check an average the way it checks a column value.
But the group average has not been calculated at this stage, so the query fails.
Conditions on averages belong in HAVING.
SELECT season, AVG(total_rides) AS average_rentals
FROM daily_ridership
WHERE AVG(total_rides) > 4000
GROUP BY season;
Why does this condition belong in HAVING?
WHERE filters rows before the calculation; HAVING filters groups after their averages have been calculated.
Moving the condition changes the question.
5. Copy the five busiest winter observations into review_queue.
Show one possible answer
INSERT INTO review_queue
(record_id, observation_date, season, working_day, weather, total_rides)
SELECT record_id, observation_date, season, working_day, weather, total_rides
FROM daily_ridership
WHERE season = 'Winter'
ORDER BY total_rides DESC
LIMIT 5;
Possible mistake: copying rows back into the source table
The mistaken reasoning: both tables contain rental data, so perhaps the copies belong back in daily_ridership.
But that is the source table, and the identifiers already exist there.
The practice is meant to add copies to the review table.
INSERT INTO daily_ridership
(record_id, observation_date, season, year, month, holiday, weekday,
working_day, weather, temperature_normalized,
apparent_temperature_normalized,
humidity_normalized, wind_speed_normalized, casual_rides,
registered_rides, total_rides)
SELECT record_id, observation_date, season, year, month, holiday, weekday,
working_day, weather, temperature_normalized,
apparent_temperature_normalized,
humidity_normalized, wind_speed_normalized, casual_rides,
registered_rides, total_rides
FROM daily_ridership
WHERE season = 'Winter'
ORDER BY total_rides DESC
LIMIT 5;
Possible mistake: copying every winter day instead of the five busiest
The mistaken reasoning: perhaps selecting a season already narrows the result to its five busiest observations.
In fact, this condition selects every winter day; without sorting and limiting, all of those rows will be copied.
INSERT INTO review_queue
(record_id, observation_date, season, working_day, weather, total_rides)
SELECT record_id, observation_date, season, working_day, weather, total_rides
FROM daily_ridership
WHERE season = 'Winter';
Back to the practical question
The guide began with a practical question: when does a bike-share operator need to prepare for more demand?
These questions use the commands from this guide to answer it.
Try each question before opening its answer. Each item includes one possible answer, with the result and what it means, and two plausible mistakes to learn from.
1. In which months is demand highest? Compare the average daily rentals in each month.
To compare, show the month number and the average number of daily rentals in that month, rounded to a whole number.
Show one possible answer
SELECT month,
ROUND(AVG(total_rides), 0) AS average_rentals
FROM daily_ridership
GROUP BY month
ORDER BY month;
In this dataset, the daily average rises from about 2,176 rentals in January to about 5,772 in June.
Every month from May to October averages above 5,100, and the average falls to about 3,404 in December.
Demand is highest in summer and early fall.
Possible mistake: listing every day in month order
The mistaken reasoning: perhaps sorting the days by month is enough to compare the months.
But the query returns all 731 daily rows, not one summary per month, so the comparison is left to the reader.
SELECT month, total_rides
FROM daily_ridership
ORDER BY month;
Possible mistake: adding up the rentals
The mistaken reasoning: perhaps “demand” means the total number of rentals.
But months have different numbers of days, for example 57 days of February against 62 of August, so a total favors longer months.
Here August has the largest total even though June has the highest daily average.
The average per day compares months fairly.
SELECT month,
SUM(total_rides) AS rentals
FROM daily_ridership
GROUP BY month
ORDER BY rentals DESC;
2. How does demand look in each kind of weather? Compare the average daily rentals for each weather label, with the number of days behind each average.
To compare, show the weather label, the number of days and the average number of daily rentals, rounded to a whole number.
Show one possible answer
SELECT weather,
COUNT(*) AS days,
ROUND(AVG(total_rides), 0) AS average_rentals
FROM daily_ridership
GROUP BY weather
ORDER BY average_rentals DESC;
Clear or partly cloudy days average about 4,877 rentals, mist or cloudy days about 4,036, and the 21 days of light rain or snow about 1,803, roughly 37% of a clear day.
Only 21 days fall in the rain or snow group, spread across seasons, so treat the size of that gap with caution.
The seasons are also mixed (10 of the 21 days are in Fall), but within every season the rain or snow days average less than the clear days, for example 2,752 against 5,878 in Summer, so the gap is not only a result of mixing seasons.
The direction is clear: days of rain or snow are much quieter.
Possible mistake: looking only at rainy days
The mistaken reasoning: perhaps to see the effect of rain it is enough to look at rainy days.
The query returns one number, about 1,803, with nothing to compare it to.
Without the average for other weather, we cannot tell whether that number is low.
SELECT ROUND(AVG(total_rides), 0) AS average_rentals
FROM daily_ridership
WHERE weather LIKE '%rain%';
Possible mistake: counting days instead of averaging rentals
The mistaken reasoning: perhaps the weather label with the most days is the one with the highest demand.
But the number of days says how common a type of weather is, not how many rentals it brings.
SELECT weather,
COUNT(*) AS days
FROM daily_ridership
GROUP BY weather
ORDER BY days DESC;
3. Did demand change between the two years? Compare the average daily rentals in 2011 and 2012.
To compare, show the year, the number of days and the average number of daily rentals, rounded to a whole number.
Show one possible answer
SELECT year,
COUNT(*) AS days,
ROUND(AVG(total_rides), 0) AS average_rentals
FROM daily_ridership
GROUP BY year
ORDER BY year;
The daily average was about 3,406 in 2011 and about 5,600 in 2012, around 64% higher, which may indicate that the system grew.
If so, a comparison that averages both years together partly measures growth as well as the season.
In the season example in part 1 each season has almost the same number of days from each year, so growth tilts that comparison very little.
Possible mistake: selecting the year without grouping
The mistaken reasoning: perhaps selecting year next to the average is enough to get one average for each year.
Without GROUP BY year, the query returns a single average for the whole dataset, labeled with one arbitrary year.
SELECT year,
ROUND(AVG(total_rides), 0) AS average_rentals
FROM daily_ridership;
Possible mistake: comparing the busiest day of each year
The mistaken reasoning: perhaps the busiest day of a year describes its demand.
But one extreme day is not a typical day: the two maximums, 6,043 and 8,714, say little about how many rentals a normal day brought.
SELECT year,
MAX(total_rides) AS busiest_day
FROM daily_ridership
GROUP BY year;
4. Does the seasonal pattern repeat in each year? Compare the average daily rentals for each season, separately for 2011 and 2012.
To compare, show the year, the season and the average number of daily rentals, rounded to a whole number.
To compare seasons within each year, group by two columns. ORDER BY can also list more than one column:
rows are sorted by the first column, and rows with the same value are sorted by the second.
Show one possible answer
SELECT year,
season,
ROUND(AVG(total_rides), 0) AS average_rentals
FROM daily_ridership
GROUP BY year, season
ORDER BY year, average_rentals DESC;
In both years, Summer is the busiest season and Winter the quietest:
4,464 and 1,667 in 2011, and 6,824 and 3,531 in 2012. The seasonal pattern repeats, and the growth appears as a higher level in every season, with Winter 2012 more than double Winter 2011.
Possible mistake: grouping only by season
The mistaken reasoning: perhaps because year is in the SELECT list, it is already part of the grouping.
But the groups are only the four seasons, so each average mixes both years and the year value shown is one arbitrary year.
SELECT year,
season,
ROUND(AVG(total_rides), 0) AS average_rentals
FROM daily_ridership
GROUP BY season;
Possible mistake: sorting by the average only
The mistaken reasoning: perhaps sorting by the average shows the busiest seasons first.
But the rows of the two years are then interleaved, so it is hard to compare the seasons within each year.
SELECT year,
season,
ROUND(AVG(total_rides), 0) AS average_rentals
FROM daily_ridership
GROUP BY year, season
ORDER BY average_rentals DESC;
5. Which month had the highest average rentals in 2012?
Show the month number and the average number of daily rentals, rounded to a whole number, for that month only.
Show one possible answer
SELECT month,
ROUND(AVG(total_rides), 0) AS average_rentals
FROM daily_ridership
WHERE year = 2012
GROUP BY month
ORDER BY average_rentals DESC
LIMIT 1;
The result is month 9 (September), with an average of about 7,286 rentals per day.
Possible mistake: forgetting to restrict the year
The mistaken reasoning: perhaps grouping by month is enough to find the busiest month of 2012.
Without WHERE year = 2012, the query averages both years for each month, so it returns June (about 5,772) and not September 2012.
SELECT month,
ROUND(AVG(total_rides), 0) AS average_rentals
FROM daily_ridership
GROUP BY month
ORDER BY average_rentals DESC
LIMIT 1;
Possible mistake: sorting by the month number
The mistaken reasoning: perhaps ORDER BY month DESC LIMIT 1 returns the last month in the ordering that matters.
But it sorts by the month number, not by demand, so it returns month 12, December, which is not the busiest.
SELECT month,
ROUND(AVG(total_rides), 0) AS average_rentals
FROM daily_ridership
WHERE year = 2012
GROUP BY month
ORDER BY month DESC
LIMIT 1;
What the data suggest
- Demand is highest from May to October, and especially from June to September, so that is when an operator would most need capacity.
- Clear days are busy and days of rain or snow are much quieter, which suggests that the weather forecast is useful for planning service from day to day.
- Day type changes who rides: casual rentals are higher on nonworking days and registered rentals on working days (see Review practice), so leisure and commuting demand need different preparation.
- The daily average in 2012 is about 64% higher than in 2011, which may indicate that the system grew, so numbers from an earlier year should be used with care.
What the data cannot tell us
These are averages of daily totals for one system in two years.
The table has no hours or station locations, so it cannot say at what time of day or in which neighborhood more bikes are needed.
It also cannot show why demand changed, and two years are too few to separate a seasonal pattern from growth with confidence.
Use these results to decide which questions to ask next, not as a forecast for another system or for today.
Continue learning
Continue with the Python part of the unit: Python for databases, part 1.
For more about structured data, tables, and databases, return to the opening-unit section on structured data and databases.