Knowledge Guide
HomeDatabasesSQL Practice Problems

easy Student Course Averages

Average grade per student per course — only courses they actually took

Students, Courses, Grades(student_id, course_name, grade). Report each student's average grade in each course they're enrolled in, sorted by student_id, then course_name. The earlier solution CROSS JOINed every student against every course, producing slots for courses a student never took (a NULL average) — but there is no enrollment table, so "enrolled in" can only mean "has a grade row". The straightforward grouped join is both correct and simpler:

SELECT s.student_id, s.student_name, g.course_name,
       ROUND(AVG(g.grade), 2) AS average_grade
FROM Students s
JOIN Grades g ON g.student_id = s.student_id
GROUP BY s.student_id, s.student_name, g.course_name
ORDER BY s.student_id, g.course_name;

Traced on the sample

studentcoursegradesAVG (2dp)
1 AliceMath90, 88, 9189.67
1 AlicePhysics85, 8283.50
1 AliceProgramming9595.00
2 BobMath8585.00
2 BobProgramming8888.00
13 JohnMath/Physics/Prog78 / 80 / 9278.00 / 80.00 / 92.00

Student 6 (Alex) has no grades, so they correctly do not appear. Note Alice/Programming is 95.00 (a single grade) — averaging one value returns that value.

Pitfalls

Takeaways


Re-authored for correctness for this guide (the prior CROSS JOIN produced courses students never took, and an incorrect Programming average). See also: Aggregate Functions, INNER JOIN, GROUP BY.

🤖 Don't fully get this? Learn it with Claude

Stuck on Student Course Averages? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **Student Course Averages** (Databases). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **Student Course Averages** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **Student Course Averages**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **Student Course Averages**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes