tags:

views:

102

answers:

1

Such as this one:

SELECT id, count( * ) , company FROM jobs GROUP BY company

Where id is the primary key of jobs

+1  A: 

Similar question:

http://stackoverflow.com/questions/1225144/why-does-mysql-allow-group-by-queries-without-aggregate-functions

This is MySQL specific, and is not ANSI standard SQL. Most other database engines do not allow columns on which is not being grouped or being run through an aggregate function.

It seems MySQL retains the value of the first row that matches the criteria.

The aggregate function that has this behaviour is FIRST(), and although MySQL implements it, this seems to be default behaviour for columns that are not grouped on, and which are not run through any other aggregate function.

In ANSI standard SQL you would do:

SELECT FIRST(jobtitle), company FROM jobs GROUP BY company;

Whereas in MySQL you could just (however the ANSI standard works just aswell):

SELECT jobtitle, company FROM jobs GROUP BY company;
Yannick M.
FWIW, SQLite also supports this non-standard behavior of group-by, but SQLite returns the value from the *last* matching row in the group.
Bill Karwin