CODE WORKSPACE

Count at the right grain

From order_items, return three counts in one row: item_rows (all rows), distinct_orders (unique order_id), and distinct_skus (unique sku).

One row, three columns. COUNT(*) counts item rows; COUNT(DISTINCT order_id) counts the orders those items belong to.

Sample input

order_items15 item rows in all, across 7 distinct orders and 7 distinct skus. order_id repeats across line items.
order_idskuquantityunit_price
1001SKU-A180
1001SKU-B215
1001SKU-C1120
1002SKU-C1120
1002SKU-D120
1004SKU-A180

Expected output

Expected outputOne row of counts. item_rows (15) is larger than distinct_orders (7) because the table is at item grain.
item_rowsdistinct_ordersdistinct_skus
1577

Constraints

order_items is at item grain, so order_id repeats. Use COUNT(*) for rows and COUNT(DISTINCT ...) for unique entities. Alias the columns item_rows, distinct_orders, and distinct_skus.

Expected skills

Understanding table grain and the difference between COUNT(*), COUNT(column), and COUNT(DISTINCT column).

SQL
Loading...

AI evaluation

Run the SQL query to inspect preview rows.