Visa Inc. Interview Question

Explain the logic while solving sql, python question