Count orders and total their value

Return one row with total_orders (the number of orders, including rows with a NULL amount) and total_amount (the sum of non-NULL amounts). Return 0 for total_amount when there are no amounts to sum. Use these exact column aliases.

Difficulty: beginner

Show hint

COUNT(*) counts rows. SUM ignores NULL; COALESCE can replace a NULL aggregate with 0.

Tables and sample data

Each test starts with fresh tables. Result rows may be returned in any order. Column names and column order must match.

Sample data: orders
order_id (integer)customer_id (integer)amount (numeric)status (text)region (text)
1125.5paidEast
2240pendingWest
33NULLpaidNorth
Expected sample result
total_orderstotal_amount
365.5

Your solution

SQL · PostgreSQL

Results

PostgreSQL loads when you Run or Submit. Write one read-only SELECT or WITH query. Your query runs in this browser.

Back to Lesson