Average Marks per Student and Subject
Updated
Problem statement
A school records exam results in two tables. Each row of student is one mark: the student's student_id, name and class, the subject_id the mark belongs to, and the marks scored. A student who took several tests in a subject has several rows for it. The subject table gives the subject_name of each subject_id.
Write a query that finds every student's average mark in each subject they were graded in. Return student_id, name, subject_name and avg_marks, the average rounded to 2 decimal places.
A NULL in marks is a test that has not been graded yet and does not count toward the average. If a student has no graded mark at all in a subject, leave that student and subject out. Return the rows in any order.
Table schema
Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.
subject
One row per subject.
| Column | Type | Nullable | Description |
|---|---|---|---|
| subject_idPK | Integer | No | Unique subject ID. |
| subject_name | Text | No | The subject's name. |
student
One row per mark: a student, a subject and the score on one test.
| Column | Type | Nullable | Description |
|---|---|---|---|
| student_id | Integer | No | The student's ID; repeats on each of their rows. |
| name | Text | No | The student's name. |
| class | Text | No | The student's class. |
| subject_id | Integer | No | The subject this mark is for. |
| marks | Integer | Yes | Score from 0 to 100, or NULL if not graded yet. |
Foreign key: subject_id → subject(subject_id)
Expected result
Your query or function must return these columns.
| Column | Type | Nullable | Description |
|---|---|---|---|
| student_id | Integer | No | — |
| name | Text | No | — |
| subject_name | Text | No | — |
| avg_marks | Decimal | No | — |
Row order: any order is accepted. Numeric tolerance: 0.
Constraints
- The
subjecttable has between1and50rows, andsubject_idis unique. Thestudenttable has between0and1000rows. - Every
student.subject_idappears insubject. marksis an integer from0to100, orNULL. No other column is everNULL.- All rows with the same
student_idhave the samenameandclass. Two different students can have the same name. - A student has at most
20rows for one subject. subject_namevalues are unique, and no two names in a column differ only in letter case.- No average lands exactly halfway between two hundredths, so any usual rounding gives the same result.
More Bank of America Onsite Interview questions
- Centered Star PyramidONSITE INTERVIEW · Seen Nov 2025
- Employees Without a ManagerONSITE INTERVIEW · Seen Nov 2025
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2025
- N-th Fibonacci NumberONSITE INTERVIEW · Seen Nov 2025
- Reverse Each Word in a SentenceONSITE INTERVIEW · Seen Nov 2025
- Second Highest SalaryONSITE INTERVIEW · Seen Nov 2025
- Three Numbers That Add Up to a TargetONSITE INTERVIEW · Seen Apr 2025
- Duplicate RowsONSITE INTERVIEW · Seen Oct 2023
- Factorial of a NumberONSITE INTERVIEW · Seen Oct 2023
- Palindrome String CheckONSITE INTERVIEW · Seen Sep 2023
Similar questions at other companies
- Delete Duplicate Employee RecordsInfosys · ONSITE INTERVIEWInfosys interview questions
- Records Created or Updated in a Time WindowPelatro · ONSITE INTERVIEW
- Top Ten Employee SalariesSolytics Partners · ONSITE INTERVIEW
- Latest New JobSphere · ONSITE INTERVIEW
- Employees Whose Manager Left the CompanyZS · ONSITE INTERVIEWZS interview questions
- Employees Above Average SalaryJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- Retrieve Every Student RecordLSEG · ONSITE INTERVIEWLSEG interview questions
- Combine Winner and Loser NamesAmazon · ONSITE INTERVIEW
- Conditionally Insert or Update RecordsKlaviyo · ONSITE INTERVIEWKlaviyo interview questions
- Employees with Matching DepartmentsAMD · ONSITE INTERVIEWAMD interview questions