Harmanjit Singh
Harmanjit Singh

Reputation: 3

How to find percentage of query result in sql

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;

Upvotes: 0

Views: 89

Answers (2)

Ricardo Francois
Ricardo Francois

Reputation: 792

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

SELECT ROUND(((SELECT COUNT(department_name) 
     FROM hr_departments 
     WHERE manager_id IS NOT NULL) / 
     (SELECT COUNT(department_name)
     FROM hr_departments) * 100), 2)
FROM hr_departments

Upvotes: 0

MT0
MT0

Reputation: 168361

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.

Upvotes: 1

Related Questions