Department has Maximum Staff - sql

Write a query to display the name of the department that has the maximum staff count order by department name.
This is the schema of my problem:
I tried this:
select d.department_name
from department d
inner join staff s on d.department_id=s.department_id
group by d.department_name
having count(s.staff_id) >= all
( select count(s.staff_id) as cnt
from department d
inner join staff s on d.department_id=s.department_id
group by department_name )
order by d.department_name desc;
and I was able to pass one test case which results in 'SE' department, but I wasn't able to pass another test case.I don't know what second testcase want. I am not sure what I have done wrong in my code above.

You can use the windows function as follows:
select department_name, cnt
from
(select department_name, rank() over (order by cnt desc) as rn, cnt
from
(select d.department_name, count(1) cnt
from department d inner join staff s
on d.department_id=s.department_id
group by d.department_name))
where rn = 1
order by department_name

Related

Write a query to display the name of the department that has the maximum staff count order by department name

select max(count(department_id))
from staff
group by department_id
ERROR at line 4:
ORA-00918: column ambiguously defined
Select department_name
from staff s
inner join department d on s.department_id=d.department_id
having count(s.department_id) in (Select max(count(department_id))
from staff) group by department_id
--- Expected output ---
DEPARTMENT_NAME
------------------------------
SE
```none
---Difference in Output---
(select max(count(department_id)) from staff)group by departmentDEPARTMENT_idNAME
*------------------------------
SERROR at line 4:
ORA-00918: column ambiguously defined
Summary of tests
+------------------------------+
| 2 tests run / 0 test passed |
+------------------------------+
Query:
select dept.DEPARTMENT_NAME, count(dept.DEPARTMENT_NAME) as staff_count from
hr.departments dept, hr.employees emp
where dept.DEPARTMENT_ID=emp.DEPARTMENT_ID(+)
group by dept.DEPARTMENT_NAME
order by count(dept.DEPARTMENT_NAME) desc
FETCH FIRST 1 ROW ONLY;
You can run the above query in https://livesql.oracle.com/
And the for more information on Fetch can be found on link
https://oracle-base.com/articles/12c/row-limiting-clause-for-top-n-queries-12cr1
On multiple places, You missed alias and I have added it as following:
SELECT
D.DEPARTMENT_NAME -- ADDED ALIAS HERE
FROM
STAFF S
INNER JOIN DEPARTMENT D ON S.DEPARTMENT_ID = D.DEPARTMENT_ID
GROUP BY
D.DEPARTMENT_ID -- ADDED ALIAS HERE
HAVING
COUNT(S.DEPARTMENT_ID) IN (
SELECT
MAX(COUNT(DEPARTMENT_ID))
FROM
STAFF
);
Also, You can achieve the same result using the following query:
SELECT
D.DEPARTMENT_NAME
FROM
DEPARTMENT D
JOIN (
SELECT
S.DEPARTMENT_ID,
COUNT(1)
FROM
STAFF S
ORDER BY
2 DESC
FETCH FIRST ROWS ONLY
) S ON S.DEPARTMENT_ID = D.DEPARTMENT_ID;
Cheers!!
select department_id, count(*) from staff group by department_id
having count(*) = (select max(count(*))
from staff group by department_id )
check it out
select department_name from (select d.department_name,count(s.staff_id) as c from department d join staff s on d.department_id=s.department_id
group by d.department_name) mx where c=(select max(count(staff_id)) from staff group by department_id) order by department_name;
SELECT department_name
FROM department
WHERE department_id IN
(SELECT department_id
FROM staff
HAVING COUNT(department_id) IN
(SELECT MAX(COUNT(department_id))
FROM staff
GROUP BY department_id)
GROUP BY department_id);

Write a query to display the name of the department that has the maximum student count

this is the schema Write a query to display the name of the department that has the maximum student count.
this is what is tried.
select d.department_name,count(s.student_id)
from department d left join student s
on d.department_id=s.department_id
group by d.department_name,d.department_id
order by d.department_name;
and i think there is something missing in my code
You're almost there.
Order the result in descending order on the number of students and then take the first row:
SELECT department_name
FROM
(
SELECT d.department_name,
COUNT(*) AS nr_students
FROM department d
JOIN student s
ON d.department_id = s.department_id
GROUP BY d.department_name
ORDER BY nr_students DESC
)
WHERE ROWNUM <= 1;
Based on the schema mentioned, you would have to make a join (INNER JOIN) to the department table from the staff table to get the name of the department.
If the name of the department is not desired and the counts can just be based on the department_id, then a join is not required.
The queries for both the scenarios is mentioned below.
Oracle SQL query for result with the department name, i.e. with INNER JOIN
SELECT D.DEPARTMENT_NAME, COUNT(S.DEPARTMENT_ID) AS STAFF_COUNT FROM **DEPARTMENT D, STAFF S** --INDICATES INNER JOIN IN ORACLE SQL
WHERE D.DEPARTMENT_ID = S.DEPARTMENT_ID
GROUP BY D.DEPARTMENT_NAME
ORDER BY STAFF_COUNT DESC
Oracle SQL query for result without the department name, just the department_id
SELECT S.DEPARTMENT_ID,COUNT(S.DEPARTMENT_ID) AS STAFF_COUNT FROM STAFF S
GROUP BY S.DEPARTMENT_ID
ORDER BY STAFF_COUNT DESC
Hope this helps. Cheers.
I tried this and it worked.
select department_name
from department d inner join student s
on s.department_id=d.department_id
having count(*) in (
select max(count(student_id))
from student s join department d
on d.department_id=s.department_id
group by d.department_id)
group by d.department_id,department_name;
Select * from (
SELECT D.DEPARTMENT_NAME, COUNT(S.DEPARTMENT_ID) AS STAFF_COUNT
FROM DEPARTMENT D, STAFF S
WHERE D.DEPARTMENT_ID = S.DEPARTMENT_ID
GROUP BY D.DEPARTMENT_NAME
ORDER BY STAFF_COUNT DESC)
where rownum=1;
This query will give department name that has maximum number of student count

SQL Oracle - optimum query

I have three tables with the following schemas:
Persons(Person_id, Department_id, Sure_name, Name, Birthyear, Height, Manager_id)
Departments (Department_id, Department_Name, Code)
Salaries (Salary_id, Person_id, Salary)
I need to run a query which will display the name of the department, for which the difference between the workers' minimum and maximum heights is the greatest.
I have done it the following way:
select Department_Name
from Departments
where Department_id = (select Department_id
from Departments
join Persons
using (Department_id)
group by Department_id
having max(height) - min(height) = (select max(max(height) - min(height))
from Departments
join Persons
using (Department_id)
group by Department_id));
And it works fine, just I'm not really sure if the solution is optimum, there are two nested queries here, I wonder if I could achieve the same in a simpler way.
Try this.
SELECT Department_Name
FROM Departments D
INNER JOIN (SELECT Department_id,
Max(height) - Min(height) AS diff
FROM Departments
JOIN Persons
ON using (Department_id)
WHERE ROWNUM = 1
GROUP BY Department_id
ORDER BY diff DESC) B
ON d.Department_id = b.Department_id
or use Window Function
SELECT Department_id,
Department_Name
FROM (SELECT Row_number()OVER(ORDER BY Max(height)- Min(height) desc ) rn,
Department_id,
Department_Name
FROM Departments
JOIN Persons
ON using(Department_id)
GROUP BY Department_id,
Department_Name)a
WHERE rn = 1
This might work for you, a bit complicated but using the analytic function RANK() probably simplifies things a bit over using nothing but aggregates:
SELECT d.department_id, d.department_name, d.code, p1.height_diff
FROM departments d INNER JOIN (
SELECT department_id, height_diff, RANK() OVER ( ORDER BY height_diff DESC ) AS rn
FROM (
SELECT department_id, MAX(height) - MIN(height) AS height_diff
FROM Persons
GROUP BY department_id
)
) p1
ON d.department_id = p1.department_id
WHERE p1.rn = 1;
Please see SQL Fiddle demo here. N.B. I had previously ordered incorrectly, now corrected to DESC.

SELECT specific information

My tables are structured like this (there are more values in the tables but I only wrote the ones relevant to this):
Department(dep_id, dep_name)
Employee(dep_id)
I need to display dep_name and the number of employees in every department, except once specific department (let's call it DepX) and only the departments with more than one employee.
I tried multiple methods to solve this but none of them worked.
Some methods I tried:
SELECT department.dep_name, COUNT(employee.dep_id) AS NumberOfEmployees FROM employee
INNER JOIN department ON employee.dep_id=department.dep_id
WHERE dep_name<>'DepX'
GROUP BY dep_id
HAVING COUNT(employee.dep_id) > 1;
SELECT dep_name FROM department
WHERE dep_name <>'DepX'
UNION
SELECT COUNT(*) FROM employee
WHERE COUNT(*) > 1
GROUP BY dep_id;
I can't figure this out. Thanks!
The first example does now work because you're including dep_name in your results without an aggregation but not grouping on it.
You can either use the department name in your grouping instead of the ID:
SELECT department.dep_name, COUNT(employee.dep_id) AS NumberOfEmployees FROM employee
INNER JOIN department ON employee.dep_id=department.dep_id
WHERE dep_name<>'DepX'
GROUP BY department.dep_name
HAVING COUNT(employee.dep_id) > 1;
or do the COUNT in a subquery:
SELECT department.dep_name,
e.NumberOfEmployees
FROM department
INNER JOIN (SELECT dep_id,
COUNT(*) NumberOfEmployees
FROM employee
GROUP BY dept_id
HAVING COUNT(dept_id) > 1
) e
ON department.dep_id = e.dep_id
WHERE dep_name<>'DepX'
SELECT department.dep_name, COUNT(employee.dep_id) AS NumberOfEmployees FROM employee
INNER JOIN department ON employee.dep_id=department.dep_id
WHERE department.dep_name not in('DepX')
GROUP BY department.dep_name
HAVING COUNT(employee.dep_id) > 1;
update your table alias per your need
TEST this. This query help you return not only dept_name, it can return all fields from Department if you want:
SELECT d.*, A.numOfEmployees
FROM Department d,
(
SELECT e.dep_id, COUNT(*) numOfEmployees
FROM Employee e
GROUP BY e.dep_id
HAVING COUNT(*) > 1
) A
WHERE d.dep_id = A.dep_id
AND d.dep_name != 'DepX'

Select the biggest value

I am trying to solve a simple problem but i am getting stack on the details.
I have 2 tables, one has employees and the other one has departments. My problem: I am trying to check which department has the most employees and output only that specific department.
So far I have:
select count(*) Number_of_employees
from department d, employee e
where d.department_id = e.department_id
group by department_name
which outputs:
NUMBER_OF_EMPLOYEES
----------------------
2
4
3
3
3
My goal is to to output only the department with the most employees which is the department with 4 employees.
I tried using the MAX and JOIN but i am not so good with join yet so any suggestions will be appreciated.
#Zsolt Botykai
I think this is correct, apart from order by needs to be DESC, and I don't think you can refer to number_of_employees inside the query. ( you can't in oracle anyway ).
select department_name
from
(select department_name
,number_of_employees
from
( select department_name, count(*) Number_of_employees
from department d, employee e
where d.department_id = e.department_id
group by department_name)
order by Number_of_employees DESC)
where rownum = 1
You could do it this way to avoid the rownum:
select
max(d.department_name) keep (dense_rank first order by count(1) desc) as department_name
, count(1) as number_of_employees
from employee e
inner join department d on (e.department_id = d.department_id)
group by d.department_name
;
select department_name from
( select department_name, count(*) Number_of_employees
from department d, employee e
where d.department_id = e.department_id
group by department_name
order by 2 desc )
where rownum = 1
should do.
HTH