Sum() function
sum() function returns the sum of all values of the column, in case all values are null , it returns null. so, total() function is a comparatively better function.
Syntax: sum(name_of_the_column)
Python3
# import the sqlite module import sqlite3 # establishing a connection to the database connection = sqlite3.connect( "sales.db" ) # creating a cursor object cursor = connection.cursor() # sum of all the yearly sale sum_yearly_sale = "select sum(yearly_sale) from sales1" cursor.execute(sum_yearly_sale) # Print the sum of scores print ( "The sum of yearly sale is :" ) print (cursor.fetchone()[ 0 ]) # Closing database connection connection.close() |
Output:
The sum of yearly sale is : 265767.0
Using SQLite Aggregate functions in Python
In this article, we are going to see how to use the aggregate function in SQLite Python. An aggregate function is a database management function that groups the values of numerous rows into a single summary value. Average (i.e., arithmetic mean), sum, max, min, Count are common aggregation functions. SQLite provides us with many aggregate functions used for statistical analysis.
Database for demonstration: To download the database click here.