SQL: few fixes