Back to Blog

Why Your AI's Average Order Is Only the Latest Ones

Asked for the average order and got $42. That is the average of the latest 100 orders. The table's average is $68. The AI put LIMIT under the average, so older orders never entered the number.

You ask: "What's the average order?" The AI says $42. The average of every order is $68. The latest 100 orders really do average $42. Wholesale orders from last year are larger, and they never got into the query.

SELECT AVG(amount)
FROM (
  SELECT amount
  FROM orders
  ORDER BY created_at DESC
  LIMIT 100
) recent

AVG is correct for the rows it can see. LIMIT 100 means it can see the latest page only. The answer is the average of that page.

A page is not the table

The latest orders are a reasonable sample only when you say they are a sample. Nothing in the number $42 says "last 100." It reads as the average order.

-- the average you asked for
SELECT AVG(amount) FROM orders

-- the average of the latest 100, labeled as such
SELECT AVG(amount) FROM (
  SELECT amount FROM orders
  ORDER BY created_at DESC
  LIMIT 100
) recent

Same shape for a filtered question. "Average order in December" is AVG(amount) with a December filter and no LIMIT. "Average of the last 100 December orders" is the limited form, and the sentence has to include the 100.

This is not a top 10 that changes every time you ask. There, the missing ORDER BY makes the set unstable. Here the order is fixed, so the $42 comes back every time and still leaves out the rest of the table. It is also what happens when the tool only returns a few hundred rows and the model averages the rows in the reply instead of asking SQL for AVG.

How to catch it

SELECT
  (SELECT AVG(amount) FROM orders) AS all_orders,
  (SELECT AVG(amount) FROM (
     SELECT amount FROM orders ORDER BY created_at DESC LIMIT 100
   ) recent) AS latest_100

If the AI's figure matches latest_100 and not all_orders, it averaged a page. Use the same pair inside whatever date filter you actually wanted. A gap of a few dollars is enough. The page is skewed toward whatever you have been selling lately.

The prompt that heads this off

When I ask for an average, a total, or a count, compute it in SQL over
every row that matches the filter. Do not LIMIT the rows and then average
what came back. If I ask about recent orders, say how many or which dates
you included.

The short version

AVG of ORDER BY created_at DESC LIMIT 100 is the average of the latest 100 orders. The average order has no LIMIT. If you mean the recent ones, say so in the answer.


Read-only MCP gateway: mcpserver.design.

Related reading: