fix SQL for pagination