{"id":"code-work-hr-sql-headcount-as-of","audience":"hr","category":"coding","track":"coding","technique":"context","title":"Count headcount by department on a given date","free":false,"task":"Write one SQL query for headcount by department as of 2026-09-30. Someone counts if hired on or before that date and either not terminated or terminated on or after it, because a person's last day still counts. Leave out contractors (worker_type = 'Contractor'). Return columns named exactly department and headcount, sorted by department name. One code block, at most 14 lines, then under 25 words outside the code.","materialLabel":"Schema and sample rows (attached to your prompt automatically)","material":"HR DATABASE - Fernbrook Dental Group (Postgres)\n\nTable: employees\n- id (int)\n- full_name (text)\n- department (text)\n- worker_type (text): 'Employee', 'Contractor', 'Intern'\n- hire_date (date)\n- term_date (date, NULL if still employed)\n\nSample rows:\n101 | Maya Chen | Front Office | Employee | 2022-03-14 | NULL\n102 | Luis Romero | Finance | Employee | 2019-11-01 | 2026-09-30\n103 | Priya Shah | Front Office | Employee | 2026-10-05 | NULL\n104 | Sam Ortiz | IT | Contractor | 2025-02-01 | NULL\n105 | Jae Park | Clinical | Intern | 2026-06-15 | 2026-08-29\n\nNotes from Gwen:\n- This feeds the Q3 board report.\n- Interns count as headcount this year.\n- The payroll system counts differently and will never match exactly.","rules":[{"type":"codeBlocks","count":1},{"type":"maxCodeLines","max":14},{"type":"maxProseWords","max":25}],"mistakes":[{"title":"Not defining active","why":"The AI uses term_date > the date or ignores NULLs, so people leaving that day drop out.","fix":"Add: \"Count if term_date is NULL or on or after 2026-09-30.\"","checks":["r0"]},{"title":"Forgetting contractors","why":"The AI counts everyone in the table.","fix":"Add: \"Leave out worker_type Contractor.\"","checks":["r1"]},{"title":"Leaving the output open","why":"The AI names columns its own way and sorts by count.","fix":"Add: \"Columns department and headcount, sorted by department.\"","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":"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 12 words. Nothing else.\"","checks":["maxProseWords"]}],"promptHints":[{"label":"Defines active on the date","patterns":["2026-09-30","last day","on or after","term_date"]},{"label":"Leaves out contractors","patterns":["contractor"]},{"label":"Sets the output","patterns":["headcount","department","sort|order"]},{"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":"Say exactly who counts on the date, including people whose last day is that day, and who's left out.","rubricLabels":["Counts people with hire_date <= '2026-09-30' and (term_date IS NULL OR term_date >= '2026-09-30'), so Luis Romero, whose last day is Sept 30, is counted","Excludes contractors (worker_type <> 'Contractor' or NOT IN ('Contractor')) while keeping interns","Returns columns named exactly department and headcount, grouped by department and ordered by department"]}