SQL trimmed down and optimised. Enjoy.