0

I am having "ORA-00979: not a GROUP BY expression" error in a query using group by and having. Can anybody tell me where the problem is? This is the query.

select DEPTNUM, INSTNAME, DEPTNAME from department
NATURAL join academic
group by DEPTNUM
having count(deptnum) > 10

3 Answers3

0

Either add INSTNAME, DEPTNAME in the GROUP BY, or use aggregate function (such as MIN, MAX) for INSTNAME, DEPTNAME

Andrey Belykh
  • 2,578
  • 4
  • 32
  • 46
0

If you need and ggreted result on a column you shoudl not have this column in group by
and your error is related to the fact you have not column in group by clause taht are not aggregated in select

so try

select INSTNAME, DEPTNAME from department
NATURAL join academic
group by INSTNAME, DEPTNAME
having count(deptnum) > 10
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

Eschew natural join. It is pretty much an abomination, because it uses columns that have the same name rather than declared foreign key relationships.

Your real problem is the group by and select. I would suggest:

select DEPTNUM, INSTNAME, DEPTNAME
from department d join
     academic a
     using (deptnum)
group by DEPTNUM, INSTNAME, DEPTNAME
having count(deptnum) > 10;

I would also qualify instname and deptname, but it is not clear what table they come from.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786