0%

Summarize Many Rows · practice

Calculate an Average Loan Period

The shortest and longest periods show the edges of the catalog, but neither describes its center. If all six loan periods were shared evenly, each item would have nine days. An average gives that balancing point.

Open summary.sql. Add a comma after the MAX , then add this fifth result column before FROM items:

AVG(loan_days) AS average_loan_days

Press Run. The one-row report grows by one :

Columns: item_count | total_loan_days | shortest_loan_days | longest_loan_days | average_loan_days
6 | 54.0 | 2.5 | 20.5 | 9.0

AVG(loan_days) asks SQLite to calculate the numeric average of the contributing values. For this catalog, the total is 54.0 and the count is 6, so the result is 9.0.

You can picture the calculation as sharing the total evenly across the counted rows. If three unseen periods are 2.5, 4.25, and 8, their total is 14.75; dividing by three produces a fractional average. SQLite uses the rows in the query rather than a value typed into the report.

The .0 does not turn the result into text. It is SQLite’s numeric answer, displayed through Python without rounding or decoration. A different catalog might produce a value such as 6.25, and that fractional part is meaningful. Do not convert the average to an integer or wrap it in a phrase.

Keep all four earlier expressions in place. Looking at count, total, endpoints, and average together makes it possible to check the result. An average should fall between the minimum and maximum, while total divided by count provides another useful mental check.

Those checks help without becoming extra code. Do not calculate the average in Python after reading the other columns. Keeping AVG(loan_days) in SQL means the database returns one coherent summary from one source set.

Submit uses one catalog whose average looks whole and another whose average is fractional. It compares numeric values with a small tolerance because some decimal fractions cannot be represented exactly inside a computer. You do not need to round them; return SQLite’s result directly.

If Run repeats the total under the new heading, check that the name is AVG, not SUM. If it averages IDs, check the column inside the parentheses. The calculation belongs to loan_days.

You now have five ways to look at the same set of source rows. Count measures its size, sum its total, minimum and maximum its edges, and average its balancing point.

Task

Add AVG(loan_days) AS average_loan_days as the fifth result column in summary.sql. Keep the four existing summary values unchanged.

Run the query and submit SQLite’s numeric average without formatting or rounding it.