Fractal interview question

SQL, leetcode medium to hard level