SQL: Several fixes and additions