Fix SQL query. Refs. #386