Python SQLAlchemy - Group_by and return max date Last Updated : 22 Nov, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we are going to see how to use Group_by and return max date SQLAlchemy in Python. Installing SQLAlchemy SQLAlchemy is available via pip install package. pip install sqlalchemy However, if you are using flask you can make use of its own implementation of SQLAlchemy. It can be installed using - pip install flask-sqlalchemyExample Before we move ahead, we need to have a database and a table to work with. For this example, we are using mySQL database and have created a users table. The table has 6 columns and 7 records as shown below. Users Table - mySQLIn the above table, we can see that there are 3 distinct users namely, John, Emma, and Liam. We will GROUP BY on the basis of their first_name and last_name. The table has a created_on field which is a TIMESTAMP data type. It will be used to pick the maximum date for a distinct user. Approach:First, we import the sqlalchemy library as db for simplicity. All the sqlalchemy objects, methods, etc will be imported using db prefix for better clarity.We then create the engine which will serve as a connection to the database to perform all the database operations.Create the metadata object. The metadata object `metadata` contains all the information about our database.Use the metadata information to fetch the users table from database.We can now write an SQLAlchemy query to fetch the required records. We perform GROUP BY operation on first_name and last_name fields and in the SELECT query retrieve the maximum date using the SQLalchemy's `func.max()` function.Print all the fetched records. In the output we can view that we have only 3 distinct users and the corresponding updated_on field for all of the users is the maximum date that we have in the table entries.Below is the implementation: Python import sqlalchemy as db from sqlalchemy.engine import result # Define the Engine (Connection Object) engine = db.create_engine( "mysql+pymysql://root:password@localhost/Geeks4Geeks") # Create the Metadata Object meta_data = db.MetaData(bind=engine) db.MetaData.reflect(meta_data) # Get the `users` table from the Metadata object USERS = meta_data.tables['users'] # SQLAlchemy Query to GROUP BY and fetch MAX date query = db.select([ USERS.c.email, USERS.c.first_name, USERS.c.last_name, db.func.max(USERS.c.created_on) ]).group_by(USERS.c.first_name, USERS.c.last_name) # Fetch all the records result = engine.execute(query).fetchall() # View the records for record in result: print("\n", record) Output: Comment More infoAdvertise with us Next Article Python SQLAlchemy - Group_by and return max date A apathak092 Follow Improve Article Tags : Python Python-SQLAlchemy Practice Tags : python Similar Reads Group by and count function in SQLAlchemy In this article, we are going to see how to perform Groupby and count function in SQLAlchemy against a PostgreSQL database in Python. Group by and count operations are performed in different methods using different functions. Such kinds of mathematical operations are database-dependent. In PostgreSQ 2 min read SQLAlchemy - Order By before Group By In this article, we are going to see how to perform the orderby function before using a groupby function in SQLAlchemy against a PostgreSQL database in Python. PostgreSQL, Group by is performed using a function called groupby(), and order by the operation is performed using orderby(). Usage: func.s 2 min read SQLAlchemy: How to group by two fields and filter by date In this article, we will see how to group records by two fields and filter by date using SQLAlchemy in Python. Since we are going to use MySQL in this article, we will also install a SQL connector for MySQL in Python. However, none of the code implementations changes with change in the database exce 3 min read Max and Min date in Pandas GroupBy Prerequisites: Pandas Pandas GroupBy is very powerful function. This function is capable of splitting a dataset into various groups for analysis. Syntax: dataframe.groupby([column names]) Along with groupby function we can use agg() function of pandas library. Agg() function aggregates the data tha 1 min read Join with sum and count of grouped rows in SQLAlchemy SQLAlchemy is a popular Python ORM (Object-Relational Mapping) library that provides a convenient way to interact with databases. One of the common tasks when working with databases is to perform joins between tables and calculate aggregate values based on grouped rows. In this article, we will expl 4 min read Like