added sorting to sql queries