Brightlane's operations report shows every order alongside the platform-wide order count for context.
Write a query to return the ID and status of every order, plus the total number of orders on each row.
Assumptions:
- The
orderstable has one row per order with anidand astatus. - The total order count is the number of records in
orders. The same value should appear on every output row.
Output:
- One row per order, with columns
id,status, andtotal_orders.
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
id,
status,
COUNT(*) OVER () AS total_orders
FROM
orders The shape
COUNT(*) OVER () counts every row in the orders table and writes that single number onto every output row, next to each order's own id and status. The empty parentheses after OVER make the window the entire result.
Clause by clause
SELECT id, statusreturns each order's identifier and status, one row per order.- The window column is:
COUNT(*) OVER () AS total_ordersCOUNT(*) counts the rows in its window. OVER () with empty parentheses defines that window as every row in the result, so the count is the total number of records in orders. The same value, 200 for this dataset, appears in total_orders on every row.
FROM ordersreads every order. No filter, because the report counts the whole table.
Why this and not a plain COUNT(*) with GROUP BY
A standalone SELECT COUNT(*) FROM orders returns a single row holding the count. The operations report needs the count attached to every order, not in place of them. The window form keeps every row in the output and adds the aggregate as a side-by-side column, which is exactly the shape the report consumes.
You practiced COUNT(*) OVER () — compute a single record count across the entire result and replicate it onto every row.