Retail · SQL
Profiling order status distribution to support a revenue discrepancy investigation
Independent grading: 100/100(verify, opens in a new tab)
Problem
Nura Ibrahim, CFO at Meridian Retail (simulated retailer), flagged a revenue discrepancy and needed an orientation step before deeper investigation could begin. The specific question was: what distinct order statuses exist in the orders table, and how many orders carry each one? This gave the investigation a factual baseline about how orders were classified.
What I did
The query targets retail.orders directly and uses COUNT(*) as order_count, which is appropriate because every row in that table represents exactly one order. Grouping by status at that grain produces one row per distinct status value with no double-counting. Adding ORDER BY order_count DESC meant the output was immediately readable, with the most frequent status at the top rather than in arbitrary order.
Finding
The table contains 7 distinct order statuses: delivered, shipped, cancelled, confirmed, returned, pending, and refunded, with delivered being the most common at 14,061 orders.
Outcome
Nura received a complete, ordered snapshot of how orders are classified, giving the revenue investigation a reliable starting point for scoping further queries.
SELECT status, count(*) AS order_count FROM retail.orders GROUP BY status ORDER BY order_count DESC
This query groups every row in retail.orders by its status value and counts occurrences, then sorts the result by frequency so the most common statuses appear first.
Next step
A logical next step would be to filter this breakdown by a specific date window, such as October 2023, to see whether the status distribution in that month differs from the overall pattern.
- GROUP BY aggregation in SQL
- COUNT(*) with aliasing in SQL
- PostgreSQL table scoping with schema-qualified names