Pregunta de entrevista de Amazon

Solve this SQL question and the expected results