Moody's interview question

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