Final challenge: the marks report
After this: Build a report of averages per student, keep the good ones, and explain who is missing and why.
The head teacher wants a marks report: each student's average, the students who average 75 or more, and the students with the largest totals. This challenge builds it in five steps, and each step is scored on its own. You will predict an average that has a missing mark in it, write the line that keeps the good students, explain why one student is missing, fix a query that SQLite refuses, and predict the top two totals. Every step uses the school tables from the lessons. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 5Average per student
Average per student
Student 3, Chloe, has 85 in Maths and no mark yet (NULL) in History. In this report, what is her average?
SELECT student_id, ROUND(AVG(mark), 1) AS averageFROM enrolmentsGROUP BY student_idORDER BY student_id;Keep the good students
Complete the query, so that it keeps only the students whose average mark is 75 or more, best first. It should give the two rows shown.
SELECT student_id, COUNT(*) AS courses, ROUND(AVG(mark), 1) AS averageFROM enrolmentsGROUP BY student_id______________________ -- ← only averages of 75 or moreORDER BY average DESC;Output2 rows
| student_id | courses | average |
|---|---|---|
| 6 | 1 | 91 |
| 3 | 2 | 85 |
Who is missing
Eve is student 5. She is in the students table, but neither report above has a row for her. Why?
Fix the query
This query should list the students who average 75 or more, but SQLite refuses it: misuse of aggregate: AVG(). Which change fixes it?
SELECT student_id, AVG(mark)FROM enrolmentsWHERE AVG(mark) >= 75GROUP BY student_id;The top two totals
Each student's marks added up, largest first, and only two rows. What does this query give back?
SELECT student_id, SUM(mark) AS totalFROM enrolmentsGROUP BY student_idORDER BY total DESCLIMIT 2;- Build a report of one row per student, keep the groups that pass a test with HAVING, explain why a student with no rows is missing, fix an aggregate placed in WHERE, and find the largest totals.