Morgan Stanley Interview Question

puzzles, math, sql question with join, standard c/c++