Refactor the sql statement in the database sample