Bug #51745 Lack of GROUP BY fails to stop query execution with non-aggregated column
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.
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.
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.