▸_sqlgym

46. Paid vs Unpaid Invoices

MediumAggregationconditional aggregationCASEFILTERGROUP BY

Conditional aggregation counts or sums only the rows that match a condition, without filtering the whole query. There are two idioms: COUNT(CASE WHEN cond THEN 1 END) and COUNT(*) FILTER (WHERE cond) — SQLite (3.45) supports both and they give identical results.

Task: for each client, count how many invoices are paid and how many are unpaid, and sum the amount of the paid ones.

Return columns client, paid_count, unpaid_count, paid_amount, where paid means status = 'paid'. Treat a missing (NULL) paid_amount sum as 0. Order by client ascending.

Tables
Loading schema…
Expected outputrows must come back in this order
Computing expected output…

This is the result for the example data above. On Submit your query is graded against this example plus 4 hidden edge cases 5test cases in all. A sloppy query that only fits the example won't pass.

SQL
⌘/Ctrl+↵ run · ⇧⌘/Ctrl+↵ submit
Run a query to see its output here.