Amazon interview question

Solve this SQL question and the expected results