N013-H2 Tier 2 · Core SQL · hard ecommerce · Brightlane

Return the exact average in a single column named `avg_quantity`

Part of Aggregate Functions (COUNT, SUM, AVG, MIN, MAX) in SQL

The problem

Brightlane's warehouse team wants the average quantity per order line item — precise enough to use in a capacity model, not rounded to a whole number.

Write a query to return the exact average in a single column named avg_quantity.

Assumptions:

  • The order_items table contains one row per product per order.
  • quantity is stored as an integer column.
  • The result must preserve the fractional portion — a value like 2.4737... is the answer, not 2.

Output:

  • A single row with one column, avg_quantity, expressed as a decimal value.
Schema · ecommerce 5 tables
categories
id integer
name text
parent_id? integer
products
id integer
name text
category_id integer
price numeric
stock_qty integer
attributes? jsonb
order_items
id integer
order_id integer
product_id integer
quantity integer
unit_price numeric
customers
id integer
name text
email text
city? text
country text
created_at timestamptz
is_active boolean
orders
id integer
customer_id integer
ordered_at timestamptz
status text
total_amount numeric

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
SELECT
  AVG(quantity) AS avg_quantity
FROM
  order_items

The shape

AVG(quantity) returns 1.05 — a decimal with a fractional part — even though quantity is stored as an integer column. PostgreSQL's AVG automatically widens integer inputs to numeric so the fractional portion of the mean survives the operation, which is exactly what the warehouse capacity model needs.

Clause by clause

  • FROM order_items is the source set: one row per (order, product) line, with quantity as an integer count of units on each line.
  • AVG(quantity) walks the column, divides the sum by the count, and returns the mean. The load-bearing behavior here is the type promotion: PostgreSQL knows that the average of integers is rarely a whole number, so it returns a numeric rather than an integer. The fractional part survives the calculation by default.
  • AS avg_quantity labels the result. The warehouse team can drop the column straight into a capacity model without renaming it.

Why this and not SUM(quantity) / COUNT(*)

AVG(quantity) and SUM(quantity) / COUNT(*) are mathematically the same operation — total divided by count. But they have very different behavior in PostgreSQL when the input is an integer column.

AVG returns numeric automatically. SUM(quantity) / COUNT(*) runs as integer-divided-by-integer, because SUM over an integer column and COUNT(*) both return integers. PostgreSQL's integer division drops the fractional part of the result entirely. For this data set, the division would return 1 instead of 1.05. The capacity model would be working with the wrong number, and there is no error to flag the loss.

To get the same behavior as AVG from a manual division, the cast has to be explicit — turning at least one operand into numeric before the division happens. AVG does that automatically; the manual division leaves it to the author.

The trap

The trap is reaching for the textbook formula (SUM / COUNT) instead of the built-in aggregate, and getting integer truncation as a silent side effect. The numbers look reasonable — 1 is plausible as an average quantity per line — but they're wrong by the fractional portion. For a capacity model that scales by tens of thousands of line items, a lost 0.05 per line compounds into a meaningful underestimate of total throughput.

The rule: when averaging an integer column, prefer AVG to a hand-rolled division. The function exists precisely to make the type widening automatic. Reach for it.

You practiced relying on AVG's automatic type-widening. Even when the input column is integer, AVG returns numeric to preserve the fractional part — unlike division (SUM(quantity) / COUNT(*)), which would truncate without an explicit cast.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.