How to select min and max from table by column score ? Is this possible with session query ?
class Player(Base):
username = Column(String)
score = Column
For the case when you need to find minimum and maximum values for the score field. You can do this with a one query using min and max functions:
from sqlalchemy.sql import func
qry = session.query(func.max(Player.score).label("max_score"),
func.min(Player.score).label("min_score"),
)
res = qry.one()
max = res.max_score
min = res.min_score
from sqlalchemy import func
max = session.query(func.max(Table.column)).scalar()
min = session.query(func.min(Table.column)).scalar()