▸_sqlgym

92. Count Items in a CSV Field

MediumString Manipulationlengthreplace

SQLite has no array type, but you can count how many times a character appears with the length trick: length(s) - length(replace(s, ch, '')) is the number of occurrences of ch. For a comma-separated list the item count is the comma count plus one.

Task: each tags value is a non-empty comma-separated list. Return id, tags, and item_count — the number of items, computed as length(tags) - length(replace(tags, ',', '')) + 1. Order by id.

Output columns, in order: id, tags, item_count.

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.