SQL: Several Fixes