Find Top Scoring Students - Problem

You're working as a data analyst for a university's academic excellence program. The program wants to identify outstanding students who have not only completed all required courses in their major but have also achieved perfect grades (A) in every single one of them.

Given three database tables:

  • students - Contains student information including their major
  • courses - Contains course information organized by major
  • enrollments - Contains enrollment records with grades

Your task: Write a SQL query to find students who have taken all courses offered in their major and received an 'A' grade in every course.

This is a classic "division" problem in relational algebra - finding entities that are related to all entities in another set. Think of it as finding students who have a perfect academic record in their field of study.

Return the results ordered by student_id in ascending order.

Input & Output

example_1.sql โ€” Basic Case
$ Input: students: [(1,'Alice','Computer Science'), (2,'Bob','Computer Science')] courses: [(101,'Algorithms',3,'Computer Science'), (102,'Data Structures',3,'Computer Science')] enrollments: [(1,101,'Fall 2023','A'), (1,102,'Fall 2023','A'), (2,101,'Fall 2023','B'), (2,102,'Fall 2023','A')]
โ€บ Output: [1]
๐Ÿ’ก Note: Alice (ID 1) completed both CS courses with A grades. Bob (ID 2) got a B in Algorithms, so he doesn't qualify.
example_2.sql โ€” Multiple Majors
$ Input: students: [(3,'Charlie','Mathematics'), (4,'David','Mathematics')] courses: [(103,'Calculus',4,'Mathematics'), (104,'Linear Algebra',4,'Mathematics')] enrollments: [(3,103,'Fall 2023','A'), (3,104,'Fall 2023','A'), (4,103,'Fall 2023','A'), (4,104,'Fall 2023','B')]
โ€บ Output: [3]
๐Ÿ’ก Note: Charlie (ID 3) aced both Math courses. David (ID 4) got a B in Linear Algebra, disqualifying him.
example_3.sql โ€” Incomplete Records
$ Input: students: [(5,'Eve','Physics')] courses: [(201,'Mechanics',3,'Physics'), (202,'Thermodynamics',3,'Physics')] enrollments: [(5,201,'Fall 2023','A')]
โ€บ Output: []
๐Ÿ’ก Note: Eve only took one Physics course out of two required, so she doesn't qualify despite getting an A.

Constraints

  • 1 โ‰ค student_id โ‰ค 1000
  • 1 โ‰ค course_id โ‰ค 1000
  • Student names and course names are non-empty strings
  • Major names are consistent across tables
  • Grades are single uppercase letters (A, B, C, D, F)
  • Each student has exactly one major
  • Each course belongs to exactly one major

Visualization

Tap to expand
๐ŸŽฏ RequirementsCS: 2 coursesMath: 2 courses๐Ÿ“š CompletionAlice: 2/2 CS โœ“Charlie: 2/2 Math โœ“โญ ExcellenceAlice: All A's โœ“Charlie: All A's โœ“๐Ÿ† Honor RollStudents: 1, 3
Understanding the Visualization
1
Identify Requirements
For each major, determine all required courses (like a graduation checklist)
2
Check Completion
Verify each student took all courses in their major
3
Verify Excellence
Ensure every grade is an 'A' - no exceptions allowed
4
Award Recognition
Students meeting both criteria earn top honors
Key Takeaway
๐ŸŽฏ Key Insight: This is a classic *relational division* problem - finding students whose set of A-grade courses exactly matches their major's complete course set. The optimal solution leverages SQL's GROUP BY and HAVING clauses to elegantly verify both completeness and excellence in a single query.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
38.2K Views
Medium Frequency
~25 min Avg. Time
1.6K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen