Describe a scenario where you had to write a complex SQL query. What was the challenge, and how did you overcome it?