Skip to content
/ Chapter 4 · Aggregations and Grouping
Lesson 4.2 · garden_shop
Lesson 4.2

SQL SUM, AVG, MIN, and MAX

SUM and AVG turn numeric columns into totals and averages; MIN and MAX find the extremes.

SUM adds values. AVG averages them. They only make sense for numeric expressions.

MIN and MAX return the smallest and largest value. They work on text and dates as well as numbers: max(order_date) is the most recent order.

Aggregates can use calculated expressions too. For each order item, the line total is quantity * unit_price, and SQL can sum or average that expression directly.

Check the unit before averaging

An average is only useful when every row represents the same kind of thing. Here, each row is one order line, so avg(quantity * unit_price) means average line total. It doesn't mean average order total unless you group line items by order first.

Pattern
select sum(number_column) as total_value,
       avg(number_column) as average_value,
       min(number_column) as smallest_value,
       max(number_column) as largest_value
from table_name
Schema · Garden Shop Table · order_items 5 columns · 48 rows
Table · order_items

One row per line item within an order.

5 columns · 48 rows
order_item_id int order_id int product_id int quantity int unit_price decimal
Your task

Return one summary row with total units sold, gross sales, the average line total, and the smallest and largest line total across all order items. Name the columns units_sold, gross_sales, average_line_total, smallest_line_total, and largest_line_total.

·

Run your query to inspect the result.

Expected answer
  • Columns: units_sold, gross_sales, average_line_total, smallest_line_total, largest_line_total.
  • Rows: 1 summary row.
  • Expected values: 87 units sold, 1129 gross sales, and line totals from 9 to 64.