Scenario: Brightlane's data analyst ran EXPLAIN on a fulfillment health report and saw the planner estimating only 5 rows for orders whose status is 'shipped' — a number the analyst suspects is wildly off because table statistics have not been refreshed since a recent data import.
Task: Write a query to return the actual count of orders whose status is 'shipped', so the analyst can compare the real number against the planner's estimate.
Assumptions:
- The
orderstable holds one row per placed order, with the order's outcome stored instatus. - A shipped order has
statusequal to'shipped'.
Output:
- One row, holding the shipped-order count.
- Columns in this order:
shipped_order_count.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
COUNT(*) AS shipped_order_count
FROM
orders
WHERE
status = 'shipped' The shape
The planner said 5; the table actually has 17 shipped orders, and COUNT(*) with the same restriction is the most direct way to measure that gap. The query is a one-line truth check against the row-estimate the analyst saw in the plan.
Clause by clause
SELECT COUNT(*) AS shipped_order_countreturns one row with the total count, labeled so the result reads as the diagnostic number rather than an unnamed column.FROM ordersreads the order records — the same table theEXPLAINplan was built against.WHERE status = 'shipped'applies the same restriction the planner was estimating selectivity for. The actual surviving row count is what the planner was trying to predict.
Why this and not a per-status breakdown
A per-status GROUP BY would also surface the shipped count, alongside every other status. That's a useful query for a different diagnostic — checking distribution skew — but it overshoots this question. The analyst is comparing one specific estimate (5) against one specific reality, so a single-row answer is the cleanest comparison surface. A grouped result would require the analyst to scan the output for the right row before doing the same subtraction.
You practiced verifying a planner row estimate by computing the real count — the gap between estimated and actual rows is the headline diagnostic in EXPLAIN ANALYZE output.