How to use avg and sum in SQLAlchemy Query? Last Updated : 22 Nov, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we are going to see how to use avg and sum in SQLAlchemy query using Python. Installing SQLAlchemy SQLAlchemy is available via the 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 the MySQL database and have created a students table. The table has 3 columns and 6 records as shown below. In the table, we have a float column `percentage` on which we will perform our average and sum operations using SQLAlchemy. 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 students table from the database.We can now write an SQLAlchemy query to fetch the required records. We first extract the average value of the percentage column using SQLalchemy's `func.avg()` function. Then we use the `func.sum()` function to get the sum of the values in the percentage column. Note that in both cases we have used the method `func.round(val, 2)` to round off the values to 2 decimal places.Print the output. In the output we can view that we have both the sum and average values for the percentage field.Below is the implementation: Python import sqlalchemy as db # 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 `students` table from the Metadata object STUDENTS = meta_data.tables['students'] # SQLAlchemy Query to get AVG query = db.select([db.func.round(db.func.avg(STUDENTS.c.percentage), 2)]) # Fetch the records avg_result = engine.execute(query).fetchall() # SQLAlchemy Query to get SUM query = db.select([db.func.round(db.func.sum(STUDENTS.c.percentage), 2)]) # Fetch the records sum_result = engine.execute(query).fetchall() # View the records print("\nAverage: ", avg_result[0]) print("\nSum: ", sum_result[0]) Output: Comment More infoAdvertise with us Next Article How to use avg and sum in SQLAlchemy Query? A apathak092 Follow Improve Article Tags : Python Python-SQLAlchemy Practice Tags : python Similar Reads How to use sum and order by in SQLAlchemy query? In this article, we are going to see how to perform the sum and count function in SQLAlchemy against a PostgreSQL database in python. SUM and count operations are performed in different methods using different functions. Such kinds of mathematical operations are database-dependent. In PostgreSQL, Gr 3 min read How to GroupBy and Sum SQL Columns using SQLAlchemy? In this article, we are going to see how to use GroupBy and Sum in SQLAlchemy. Installing SQLAlchemySQLAlchemy is available via pip install package. pip install sqlalchemyHowever, if you are using a flask you can make use of its own implementation of SQLAlchemy. It can be installed using - pip insta 4 min read How to round a number in SQLAlchemy and MySQL? In this article, we are going to round a number using SQLAlchemy and MySQL. Database related information : We have used a mysql database along with a table named "student". Database used : geeksforgeeks Table used : studentsRequirements : We need MySQL installed along the with "SQLAlchemy" module an 3 min read How to use the IN operator in SQLAlchemy in Python? In this article, we will see how to use the IN operator using SQLAlchemy in Python. We will cover 2 examples, one each for SQLAchemy Core and ORM layers. In both examples, we will count the number of records present in the category table within the sakila database. The sample data from the table loo 4 min read How to divide two columns in SQLAlchemy? In this article, we are going to divide two columns using the SQLAlchemy module of python. Installing SQLAlchemy To install SQLAlchemy, run the following command in the terminal. pip install sqlalchemy pymysql So, what we have to do in this post is to divide two columns and get output using SQLAlche 2 min read Like