首页 > 解决方案 > 这个查询的输出应该是什么

问题描述

显示位于同一地点的销售人员的销售人员 ID、销售人员姓名和位置。

我已经使用自我加入来解决它

select s.sid, s.sname, s.location 
from salesman s 
inner join salesman ss on s.location = ss.location 
where s.location=ss.location
Salesman  Table

SID SNAME   LOCATION
1   Peter   London
2   Michael Paris
3   John    Mumbai
4   Harry   Chicago
5   Kevin   London
6   Alex    Chicago

预期产出

Expected Result
SID SNAME   LOCATION
5   Kevin   London
6   Alex    Chicago
1   Peter   London
4   Harry   Chicago

标签: sqloracle11goracle10g

解决方案


我假设您并不特别关注您在预期输出中显示的序列并使用了更快的连接。

select s.sid, s.sname, s.location 
from salesman s 
inner join (
  select location from salesman group by location having count(*) >1
) ss on s.location = ss.location 

推荐阅读