fixed ordering of sql