{"id":"data2-sql-customers-multiple-active-subscriptions","audience":"analysts","category":"data","track":"data","technique":"constraints","level":"beginner","title":"Write SQL for customers with two or more active subscriptions","free":false,"task":"Customer success wants to know who holds two or more active subscriptions. Ask for one Postgres query in a single code block with at most 25 words outside it. Count only subscriptions with status 'active', leave out test customers, and sort by the count highest first, then by name.","materialLabel":"Schema and notes (attached to your prompt automatically)","material":"Schema (Postgres) - Tidewater Subscriptions\n\ncustomers(customer_id, name, email, is_test)\nsubscriptions(sub_id, customer_id, plan, status, started_at)\n  status values: 'active', 'paused', 'cancelled'\n\nNotes from the data team:\n- A customer can have many subscriptions, for example one per product line.\n- Paused and cancelled subscriptions are not active and do not count.\n- is_test = true marks internal test customers.\n- Some customers have no subscriptions at all.\n- Last month's report counted every row in subscriptions, so customers with several cancelled plans were listed by mistake.\n- The result is pasted into a spreadsheet, so keep the columns simple.\n- Columns wanted: customer_id, name, active_subs.","rules":[{"type":"codeBlocks","count":1},{"type":"maxProseWords","max":25}],"mistakes":[{"title":"Counting every subscription","why":"The AI counts paused and cancelled plans too.","fix":"Add: \"Count only status = 'active'.\"","checks":["r0"]},{"title":"Filtering the count in WHERE","why":"The AI puts the count test in WHERE, which errors.","fix":"Add: \"GROUP BY customer, then HAVING COUNT(*) >= 2.\"","checks":["r1"]},{"title":"Missing columns or order","why":"The AI returns extra columns in no particular order.","fix":"Add: \"Columns customer_id, name, active_subs; ORDER BY active_subs DESC, name.\"","checks":["r2"]},{"title":"Leaving the format to chance","why":"The AI picks its own length and layout unless told the exact shape.","fix":"Add: \"Put the code in exactly 1 code block. Keep the writing outside the code under 25 words.\"","checks":["codeBlocks","maxProseWords"]}],"promptHints":[{"label":"Says active only","patterns":["active"]},{"label":"Says HAVING","patterns":["having"]},{"label":"Excludes test customers","patterns":["is_test","test"]},{"label":"Names the columns and sort","patterns":["active_subs","order by"]},{"label":"Caps the writing","patterns":["one (short )?sentence","\\d+ words","code block"]}],"hint":"Say which rows count (active only) and where the 2-or-more filter belongs (after grouping).","mission":"Find customers with several subscriptions.","musts":["One Postgres query","Active status only","HAVING for 2 or more"],"rubricLabels":["The query counts only subscriptions with status = 'active' (in a WHERE or COUNT FILTER) and excludes customers with is_test = true","It groups by customer and keeps only those with a count of 2 or more using HAVING COUNT(...) >= 2","It returns customer_id, name and active_subs, ordered by active_subs descending and then name"]}