AlgoMaster Logo

Exercise: NULL Handling

Last Updated: May 10, 2026

1 min read

For this exercise, we will work with below data:

SQL

Exercise 1

Easy

Find all active subscriptions (where end_date is NULL). Show subscription_iduser_idplan, and start_date, ordered by subscription_id.

SQL

Expected Query

Expected Output

Exercise 2

Easy

Show each user's username and timezone, replacing NULL timezones with the literal string 'Not set'. Order by user_id.

SQL

Expected Query

Expected Output

Exercise 3

Easy

Calculate the net amount for each payment as amount - refund_amount. Treat missing refunds as zero so the result is never NULL. Show payment_idamountrefund_amount, and net_amount, ordered by payment_id.

SQL

Expected Query

Expected Output

Exercise 4

Medium

Count three things in a single query against the streams table: the total number of stream rows, the number of streams with a non-NULL country, and the number of distinct non-NULL countries.

SQL

Expected Query

Expected Output

Exercise 5

Hard

For each user who has at least one payment, return user_idnet_amount (SUM(amount) minus the total refund where missing refunds are treated as 0), and refund_ratio (total refunds divided by total amount, guarded against division by zero, with NULL ratios shown as 0). Round refund_ratio to 2 decimals. Order by user_id.

SQL

Expected Query

Expected Output