accept the following data:
Data:
id | date | name | grade
--------+---------------+-----------+---------------
1 | 2010/12/03 | Mike | 12
2 | 2010/12/04 | Jenny | 12
3 | 2010/12/04 | Ronald | 15
4 | 2010/12/03 | Yeni | 11
I want to know who has the best grade every day, something like this:
Desired Result:
id | date | name | grade
--------+---------------+-----------+---------------
1 | 2010/12/03 | Mike | 12
3 | 2010/12/04 | Ronald | 15
I thought the query should look like this:
SELECT name FROM mytable
GROUP BY date
ORDER BY grade DESC
but it returns something like this:
Current unwanted result:
id | date | name | grade
--------+---------------+-----------+---------------
1 | 2010/12/03 | Mike | 12
2 | 2010/12/04 | Jenny | 12
I searched and I found the reason:
GROUP BY occurs before ORDER BY, so it does not see and cannot apply ORDER.
so how can i apply ORDER on GROUP BY?
Note: please keep in mind that I need the simplest query because my query is actually very complex, I know that I can achieve this result with some subqueries or JOINing, but I want to know how to apply ORDER to GROUP BY. thank