编写查询以显示部门名称和每个部门的人员数,如果不存在人员,则将其显示为0,

时间:2019-07-11 07:15:34

标签: sql

代码运行正常,给出了必要的输出,但是其中一个测试用例失败了。

select department_name,count(*) as staff_count 
from department d,staff s 
where d.department_id=s.staff_id 
group by department_name 
order by department_name;
Proposed grade: 50 / 100
 Result Description
 Failed Test
 Test Case 2

 Summary of tests
+------------------------------+
| 2 tests run / 1 test passed  |
+------------------------------+

qwe

带有必要输入数据的样本输出:

DEPARTMENT_NAME                STAFF_COUNT                                      
------------------------------ -----------                                      
CSE                                      2                                      
ECE                                      1                                      
EEE                                      2                                      
IT                                       2                                      
SE                                       4                                      
DEPARTMENT_ID DEPARTMENT_NAME                DEPARTMENT_BLOCK_NUMBER            
------------- ------------------------------ -----------------------            
            1 CSE                                                  3            
            2 IT                                                   3            
            3 SE                                                   3            
            4 ECE                                                  2            
            5 EEE                                                  2            
  STAFF_ID STAFF_NAME                     DEPARTMENT_ID                         
---------- ------------------------------ -------------                         
         1 Lakshmi                                    1                         
         2 Venky                                      1                         
         3 Senthil                                    2                         
         4 Sandhya                                    2                         
         5 Geetha                                     3                         
         6 Tom                                        3                         
         7 Rekha                                      3                         
         8 Viji                                       3                         
         9 Laya                                       4                         
        10 Nisha                                      5                         
        11 Venki                                      5

1 个答案:

答案 0 :(得分:0)

为此,您需要一个外部联接,而不是一个内部联接(WHERE子句中的旧旧隐式联接需要这样做)。您还需要在两个department_id列之间进行联接。

select department_name, count(s.staff_id) as staff_count 
from department d 
  left join staff s on d.department_id = s.department_id
group by department_name 
order by department_name;

对于没有人员的部门,左联接(=外联接)将为NUL返回s.staff_id。聚合函数只是忽略NULL值,这就是您获得0的人员数量的方法。

但是您的样本数据没有任何部门,没有员工。

带有附加空白部门的在线示例:https://rextester.com/EEXE2322