How to optimize sql query

I have included a documentation on how to optimize a long-running query, mostly you should avoid using Joins un-necessary unless you really need to use it.