select count(*) from emp having count(*) > 2;

In the attachnment is the query executed on oracle 10g. But, the correct answer to the query n the test series is that it will give an error. PLease explain

Gaurav @gauravchaudhari
6 Dec 2017 10:43 am

Using having without group by is a valid syntax and works with aggregate functions like count.

As group by is not there, it simply considers whole table and while operation, it has no criteria to categorize and hence it just returns the number of rows since it considers it has one group and returns it's count.

However, if you use having without group by with conditions like empname = 'fvgf' , it'll show an error. 

Vaishnavi Joshi @vaishnavijoshi
9 Dec 2017 04:03 pm

So,what must be our answer in such cases?

Gaurav @gauravchaudhari
9 Dec 2017 04:12 pm

It is quite ambiguous question. In such cases it's better to say something works only if it works in all cases else it doesn't.