Always plan.
It really depends on the task. Some queries can be very simple.
For more complicated queries just use a wizard to get the bulk of the query created then edit the query as necessary for the requirements.
Look for existing queries or code that you can use as a pattern or template. Or copy and edit to meet the requirements.
The same approach can be used for writing programs.
Are you familar with flowcharting? A lost art in many ways but a good flowchart is a plan. Especially if you follow the common symbols and practices. Flowcharts can be simple or very complicated.
Simply sketch out a visual representation of what needs to be done be it a query or a program. Step by step.
Remember that the flowchart is also a tool and like any tool must be properly used.
Just find a flowcharting method that suits you and your environment.
Plan and code accordingly.