Bug #51745 Lack of GROUP BY fails to stop query execution with non-aggregated column

Submitted: 5 Mar 2010 4:29 Modified: 5 Mar 2010 4:34
Reporter: Michael McLaughlin Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: General Severity:S2 (Serious)
Version:Community 5.1 OS:Any
Assigned to: CPU Architecture:Any
Tags: AGGREGATED COLUMNS, GROUP BY, NON-AGGREGATED COLUMNS

[5 Mar 2010 4:29] Michael McLaughlin
Description:
The GROUP BY fails to raise an error when excluded with an aggregation function and a non-aggregated column. It simply returns the result of the aggregation against all rows in the table. This appears to be an incorrect behavior.
 

How to repeat:
DROP TABLE IF EXISTS nulls;
CREATE TABLE nulls ( stringfield VARHCAR(10));

INSERT INTO nulls VALUES ('');
INSERT INTO nulls VALUES (null);

SELECT   stringfield, count(*)
FROM     nulls
WHERE    stringfield IS NULL;

+-------------+----------+
| stringfield | count(*) |
+-------------+----------+
| NULL        |        1 |
+-------------+----------+
1 row in set (0.00 sec)

SELECT   stringfield, count(*)
FROM     nulls
WHERE    stringfield = '';

+-------------+----------+
| stringfield | count(*) |
+-------------+----------+
|             |        1 |
+-------------+----------+

SELECT   stringfield, count(*)
FROM     nulls
GROUP BY stringfield;

+-------------+----------+
| stringfield | count(*) |
+-------------+----------+
| NULL        |        1 |
|             |        1 |
+-------------+----------+
2 rows in set (0.00 sec)

SELECT   stringfield
,        CASE
           WHEN ISNULL(stringfield) THEN 'A real null'
           ELSE 'A what again?'
         END
FROM     nulls;

+-------------+-----------------------------------------------+
| stringfield | CASE                                          |
|             |   WHEN ISNULL(stringfield) THEN 'A real null' |
|             |   ELSE 'A what again?'                        |
|             | END                                           |
+-------------+-----------------------------------------------+
|             | A what again?                                 |
| NULL        | A real null                                   |
+-------------+-----------------------------------------------+
2 rows in set (0.00 sec)

SELECT   stringfield, count(*)
FROM     nulls;

+-------------+----------+
| stringfield | count(*) |
+-------------+----------+
|             |        2 |
+-------------+----------+
1 row in set (0.00 sec)

INSERT INTO nulls VALUES ('x');

SELECT   stringfield, count(*)
FROM     nulls;

+-------------+----------+
| stringfield | count(*) |
+-------------+----------+
|             |        3 |
+-------------+----------+
1 row in set (0.00 sec)

SELECT   stringfield, count(*)
FROM     nulls
GROUP BY stringfield;

+-------------+----------+
| stringfield | count(*) |
+-------------+----------+
| NULL        |        1 |
|             |        1 |
| x           |        1 |
+-------------+----------+
2 rows in set (0.00 sec)

Suggested fix:
The lack of a GROUP BY in a statement where one column is aggregated and another isn't aggregated should raise a syntax error, and not perform the operation as if the non-aggregated column weren't in the select predicate.
[5 Mar 2010 4:34] Valeriy Kravchuk
Sorry, but this is intended and documented behavior. Read http://dev.mysql.com/doc/refman/5.1/en/group-by-hidden-columns.html. 

Use ONLY_FULL_GROUP_BY SQL mode to get the behavior you expect.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.049 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.