I am struggling to figure this out, I have a simple MySQL table (test_scores) that is three columns, id(students), date and test grade.
id | date | grade |
---|---|---|
1 | 10/20/22 | A |
2 | 10/20/22 | B |
1 | 11/1/22 | F |
2 | 11/1/22 | C |
3 | 11/1/22 | A |
1 | 12/1/22 | C |
I want to get the grade a student gets on the next test after they get an F.
In this example I would like to get the following output.
id | grade | next_grade |
---|---|---|
1 | F | C |
I have tried:
SELECT id, grade, grade as next_grade FROM test_scores WHERE grade = F;
I’m just learning advanced joins and nested queries, and I just can’t figure out how to get the next grade. Any help would be appreciated.
2
Answers
There is plenty of room for optimization.
First you have to guess, that each student can’t have many graduation, but number of student is big. Also not many student have F.
So. First you have do is get all students ever get F grade
After that you need try to find another grade for that students.
This one give all scores which was after F. i.e will give two rows if you have F,A,B. But this should not happens, no?
You can replace left join with lookup if you think you can have more than one row, but it will be slightly less speed.
Recomended indexes – test_score(id) – I hope you have it. I mean you HAVE to make index on primary column in most cases.
Maybe can be useful test_score(id,date) – but this has to be checked on real data. Likely will be useful from 10m+ tables, but very low chance. just because every single student can’t have really many graduations results in his life.
ps. this will work only on date or timestamp column. If your column is text replace join to
p.p.s. if your tables are really big you may consider not do subquery, but move where from first subquery outside and just do left join. Which convert this SQL from index lookup type to index match type. It may or may not speed up it.
Using lead will be much easier, but it requires MySQL 8+.
https://dbfiddle.uk/k2-FDxQO