Sql 从联接图的其余部分断开连接

Sql 从联接图的其余部分断开连接,sql,oracle,join,Sql,Oracle,Join,我的任务是查询数据库,选择在数量最多的部门工作的人数。我做了以下工作: select count(*) from Persons join Departments using (Department_id) where Department_id = (select Department_id from Persons join Departments using (Department_id) group by Department_id having count(*) = (se

我的任务是查询数据库,选择在数量最多的部门工作的人数。我做了以下工作:

select count(*) from Persons join Departments using (Department_id)
where Department_id = 
(select Department_id from Persons join Departments using (Department_id) 
 group by Department_id having count(*) = 
  (select max(count(*)) from Persons 
   join Departments using (Department_id) group by Department_id)
);

它工作正常,但我得到一个警告,Persons与连接图的其余部分断开连接。这个解决方案有什么问题吗?或者可以更简单一些?

你说得对,连接实际上是多余的。以下是我提出的新解决方案:

select count(*) from Persons
where Department_id is not null
group by Department_id
having count(*) = (select max(count(*)) from Persons
                  where Department_id is not null
                  group by Department_id);
我不喜欢maxcount*结构。它是一个Oracle扩展,它更改了组的语义,在所有其他情况下,通过该语义每个组返回一行。那就:

with d as (
      select count(*) as cnt
      from persons
      group by department_id
     )
select *
from d
where cnt = (select max(cnt) from d);

我不知道有什么特别的警告,但根据您的查询,可能不需要加入,除非您有过时的个人数据?:SELECT*FROM SELECT COUNT*FROM PERSOMERS GROUP BY DECU_id ORDER BY 1 DESC,其中ROWNUMBER<2这听起来像是来自您客户的消息,不是来自数据库/语句。您能给我们完整准确的错误消息吗?我同意Alex的观点,这很可能不是Oracle的消息,而是来自您的应用程序的消息。如果两个部门拥有相同的最大员工数量,该怎么办?