updated query function to support groupBy orderBy and make whereClause optional