Functional approach to query database using SQLAlchemy