Databases with SQLite · 14 / 15 Exercise Hard

Statistics per subject

Objective

Create a Python program that computes statistics per subject with AVG, MAX, MIN and GROUP BY, and discards small groups with HAVING. Use an in-memory database with the sqlite3 module (sqlite3.connect(":memory:")).

Instructions

  • Create the table grades with subject (text) and grade (integer). Read n lines with subject grade and insert them.
  • Get, per subject, the average, the highest and the lowest grade, but only if it has 2 or more grades (HAVING COUNT(*) >= 2). Sort by average descending and, on ties, by subject.
  • Show each row as subject: average m, max x, min y (the average with 2 decimals).
  • If no subject meets the condition, show No subjects with 2 or more grades.
Input
6
mates 8
lengua 5
mates 6
fisica 9
lengua 7
mates 10
Output
mates: average 8.00, max 10, min 6
lengua: average 6.00, max 7, min 5

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise