从oracle中的表中选择员工

时间:2013-02-04 14:47:31

标签: sql oracle11g

大家好,我有以下问题与以下问题相关

From tables emp and dept find number of employees working in every departments by their jobs.
Showing number not more than 5.

我写了以下内容

select count(*)
from emp  e,dept d
where  e.department_id=d.department_id  and  count(*)<5

group by   e.department_id,e.job;

但它显示我有以下错误

Error at Command Line:3 Column:23
Error report:
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"
*Cause:    
*Action:

有什么问题?请帮帮我

3 个答案:

答案 0 :(得分:4)

尝试使用HAVING子句

SELECT  COUNT(*)
FROM    emp  e 
        INNER JOIN dept d 
            ON e.department_id=d.department_id  
GROUP   BY   e.department_id,e.job
HAVING  count(*)<5 ;

答案 1 :(得分:3)

由于您尝试根据count(*)值进行过滤,因此需要使用HAVING子句。 HAVING子句使用聚合函数进行过滤。

虽然@JW's answer没有任何问题,但我会略微更改它以使用ANSI JOIN语法而不是JOIN子句中的WHERE语法:

select count(*) Total
from emp e
inner join dept d
    on e.department_id=d.department_id  
group by e.department_id, e.job 
having count(*) < 5

或者,您可以使用WHERE

将查询作为子查询进行过滤
select Total
from
(
   select count(*) Total
   from emp e
   inner join dept d
      on e.department_id=d.department_id  
   group by e.department_id, e.job 
) src
where total < 5

答案 2 :(得分:2)

试试这个:: having

select count(*)
from emp  e,dept d
where  e.department_id=d.department_id  

group by   e.department_id,e.job having count(*)<5;