Pregunta de entrevista de Moody's

How to solve a specific question using sql or other languages.