SQL backend cont'd (fix errors in SQL statements; Up refactor for dirty database)