SQL optimizations

+removed some trailing spaces.