Bug fix for limit sql