Bright Money interview question

Hard level leetcode SQL problem