Skip to content
Advertisement

How would one return the students that are below the average in SQL query

For my assignment, we have to return the count of students that are below the average of the average where students are in section_id 86 and the grade_type_code is ‘FI’ I feel like I have it structured right because I’m able to return all the results just I can not figure out how to have them be less than or below what the average is. Here is what I have thanks!

EDIT: Have to use subqueries to achieve this goal.

Advertisement

Answer

Use AVG as an analytic function so that you don’t have to query the table twice:

Or, if you must use two queries:

Which, for the sample data:

Then both queries output:

| NUMBER_BELOW_AVERAGE |
| -------------------: |
|                    5 |

db<>fiddle here

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement