简体   繁体   中英

Avg value using subquery

I want to make a query that gives me the max and min average mark of students. I can get the max and min marks, but I don't know how to caluclate average of those.

SELECT MAX(mark) AS Max_mark FROM passed 
GROUP BY student_id;

This gives the max mark from every student, I need average of that values.

SELECT student_id, (MAX(mark) + MIN(mark)) / 2 AS Avg_mark FROM passed 
GROUP BY student_id;

Or are you looking for average

SELECT student_id, AVG(mark) as Avg_mark FROM passed 
GROUP BY student_id;

Use AVG to get the avarage per student. Use MIN and MAX on this to get the highest and lowest avararage over all students.

select 
  min(avg_mark) as min_avg_mark,
  max(avg_mark) as max_avg_mark
from
(
  select avg(mark) as avg_mark
  from passed 
  group by student_id
) as avg_marks;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM