lml/ost#341: Rewrite the query to actually sort the students.

- Use tips from the most excellent @Dantemss.
- Add a scope `by_student` to student_exercises along with
  requisite relations.
- Use an app level group by to avoid sending out 3 queries.