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.
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
One row per line item within an order.
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.
Related
COUNT, SUM, AVG, MIN, MAX, GROUP BY, and HAVING.
COUNT, SUM, AVG, GROUP BY, and HAVING.
Practice aggregate reports, HAVING, and answer shape.
Avoid truncating fractional results.
Group or aggregate every selected column.
Practice buckets, business labels, and conditional counts.