{"id":"code-work-pm-sql-left-join-zero-tickets","audience":"product-managers","category":"coding","track":"coding","technique":"context","title":"List Pro accounts with ticket counts, zeros included","free":false,"task":"Write one SQL query listing every account on the 'Pro' plan with its number of support tickets opened in Q3 2026 (July 1 to September 30), and accounts with zero tickets must still appear with 0. Columns: account name and ticket_count, highest count first. One code block, at most 12 lines, then under 25 words outside the code.","materialLabel":"Schema and sample rows (attached to your prompt automatically)","material":"SUPPORT DATABASE - Pinecrest Apps (Postgres)\n\nTable: accounts\n- id (int)\n- name (text)\n- plan (text): 'Free', 'Pro', 'Enterprise'\n- created_at (date)\n\nTable: tickets\n- id (int)\n- account_id (int)\n- opened_at (timestamp)\n- priority (text)\n\nSample accounts:\n1 | Juniper Clinic | Pro | 2025-02-11\n2 | Atlas Freight | Pro | 2026-08-30\n3 | Mira Studio | Free | 2026-01-04\n4 | Okafor Legal | Pro | 2024-11-19\n\nSample tickets:\n501 | 1 | 2026-07-08 10:15 | high\n502 | 1 | 2026-09-30 17:40 | low\n503 | 4 | 2026-06-29 09:00 | low\n504 | 4 | 2026-10-02 08:30 | medium\n\nNotes:\n- Atlas Freight has never filed a ticket.\n- Okafor Legal's only tickets fall just outside Q3.\n- The CS lead wants to see quiet Pro accounts for a check-in campaign.","rules":[{"type":"codeBlocks","count":1},{"type":"maxCodeLines","max":12},{"type":"maxProseWords","max":25}],"mistakes":[{"title":"Not saying zeros matter","why":"The AI writes an inner join, so quiet accounts silently disappear.","fix":"Add: \"Accounts with zero tickets must appear with 0.\"","checks":["r0"]},{"title":"Missing the WHERE trap","why":"Even with a LEFT JOIN, a date filter in WHERE drops the zero-ticket accounts again.","fix":"Add: \"Put the date filter in the ON clause, not WHERE.\"","checks":["r1"]},{"title":"Counting rows, not tickets","why":"COUNT(*) counts the empty joined row, so a quiet account shows 1.","fix":"Add: \"Use COUNT(t.id), highest count first.\"","checks":["r2"]},{"title":"Getting a menu of options","why":"The AI likes to offer two or three versions of the query, so you're left to pick and test them yourself.","fix":"Add: \"Give one query only, in one code block, no alternatives.\"","checks":["codeBlocks","maxCodeLines"]},{"title":"Letting the code sprawl","why":"The AI pads the query with extras nobody asked for, so it grows long and harder to check.","fix":"Add: \"At most 12 lines of code.\"","checks":["maxCodeLines"]},{"title":"Getting a tutorial","why":"By default the AI explains every part of the code at length, which you then have to scroll past.","fix":"Add: \"After the code, one line under 15 words. Nothing else.\"","checks":["maxProseWords"]}],"promptHints":[{"label":"Says zero-ticket accounts must show","patterns":["zero","\\b0\\b","no tickets","left join"]},{"label":"Places the date filter","patterns":["on clause","\\bon\\b.{0,20}date","not (in )?where"]},{"label":"Sets the quarter","patterns":["q3","july","2026-07","september"]},{"label":"Sets the sort","patterns":["desc","highest","most tickets"]},{"label":"Asks for one answer only","patterns":["\\b(one|single|1)\\s+(query|code block|block|version)","only one","no (alternatives|options|other versions)"]}],"hint":"The accounts you most want to see are the ones with no tickets. Say so, and think about where the date filter goes.","rubricLabels":["Uses accounts LEFT JOIN tickets, filtered to plan = 'Pro', so Pro accounts with no tickets still appear","Puts the Q3 date condition on tickets in the JOIN's ON clause (or a subquery), not in WHERE, so Atlas Freight and Okafor Legal still show 0","Counts with COUNT(t.id) or COUNT(tickets.id) (not COUNT(*), which shows 1 for accounts with no tickets), grouped by account and ordered by ticket_count descending"]}