How to Compute the Sum of All Rows of a Column of a MySQL Table Using Python? Last Updated : 26 Nov, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 web server, we use various modules in Python such as PyMySQL, mysql.connector, etc. In this article, we are going to Compute the Sum of All Rows of a Column in a specific MySQL table in a Database. First, we are going to connect to a database having a MySQL table. The SQL query that is going to be used is: SELECT SUM(column_name) FROM table_name And finally, display the sum of rows in the table. Below are some programs which depict how to compute the sum of all rows of a column of a MySQL table in a Database: Example 1 Below is the table student in database gfg which is going to be accessed by a Python script: Below is the program to get the sum of rows of a particular column in a MySQL table: Python3 # import required module import mysql.connector # connect python with mysql with your hostname, # database, user and password db = mysql.connector.connect(host='localhost', database='gfg', user='root', password='') # create cursor object cursor = db.cursor() # get the sum of rows of a column cursor.execute("SELECT SUM(Marks) FROM student") # fetch sum and display it print(cursor.fetchall()[0][0]) # terminate connection db.close() Output: Example 2 Here is another example to get the sum of rows from a table in a given database, below is the table schema and rows: Below is the python script to get row sum of members from the table club: Python3 # import required module import mysql.connector # connect python with mysql with your hostname, # database, user and password db = mysql.connector.connect(host='localhost', database='gfg', user='root', password='') # create cursor object cursor = db.cursor() # get the sum of rows of a column cursor.execute("SELECT SUM(members) FROM club") # fetch sum and display it print(cursor.fetchall()[0][0]) # terminate connection db.close() Output: Comment More infoAdvertise with us Next Article How to Compute the Average of a Column of a MySQL Table Using Python? J jeetparmarjeet01 Follow Improve Article Tags : Python Python-mySQL Practice Tags : python Similar Reads How to Compute the Average of a Column of a MySQL Table Using Python? 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 2 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 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 Concatenate Column Values of a MySQL Table Using Python? Prerequisite: Python: MySQL Create Table In this article, we show how to concatenate column values of a MySQL table using Python. We use various data types in SQL Server to define data in a particular column appropriately. We might have requirements to concatenate data from multiple columns into a s 2 min read How to Count the Number of Rows in a MySQL Table in Python? MySQL server is an open-source relational database management system which 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 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 Like