Skip to content
Advertisement

Using group by for attributes which will not be selected

Let’s suppose that I have the following table:

employee (id, name, surname, salary);

The question is: Can I group the selection with attributes which don’t appear in the select clause?

For example:

select count (*)
from employee
where salary > 40000
group by id

I know that in this case the group by clause is useless, but it just an example. Is this correct?

Advertisement

Answer

It’s implementation dependent. For example, Oracle requires that the columns named in the ‘group by’ clause appear in the select statement. Others, including MySql, for example, do not.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement