Clean Customer Order Totals with NULL Handling

Quick Overview

Write a SQL query that returns one summary row per customer, including customers with no orders, with a cleaned name, order count, total spend and average paid amount. It tests LEFT JOIN behavior, COALESCE and NULLIF, and how COUNT, SUM and AVG treat NULL and zero values.

Clean Customer Order Totals with NULL Handling

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Return one cleaned summary row per customer, including customers who have never placed an order. Use `COALESCE`, `NULLIF` and a `LEFT JOIN` to clean the name and to make the order totals come out as `0` instead of `NULL` where a customer has nothing to aggregate. ```hint Counting after an outer join A customer with no orders still produces one joined row, with every `orders` column set to `NULL`. Think about which expression you pass to `COUNT` so that row counts as zero orders. ``` ### Input Table(s) `customers` | Column | Type | Meaning | |---|---|---| | customer_id | INT | Customer identifier | | customer_name | VARCHAR(100) | Display name; may be `NULL`, empty, or padded with whitespace | Key: `customer_id` is the primary key. `orders` | Column | Type | Meaning | |---|---|---| | order_id | INT | Order identifier | | customer_id | INT | Customer who placed the order | | amount | DECIMAL(10,2) | Order amount; `NULL`, `0`, or positive | Key: `order_id` is the primary key. ### Output Contract Return these columns in this order: | Column | Rule | |---|---| | customer_id | The customer identifier | | customer_name | The name with leading and trailing whitespace removed, or `'Unknown'` when the name is `NULL`, empty, or only whitespace | | order_count | Number of orders the customer placed (an order with a `NULL` amount still counts); `0` if none | | total_spend | Sum of `amount`; `0` if the customer has no orders or no non-`NULL` amounts; rounded to 2 decimals | | avg_paid_amount | Average of `amount` over the customer's orders whose amount is neither `NULL` nor `0`; `0` if there are no such orders; rounded to 2 decimals | Sort by `total_spend` descending, then by `customer_id` ascending. ### Example `customers` | customer_id | customer_name | |---|---| | 1 | Alice | | 2 | NULL | | 3 | (empty string) | `orders` | order_id | customer_id | amount | |---|---|---| | 10 | 1 | 100.00 | | 11 | 1 | 0.00 | | 12 | 2 | 50.00 | | 13 | 2 | NULL | Expected result: | customer_id | customer_name | order_count | total_spend | avg_paid_amount | |---|---|---|---|---| | 1 | Alice | 2 | 100.00 | 100.00 | | 2 | Unknown | 2 | 50.00 | 50.00 | | 3 | Unknown | 0 | 0.00 | 0.00 | Alice has two orders (100 and 0). Her total is 100.00, and the average ignores the 0, so it is also 100.00. Customer 2 has a `NULL` name, shown as `'Unknown'`, and two orders (50 and `NULL`): both count toward `order_count`, while the total and the average are 50.00. Customer 3 has a blank name and no orders, so every count and amount is 0. ### Constraints and Clarifications - There are 1 to 10,000 customers and 0 to 100,000 orders. - `amount` is `NULL`, `0`, or a positive value up to 1,000,000. - `customer_name` may be `NULL`, empty, or have leading or trailing whitespace. - Every customer appears exactly once in the output. Orders whose `customer_id` matches no customer are not reported. - Ties on `total_spend` are broken by `customer_id`, so the row order is unique.

Overview: Write a SQL query that returns one summary row per customer, including customers with no orders, with a cleaned name, order count, total spend and average paid amount. It tests LEFT JOIN behavior, COALESCE and NULLIF, and how COUNT, SUM and AVG treat NULL and zero values.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Return one cleaned summary row per customer, including customers who have never placed an order. Use COALESCE, NULLIF and a LEFT JOIN to clean the name and to make the order totals come out as 0 instead of NULL where a customer has nothing to aggregate.

Input Table(s)

customers

ColumnTypeMeaning
customer_idINTCustomer identifier
customer_nameVARCHAR(100)Display name; may be NULL, empty, or padded with whitespace

Key: customer_id is the primary key.

orders

ColumnTypeMeaning
order_idINTOrder identifier
customer_idINTCustomer who placed the order
amountDECIMAL(10,2)Order amount; NULL, 0, or positive

Key: order_id is the primary key.

Output Contract

Return these columns in this order:

ColumnRule
customer_idThe customer identifier
customer_nameThe name with leading and trailing whitespace removed, or 'Unknown' when the name is NULL, empty, or only whitespace
order_countNumber of orders the customer placed (an order with a NULL amount still counts); 0 if none
total_spendSum of amount; 0 if the customer has no orders or no non-NULL amounts; rounded to 2 decimals
avg_paid_amountAverage of amount over the customer's orders whose amount is neither NULL nor 0; 0 if there are no such orders; rounded to 2 decimals

Sort by total_spend descending, then by customer_id ascending.

Example

customers

customer_idcustomer_name
1Alice
2NULL
3(empty string)

orders

order_idcustomer_idamount
101100.00
1110.00
12250.00
132NULL

Expected result:

customer_idcustomer_nameorder_counttotal_spendavg_paid_amount
1Alice2100.00100.00
2Unknown250.0050.00
3Unknown00.000.00

Alice has two orders (100 and 0). Her total is 100.00, and the average ignores the 0, so it is also 100.00. Customer 2 has a NULL name, shown as 'Unknown', and two orders (50 and NULL): both count toward order_count, while the total and the average are 50.00. Customer 3 has a blank name and no orders, so every count and amount is 0.

Constraints and Clarifications

  • There are 1 to 10,000 customers and 0 to 100,000 orders.
  • amount is NULL , 0 , or a positive value up to 1,000,000.
  • customer_name may be NULL , empty, or have leading or trailing whitespace.
  • Every customer appears exactly once in the output. Orders whose customer_id matches no customer are not reported.
  • Ties on total_spend are broken by customer_id , so the row order is unique.