Students with Above Average GPA in Their Department
At UptoSkills Arena, we want to identify students who are performing exceptionally well in their academic programs. We need a list of students whose Grade Point Average (GPA) is higher than the average GPA of all students within their department.
Studentstable:student_id(integer, primary key)name(varchar(100))department(varchar(50))gpa(numeric(3,2))
Return the student_id, name, and department for these high-performing students. The results should be ordered by department and then by student_id.
Handle ties by including all students whose GPA equals the calculated department average plus a small epsilon (effectively greater than the average). NULL GPAs should be ignored in calculations. If a department has no students with a non-NULL GPA, it should not appear in the results.
Tables
CREATE TABLE Students ( student_id integer PRIMARY KEY, name varchar(100) NOT NULL, department varchar(50) NOT NULL, gpa numeric(3,2) );
Example data
INSERT INTO Students (student_id, name, department, gpa) VALUES (1, 'Alice', 'Computer Science', 3.8), (2, 'Bob', 'Computer Science', 3.5), (3, 'Charlie', 'Computer Science', 3.9), (4, 'David', 'Electrical Engineering', 3.6), (5, 'Eve', 'Electrical Engineering', 3.4), (6, 'Frank', 'Mechanical Engineering', 3.7), (7, 'Grace', 'Mechanical Engineering', 3.7), (8, 'Heidi', 'Physics', NULL);
Your query is also checked on hidden datasets with different data.
Hints
Similar problems
- Find Interns with Above Average SalaryMedium
- Find Students with Above-Average GPA in Their DepartmentMedium
- Find Departments with Above Average Salary SpendingMedium
- Internship Offers by Department and SalaryMedium
- Average Score per Student GroupMedium
- Internship Performance by DepartmentMedium
More in: SQL and Databases problems
-- Find students with above-average GPA in their department
SELECT
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.