SpaceX Interview Question

How do you optimize this query