Sort a Moodle sql pivot query to return highest value for one row I am using the SQL query below to retrieve some quiz data from a Moodle database.