Fixed sql query for all works