rewrote the logic in output-sql to be more direct, also fixed a missing space character