How to Compute the Average of a Column of a MySQL Table Using Python? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 2 Likes Like Report A MySQL connector is needed to generate a connection between Python and the MySQL server. Here we will import mysql.connector library to get the average of the specified column in the given database. If you need to know how to install MySQL, see How to Install MySQL in Python 3. Average Function of SQL SQL AVG() function returns the average of values of a numeric column in a table. It is generally used with the WHERE clause. AVG() Function Syntax SELECT AVG(column_name) FROM table_name WHERE condition; The following program will help you understand this better. Database used: Students Table in school databaseSteps to be followed:So we first must import mysql.connector . Once that is imported, we gain connection to the MySQL database using the mysql.connector.connect() function.We then have to create a cursor for the table.Next, we execute our function to find the average of the Marks column of the Students table using the cursor.execute() function. Inside of this function, we place in the line, "SELECT AVG(Marks) AS average FROM students".We then create a variable named rows and set it equal to cursor.fetchall().We then use a for loop and print out i[0], which represents the average of the Marks column.We then close the database once we've done what we've needed to.And by this is we can find the average of all the rows of a column in a MySQL table using Python. Implementation: Program to find Average using MySQL connector in Python 3. Python3 import mysql.connector # database connection connection = mysql.connector.connect( host="localhost", user="root", password="", database="school") cursor = connection.cursor() # queries for retrievint all rows retrieve = "Select AVG(Marks) AS average from students;" # executing the queries cursor.execute(retrieve) rows = cursor.fetchall() for i in rows: print("Average marks is :" + str(i[0])) # committing the connection then closing it. connection.commit() connection.close() Output: Output of Marks Column Average of Students Table. Create Quiz Comment S saksham_kapoor Follow 2 Improve S saksham_kapoor Follow 2 Improve Article Tags : Technical Scripter Python Technical Scripter 2020 Python-mySQL Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like