fixed sql query to get correct set of messages