Average Score per Student Group
UptoSkills Arena needs to understand student performance. We have a record of student scores and their admission details. We want to identify groups of students who have achieved an average score above 75, considering groups based on the first letter of their name and their admission year.
Tables:
students:student_id(integer, PK),name(varchar(100)),admission_year(integer)scores:score_id(integer, PK),student_id(integer, FK),score(integer)
Return the first letter of the student's name, the admission year, and the calculated average score for groups where the average score is strictly greater than 75. Order the results first by the first letter of the name (alphabetically), then by the admission year (ascending).
Handle NULLs: Assume name and admission_year in students are NOT NULL. score in scores is NOT NULL. Ties in average score within a group are not a concern as we only care about the average. Empty results are possible if no groups meet the criteria.
Tables
CREATE TABLE students ( student_id integer PRIMARY KEY, name varchar(100) NOT NULL, admission_year integer NOT NULL ); CREATE TABLE scores ( score_id integer PRIMARY KEY, student_id integer NOT NULL REFERENCES students(student_id), score integer NOT NULL );
Example data
INSERT INTO students (student_id, name, admission_year) VALUES (1, 'Alice', 2023), (2, 'Bob', 2023), (3, 'Charlie', 2023), (4, 'David', 2024), (5, 'Eve', 2024), (6, 'Frank', 2024), (7, 'Grace', 2023), (8, 'Heidi', 2024); INSERT INTO scores (score_id, student_id, score) VALUES (1, 1, 85), (2, 1, 90), (3, 2, 70), (4, 2, 72), (5, 3, 95), (6, 3, 88), (7, 4, 80), (8, 4, 82), (9, 5, 65), (10, 5, 68), (11, 6, 78), (12, 6, 81), (13, 7, 88), (14, 7, 92), (15, 8, 76), (16, 8, 79);
Your query is also checked on hidden datasets with different data.
Hints
Similar problems
- Internship Offers by Company and DepartmentMedium
- Internship Performance by DepartmentMedium
- CTE: Top Employee Performance RankingMedium
- Students with Above Average GPA in Their DepartmentMedium
- Intern Stipend by Department and Start DateMedium
- Find Interns with Above Average SalaryMedium
More in: SQL and Databases problems
-- Calculate the average score per student group
SELECT
LEFT(s.name, 1) AS first_letter,
s.admission_year,
AVG(sc.score) AS average_score
FROM
students s
JOIN
scores sc ON s.student_id = sc.student_id
GROUP BY
first_letter,
s.admission_year
Your query runs on the example tables shown with the problem, then on hidden datasets when you submit.
Write standard SQL. MySQL habits work too: IFNULL, IF, YEAR(), DATE_FORMAT, GROUP_CONCAT, `backticks` and LIMIT 5, 10. It runs on PostgreSQL, so integer division keeps whole numbers (5 / 2 = 2): write 5.0 / 2 for 2.5.