0%

Summarize Many Rows · practice

Find the Shortest and Longest Loan Periods

The total 54.0 stretches across all six items, but it hides the inside that total. The shortest usual period is two and a half days; the longest is twenty and a half. Those endpoints help the lending desk see how different the catalog’s policies are.

Open summary.sql. After the sum, add these two in this order:

MIN(loan_days) AS shortest_loan_days,
MAX(loan_days) AS longest_loan_days

Remember the comma after the existing sum. The complete SELECT now contains count, total, shortest, and longest values before FROM items.

Press Run. SQLite should still return exactly one summary row:

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

MIN(loan_days) finds the smallest among the contributing rows. MAX(loan_days) finds the largest. They inspect the same column and same source rows as the sum, but answer different questions about that set.

Look back at the six visible periods: 7, 3, 7, 14, 2.5, and 20.5. Nothing is below 2.5, and nothing is above 20.5. The values between those endpoints still contribute to the count and sum, even though they do not change either endpoint.

These return the endpoint values themselves, not the complete item rows that contain them. The question here is “What are the shortest and longest periods?” It is not “Which items have those periods?” That second question needs more SQL than you need for this summary.

The result-column order makes the answer easier to read: the lower endpoint comes before the upper endpoint. Keep the aliases paired with the matching functions. Swapping only the headings would make a correct value tell the reader the wrong story.

Submit uses periods you have not seen, including repeated endpoints and positive fractional values. A duplicate shortest value does not change the minimum; it only means more than one source row shares it. Let SQLite find the endpoints rather than sorting and limiting rows as you did for a shortlist.

Sorting with LIMIT 1 could find one end in a separate query, but this report needs both ends beside the existing aggregates in the same result row. MIN and MAX state that intention directly.

If the values appear under the wrong headings, trace each expression from function to alias. MIN belongs with shortest_loan_days; MAX belongs with longest_loan_days.

Your one-row report now shows the size of the catalog, its total lending time, and the two values that mark the span between shortest and longest.

Task

Add MIN(loan_days) AS shortest_loan_days and MAX(loan_days) AS longest_loan_days after the existing count and sum.

Run the query, check the endpoint headings and values, and submit it.