Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give priority to ORDER BY over a GROUP BY in MySQL without subquery

I have the following query which does what I want, but I suspect it is possible to do this without a subquery:

  SELECT * 
    FROM (SELECT * 
            FROM 'versions' 
        ORDER BY 'ID' DESC) AS X 
GROUP BY 'program'

What I need is to group by program, but returning the results for the objects in versions with the highest value of "ID".

In my past experience, a query like this should work in MySQL, but for some reason, it's not:

  SELECT * 
    FROM 'versions' 
GROUP BY 'program' 
ORDER BY MAX('ID') DESC

What I want to do is have MySQL do the ORDER BY first and then the GROUP BY, but it insists on doing the GROUP BY first followed by the ORDER BY. i.e. it is sorting the results of the grouping instead of grouping the results of the ordering.

Of course it is not possible to write

SELECT * FROM 'versions' ORDER BY 'ID' DESC GROUP BY 'program'

Thanks.

like image 949
Mahmoud Al-Qudsi Avatar asked Jan 17 '11 18:01

Mahmoud Al-Qudsi


People also ask

Can we use ORDER BY before GROUP BY in MySQL?

ORDER BY has to come after GROUP BY . But if we GROUP BY first, we run into the same issue above with the MAX function and, depending on your MySQL version, ONLY_FULL_GROUP_BY mode .

Can we use ORDER BY with GROUP BY?

Using Group By and Order By Together When combining the Group By and Order By clauses, it is important to bear in mind that, in terms of placement within a SELECT statement: The GROUP BY clause is placed after the WHERE clause. The GROUP BY clause is placed before the ORDER BY clause.

Can we use GROUP BY without order?

Key Differences between GROUP BY and ORDER BY The ORDER BY clause, on the other hand, sorts the result and shows it in ascending or descending order. It is mandatory to use the aggregate function to use the Group By. On the other hand, it's not mandatory to use the aggregate function to use the Order By.

Which comes first ORDER BY or GROUP BY?

In the query, GROUP BY clause is placed before ORDER BY clause if used any.


1 Answers

By definition, ORDER BY is processed after grouping with GROUP BY. By definition, the conceptual way any SELECT statement is processed is:

  1. Compute the cartesian product of all tables referenced in the FROM clause
  2. Apply the join criteria from the FROM clause to filter the results
  3. Apply the filter criteria in the WHERE clause to further filter the results
  4. Group the results into subsets based on the GROUP BY clause, collapsing the results to a single row for each such subset and computing the values of any aggregate functions -- SUM(), MAX(), AVG(), etc. -- for each such subset. Note that if no GROUP BY clause is specified, the results are treated as if there is a single subset and any aggregate functions apply to the entire results set, collapsing it to a single row.
  5. Filter the now-grouped results based on the HAVING clause.
  6. Sort the results based on the ORDER BY clause.

The only columns allowed in the results set of a SELECT with a GROUP BY clause are, of course,

  • The columns referenced in the GROUP BY clause
  • Aggregate functions (such as MAX())
  • literal/constants
  • expresssions derived from any of the above.

Only broken SQL implementations allow things like select xxx,yyy,a,b,c FROM foo GROUP BY xxx,yyy — the references to colulmsn a, b and c are meaningless/undefined, given that the individual groups have been collapsed to a single row,

like image 188
Nicholas Carey Avatar answered Sep 19 '22 12:09

Nicholas Carey