Amazon Interview Question

Solve this SQL question and the expected results