Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count() without GROUP BY

Tags:

sql

So I tried running this simple code

select COUNT(Ename),BOSS
from EMPLOYEE
WHERE BOSS=7839

but seems I did not get the output unless I use a "group by" boss below. I am not able to understand why can't I get output without "group by".

Can anybody explain in laymen terms. Would be much appreciated.

like image 253
SASHI PANDA Avatar asked Mar 18 '26 04:03

SASHI PANDA


1 Answers

Count is an aggregate function. When you use Count (or any other aggregate function such as Count, Sum, Max, Min) next to select, then every other column item must be in group by. If you only use

select COUNT(Ename) -- there is no BOSS
from EMPLOYEE

then you don't have to use Group By. Lets say you have 5 columns: 1-COUNT(Ename), 2- BOSS, 3- column3, 4- column4, 5-column5. Then you would have to write select code as below:

select COUNT(Ename),BOSS, column3, column4, column5
from EMPLOYEE
WHERE BOSS=7839
GROUP BY
   BOSS, column3, column4, column5
like image 164
Erol Avatar answered Mar 19 '26 23:03

Erol