Use SQL features to improve code

Use some of the countless SQL features to improve & shorten code.

For example, use `AS` for better output columns, use multiple table combinations to prevent multiple queries, use `GROUP BY` or `DISTINCT`.