Replaced raw SQL string with SQLAlchemy expressions