Skip to main content
SQL โ€ข LESSON 175

Percentage by Group

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

Advancedโฑ 3 Minutesโญ 1880 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