SSMS Count with HAVING - SQL - sql

I'm trying to count the number of Departments in a database where there are exaclty 6 employees in each Department.
I've written the query below, but it returns the rows with the Departments where there are 6 employees.
But what I'd like is the TOTAL NUMBER of ROWS where each Department has 6 employees.
Does anyone know how I can modify this query to give me a total number, please?
TIA
select count(Department)
--Department as [Department Name]
from HumanResources.vEmployeeDepartment
GROUP BY Department
HAVING count(Department) = 6

You can wrap your query with another count statement to count the number of departments returned by the inner query:
select COUNT(*) from
(
select Department as cnt
from HumanResources.vEmployeeDepartment
GROUP BY Department
HAVING count(Department) = 6
) as t

Related

how to display employee that doesn't belong to department (SQL)

how to write SQL to display How many employees doesn't belong to any department?
How about this?
select count(*)
from employees
where deptno is null;
That's basic filtering:
select count(*) cnt
from employees
where depno is null
That gives you the count of employees that are not assigned to a department. If you want to list the corresponding rows:
select *
from employees
where depno is null

Find department with maximum number of employees in Oracle SQL

I have these two tables and I need to find the department name with maximum number of employees.
The other solutions were not for Oracle, so I'm posting this question. Also, it would be really helpful if the query can be explained thoroughly as I'm finding it hard to visualise it.
EMPLOYEE
EMPNO EMPNAME MANAGER SALARY DEPT_NO
1 Puja 6 30000 2
2 Purabi 1 15000 3
3 Barun 6 23000 2
4 Sudha 1 20000 1
5 Amal 2 20000 1
6 Rakesh 3 30000 4
DEPARTMENT
Dept_No Dept_Name Location
1 Production LaneA
2 Marketing LaneB
3 Sales LaneC
4 HR LaneD
So far I could manage getting the highest number of employees. So I was thinking if somehow I can write another sub-query where I count the employees in the departments again and compare them to the max_num_emp that I calculated in the first query.
This is the query which retrieves the maximum number of employees. It does not return the dept_no.
select count(dept_no)
from employee
group by dept_no
order by count(dept_no) desc
fetch first row only;
Expected output
DEPT_NAME
Production
Marketing
I can also add the dept_no column in the query, then I will have to somehow find out how to get the max and that was somehow giving me errors because the query was violating some rules. I had actually tried doing max(above query).
So I thought of just getting the maximum employee count and then determine all departments which have those many employees and display their name.
You have a working query which you need to join to the table department:
select d.Dept_Name
from department d inner join (
select dept_no
from employee
group by dept_no
order by count(*) desc
fetch first row only
) t
on t.dept_no = d.dept_no
Edit
Try this (I cannot try it):
select d.dept_name
from department d inner join (
select x.dept_no from (
select dept_no, rank() over (order by count(*) desc) rn
from employee
group by dept_no
order by count(dept_no) desc
) x
where x.rn = 1
) t
on t.dept_no = d.dept_no
You may have used FETCH..FIRST syntax using WITH TIES instead of ONLY.
SELECT d.dept_name
FROM department d
JOIN employee e ON d.dept_no = e.dept_no
GROUP BY d.dept_name
ORDER BY COUNT(*)
DESC FETCH FIRST 1 ROW WITH TIES ;
Demo
If you are not looking for duplicates, then:
select d.dept_name, count(*)
from department d join
employee e
on d.dept_no = e.dept_no
group by d.dept_no, d.dept_name
order by count(dept_no) desc
fetch first row only;

Sql query to return the count of employees in each department

I am new to Sql query if I had a table called Employee:
Id, Name, Department
1 tim sales
2 tom sales
3 jay HR
4 ben design
5 lin design
I am trying to write a query that returns the number of employees in each department.
SELECT COUNT(Department) FROM Employee
Does anyone have any advise on how I can improve this query?
****Edit
What about if I wanted to return the number of employees in the same department
SELECT COUNT(id), department FROM Employee GROUP BY department;
Using group by
SELECT COUNT(id), department FROM Employee GROUP BY department
OR
By WHERE conditionSELECT COUNT(id),Department FROM Employee WHERE Department='HR'
SELECT COUNT([id]), [department] FROM [Employee], GROUP BY [department].

list pair of employees that work in same department

in oracle SQL how can I list pair of employees that work in same department ... my employee table as follow:
employeeid department
101 2
102 2
103 3
104 3
105 4
You can get all employees in departments that have multiple employees by
SELECT * FROM employees
WHERE department IN
(
SELECT department
FROM employees
GROUP BY department
HAVING COUNT(*) > 1
)
Or use LISTAGG to show the IDs in a list:
SELECT
department,
LISTAGG(employeeid, ', ') AS EmployeeList
FROM employees
GROUP BY department
HAVING COUNT(*) > 1
If your table is called t, you can "list" the pairs of employees who work in the same department with a self-join, like this:
select t1.employeeid as employee1, t2.employeeid as employee2, department
from t t1 join t t2 using (department)
where t1.employeeid < t2.employeeid
Fair warning: for departments with a large number of employees, the number of pairs grows asymptotically like 1/2 times the square of the number of employees in that department, so you may get a very large number of rows on a "real" input table.
If needed, you can sort the results as needed, for example add this line:
order by department, employee1, employee2

SQL Query using Group by 4

I have tables which looks like below.
Employee table
Date Employee ID Employer ID Salary
2/3/2011 10 20 45666
3/12/2009 43 53 2356
Employer Table
Employer ID State
53 OH
42 MI
Trying to get the total salary by month and by state using group by clause. But not getting the results. what am i doing wrong?? any help appreciated
select date, sum(salary) from employee
group by to_char(date,'MON')
select sum(salary) from employee A, Employer B
where A.employer id=B.employer id
group by B.state
Also i need to get the top 10 distinct employee ids based on their salary
select DISTINCT employee id from employee
where rownum<=10
order by salary desc
You have to group by the exact expression in your select list, e.g.,
select to_char(date,'MON'), sum(salary)
from employee
group by to_char(date,'MON');
You probably want to include the state in your second query:
select b.state, sum(salary)
from employee A, Employer B
where A.employer_id=B.employer_id
group by B.state;
Generally speaking, stating in your question that you're "not getting the results" is not very helpful to the folks you're asking help of. Please provide any error messages or output that describes what "not getting the results" means.