SELECT tasks.*, students.*,
(
SELECT grade
FROM grades
WHERE grades.task_id = tasks.task_id
AND grades.username = students.username
ORDER BY
date DESC
LIMIT 1
) AS lastgrade
FROM assignments a
JOIN assignment_tasks at
ON at.assignmentID = a.assignmentID
JOIN assignment_students ast
ON ast.assignmentID = a.assignmentID
JOIN tasks
ON tasks.task_id = at.task_id
JOIN students
ON students.username = ast.username