▸_sqlgym

95. Avoid Divide-by-Zero with NULLIF

EasyNULL HandlingNULLIFCOALESCE

Dividing by zero would normally be a problem, but in SQLite division by NULL yields NULL (no error). The idiom x / NULLIF(divisor, 0) converts a zero divisor into NULL so the whole expression becomes NULL instead of blowing up — then you can wrap it in COALESCE to show a default.

Task: for each campaign return id, clicks, impressions, and ctr — the click-through rate clicks / impressions as a ratio rounded to 3 decimals. When impressions is 0, ctr must be 0.0 (not an error and not NULL). Order by id.

Output columns, in order: id, clicks, impressions, ctr.

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.