I do this a lot with GPT4 and I can't recall it ever generating an invalid query, and I did have some complicated ones.
The issue is that SQL is not composable. It's necessary to articulate the messy operation all at once and it's not possible to iterate on it.
Good luck!