How to ORDER BY DESC in SQLAlchemy in Python

The ORDER BY keyword lists the rows of a table as sorted by the values of a column. The DESC keyword specifies an ordering in descending order. Using the keywords together in SQLAlchemy returns an SQLAlchemy Query object with the rows sorted by the given column in descending order.

Solution for How to ORDER BY DESC in SQLAlchemy in Python : You can use sqlalchemy.sql.expression.desc() with sqlalchemy.orm.query.Query.order_by() to use the ORDER BY keyword with the DESC keyword in SQLAlchemy Call session.query(Class) with an SQLAlchemy Session object session and your table class Class to create an SQLAlchemy Query object query corresponding to the given session. Create a DESC expression over the target column Column by calling sqlalchemy.sql.expression.desc(Column). Call query.order_by(desc_expression) to apply the previous result desc_expression, returning a SQLAlchemy Query. Note that query.order_by() can take more than one ordering criterion (to break ties, for instance).


how-to-order-by-desc-in-sqlalchemy-in-python