Python Practice › SQLite Programs › Program 9
Aggregate functions
Write a Python program to get the count, average, minimum and maximum marks.
Output
(4, 79.5, 65, 91)
Approach
COUNT,AVG,MIN,MAXin one query.
Solution
import sqlite3 conn = sqlite3.connect(":memory:")conn.execute("CREATE TABLE students (name TEXT, marks INTEGER)")conn.executemany("INSERT INTO students VALUES (?, ?)", [("Asha", 91), ("Ravi", 74), ("Meena", 88), ("John", 65)])print(conn.execute("SELECT COUNT(*), ROUND(AVG(marks), 2), MIN(marks), MAX(marks) FROM students").fetchone())Output
(4, 79.5, 65, 91)
Learn the concept
New to this topic? Read the lesson, then come back.