The example in https://moodle.org/mod/forum/discuss.php?d=213578#p1003462 shows one case where there are more than two duplicated rows, so Tim's query:
SELECT
MAX(id),
question,
attempt,
seq_number,
COUNT (seq_number) AS NumOccurrences
FROM
mdl_question_states
GROUP BY
question,
attempt,
seq_number
HAVING (COUNT(seq_number) > 1 )
ORDER BY attempt, question;
does not return ALL the duplicate rows.
And the DELETE code that Tim posted also does not remove all the duplicates. I still crash with the "Cannot insert duplicate key row in object 'dbo.mdl_question_attempt_steps' with unique index 'mdl_quesattestep_queseq_uix'" error AFTER running that DELETE statement.
Considering that I have 8 million rows in the mdl_question_states table, how can I get a list of all the duplicate rows that are causing this error? I'm completely unable to upgrade Moodle because of this problem...