Library › Coding AI › HR and recruiting
Count headcount by department on a given date
Practice: Giving the right context. About 5 minutes.
About this exercise
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.
Common mistakes when you ask AI for this
- Not defining active. The AI uses term_date > the date or ignores NULLs, so people leaving that day drop out. Try: Add: "Count if term_date is NULL or on or after 2026-09-30."
- Forgetting contractors. The AI counts everyone in the table. Try: Add: "Leave out worker_type Contractor."
- Leaving the output open. The AI names columns its own way and sorts by count. Try: Add: "Columns department and headcount, sorted by department."
- Getting a menu of options. The AI likes to offer two or three versions of the query, so you're left to pick and test them yourself. Try: Add: "Give one query only, in one code block, no alternatives."
- Getting a tutorial. By default the AI explains every part of the code at length, which you then have to scroll past. Try: Add: "After the code, one line under 12 words. Nothing else."
What a strong prompt includes
- Defines active on the date
- Leaves out contractors
- Sets the output
- Asks for one answer only
Related exercises
- Add a cron line that matches the others in a crontab (Coding AI · Product managers)
- Add dark mode by overriding your existing CSS variables (Coding AI · Software developers)
- Add input checks to your number guessing game (Coding AI · High school students)
- Add input validation to a Java method with exact messages (Coding AI · Software developers)
- Add labels to a signup form without rewriting the page (Coding AI · High school students)