Skip to main content
SQL • LESSON 175

Percentage by Group

How can we calculate what percentage of the total workforce belongs to each department?

Advanced3 Minutes1880 XP
🤔 THE QUESTION

How can we calculate what percentage of the total workforce belongs to each department?

💡 WHAT IS IT?

Dividing group counts by the global window sum (SUM(COUNT(*)) OVER ()) computes percentage shares per group.

🎯 WHAT IS IT USED FOR?

Workforce allocation share, departmental percentage distribution, portfolio weightings.

💻 EXAMPLE
SELECT
department_id,
COUNT(*) AS employee_count,
100.0 * COUNT(*) / SUM(COUNT(*)) OVER () AS percentage_of_employees
FROM employees
GROUP BY department_id;

🎯 Mission Objectives

Practice typing production-grade SQL code for Percentage by Group.

  • Percentage share calculation
  • SUM(COUNT(*)) OVER ()
  • Window on aggregate
  • Distribution analytics