CODE WORKSPACE

Bucket missing countries

For active (not soft-deleted) customers, return customer_id and country_bucket, where country_bucket is the country or the text 'unknown' when the country is missing. Order by customer_id.

Filter deleted_at IS NULL, then wrap country in COALESCE(country, 'unknown') and alias it country_bucket.

Sample input

customersAll 6 customers. Customer 2 has a missing country; customer 5 is soft-deleted (deleted_at is set).
customer_idcountrydeleted_at
1USnull
2nullnull
3GBnull
4INnull
5US2026-01-20
6GBnull

Expected output

Expected outputCustomer 2's NULL country becomes 'unknown'; customer 5 is excluded because deleted_at is not NULL.
customer_idcountry_bucket
1US
2unknown
3GB
4IN
6GB

Constraints

Active means deleted_at IS NULL. Replace a missing country with COALESCE(country, 'unknown'). Do not use country = NULL.

Expected skills

Testing for missing values with IS NULL and substituting defaults with COALESCE.

SQL
Loading...

AI evaluation

Run the SQL query to inspect preview rows.