代码运行正常,给出了必要的输出,但是其中一个测试用例失败了。
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 |
+------------------------------+
带有必要输入数据的样本输出:
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
答案 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