Home > Blockchain >  How to find percentage of query result in sql
How to find percentage of query result in sql

Time:02-01

Find the percentage of departments that have a manager. Round the result to 2 decimal places.

I am able to find the result of how many departments has manager, but how will i find its percentage?

SELECT  department_name 
from hr_departments
where manager_id IS NOT NULL;

CodePudding user response:

Since this appears to be a homework question, you want to use an aggregation function to COUNT the number of departments where the manager_id IS NOT NULL and compare that to the COUNT of the total number of rows in the table; then to get the value as a percentage multiply the value by 100 and then ROUND to 2 decimal places.

CodePudding user response:

-- % that have managers = (# that have managers / total # of departments) * 100

SELECT ROUND(((SELECT COUNT(department_name) 
     FROM hr_departments 
     WHERE manager_id IS NULL) / 
     (SELECT COUNT(department_name)
     FROM hr_departments) * 100), 2)
FROM hr_departments
  •  Tags:  
  • Related