How to Compute the Average of a Column of a MySQL Table Using Python? Last Updated : 28 Oct, 2021 Comments Improve Suggest changes Like Article 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. Comment More infoAdvertise with us Next Article How to Compute the Average of a Column of a MySQL Table Using Python? S saksham_kapoor Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2020 Python-mySQL Practice Tags : python Similar Reads How to Compute the Sum of All Rows of a Column of a MySQL Table Using Python? MySQL server is an open-source relational database management system that is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a w 2 min read How to Get the Minimum and maximum Value of a Column of a MySQL Table Using Python? Prerequisite: Python: MySQL Create Table In this article, we are going to see how to get the Minimum and Maximum Value of a Column of a MySQL Table Using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database 2 min read How to Perform Arithmetic Across Columns of a MySQL Table Using Python? Python is a dynamic language, and Python applications can be integrated with database servers. The module used to access a MySQL database from Python is MySQL Connector Python. PyMySQL, MySQLDB and mysqlclient are other Python modules to communicate with a MySQL database server in Python. However, w 5 min read How to Add a Column to a MySQL Table in Python? Prerequisite: Python: MySQL Create Table Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in python for communicating with a MySQL database. Â ALTER statemen 4 min read How to Count the Number of Rows of a Given SQLite Table using Python? In this article, we will discuss how we can count the number of rows of a given SQLite Table using Python. We will be using the cursor_obj.fetchall() method to do the same. This method fetches all the rows of a query result. It returns all the rows as a list of tuples. An empty list is returned if t 2 min read Like