Here are some hints for better query structure:
In some cases, command statements that include subqueries can also be formulated as joins and may run faster.
If you group on multiple columns in a GROUP BY clause, list the columns by descending order by number of unique values. This will give you the best query performance.
Join indexes often cause join queries to execute faster than ad hoc joins, at the expense of using more disk space. However, when a join query does not reference the largest table in a multi-table join index, an ad hoc join usually outperforms the join index.
You can improve performance by using an additional column to store frequently calculated results.