SQL for HR: Attrition Analysis from an HR Database
<p>Anvaya Home Health sends nurses, physiotherapists and care attendants into patients' homes across four cities, and every resignation means a family starting again with a stranger. Its founder has a board meeting in two weeks and wants the year's attrition: how many people left, where, when and why. The HR slide and the supervisors tell different stories. Kavya Menon, the HR analyst, decides to build every figure as SQL on the HR system's export, so the board gets numbers that tie together and can be re-run every quarter.</p><p>Across five lessons you build them with her, in the SQL lab, on a read-only copy of the HR database: headcount on a date that ignores the misleading status column, exits dated by the last working day rather than the resignation, withdrawn resignations and interns kept out, an attrition rate over the average headcount that keeps its decimals, department attrition with LEFT JOINs that keep the team that lost nobody, tenure bands with DATEDIFF and exact edges, messy exit reasons cleaned with LOWER and TRIM, a self-join to find managers who lost three or more, a running total and a RANK of regrettable exits with window functions, and a headcount bridge built with UNION ALL that ties to the headcount. Every lesson you write the query yourself first, then hand the same job to your AI (ChatGPT, Claude, Copilot or Gemini) and learn exactly where it goes wrong. In the capstone your AI writes the board's queries from one brief, and you prove every number before it reaches the board.</p>
Tools: SQL (MySQL), NOT EXISTS and LEFT JOIN, CTEs and conditional aggregation, Dates with DATEDIFF and DATE_FORMAT, Self-joins, Window functions (SUM OVER, RANK), HR analytics: headcount and attrition, AI assistants for SQL