MySql Order By Group SELECT course_id, student_name, score, ROW_NUMBER() OVER (PARTITION BY course_id ORDER BY score DESC) AS row_num