Corrected sql query