Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does MySQL allow "group by" queries WITHOUT aggregate functions?

Surprise -- this is a perfectly valid query in MySQL:

select X, Y from someTable group by X 

If you tried this query in Oracle or SQL Server, you’d get the natural error message:

Column 'Y' is invalid in the select list because it is not contained in  either an aggregate function or the GROUP BY clause. 

So how does MySQL determine which Y to show for each X? It just picks one. From what I can tell, it just picks the first Y it finds. The rationale being, if Y is neither an aggregate function nor in the group by clause, then specifying “select Y” in your query makes no sense to begin with. Therefore, I as the database engine will return whatever I want, and you’ll like it.

There’s even a MySQL configuration parameter to turn off this “looseness”. http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_only_full_group_by

This article even mentions how MySQL has been criticized for being ANSI-SQL non-compliant in this regard. http://www.oreillynet.com/databases/blog/2007/05/debunking_group_by_myths.html

My question is: Why was MySQL designed this way? What was their rationale for breaking with ANSI-SQL?

like image 945
Aaron Fi Avatar asked Aug 03 '09 23:08

Aaron Fi


People also ask

Can we use GROUP BY without aggregate function MySQL?

Do not use the GROUP BY clause without an aggregate function. The rule is deprecated in the new SQL Server versions. The execution plan generated by using the GROUP BY is not different than the one of using DISTINCT.

Can GROUP BY be used without aggregate functions in SQL?

You can use the GROUP BY clause without applying an aggregate function.

Is aggregate function necessary with GROUP BY?

Expressions that are not encapsulated within an aggregate function and must be included in the GROUP BY Clause at the end of the SQL statement. This is an aggregate function such as the SUM, COUNT, MIN, MAX, or AVG functions.

Can GROUP BY only be used with aggregate functions?

Guidelines of using GROUP BY clause (3) GROUP BY clause can only be used with aggregate functions like SUM, AVG, COUNT, MAX, and MIN. If it is used with single row functions,Oracle throws an exception as "ORA-00979: not a GROUP BY expression". (4) Aggregate functions cannot be used in a GROUP BY clause.


2 Answers

According to this page (the 5.0 online manual), it's for better performance and user convenience.

like image 52
Miroslav Genev Avatar answered Oct 08 '22 14:10

Miroslav Genev


I believe that it was to handle the case where grouping by one field would imply other fields are also being grouped:

SELECT user.id, user.name, COUNT(post.*) AS posts  FROM user    LEFT OUTER JOIN post ON post.owner_id=user.id  GROUP BY user.id 

In this case the user.name will always be unique per user.id, so there is convenience in not requiring the user.name in the GROUP BY clause (although, as you say, there is definite scope for problems)

like image 28
Cebjyre Avatar answered Oct 08 '22 13:10

Cebjyre