How to get the most out of Aggregate Functions in SQL