refactor SQL codebase