Aggregate functions

EasySQLite Programs · 9 of 20

Write a Python program to get the count, average, minimum and maximum marks.

Advertisement
Output
(4, 79.5, 65, 91)

Approach

  1. COUNT, AVG, MIN, MAX in one query.

Solution

Python
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.

More sqlite programs

← All 20 SQLite Programs

Leave a Comment