To find the student number of the student with the highest average score, how to write a SQL query to associate the grades with the student table?
P粉786432579
P粉786432579 2023-08-15 15:11:01
0
1
473

这是我的学生表 stud 表

rollno name
1 A
2 B
3 C
4 B
5 D
6 C

这是我的成绩表 marks 表

rollno mar eng maths phy chem
1 40 45 38 50 50
2 28 50 45 41 38
3 41 42 43 44 45
4 45 44 43 42 41
5 33 32 42 15 41

对于以下查询:

select rollno,(mar+eng+maths+phy+chem)/5 as average from marks;

我得到的结果是:op

rollno average
1 44.6000
2 40.4000
3 43.0000
4 43.0000
5 32.6000

现在要找到平均分最高的学生的学号,我应该写什么查询?

我尝试使用

select rollno,max( select (mar+eng+maths+phy+chem)/5 from marks ) from marks;

但是给我返回了语法错误

P粉786432579
P粉786432579

reply all (1)
P粉066224086

This syntax is wrong because the subquery returns multiple rows. Only one value can be used in the max function.

This is a better way -

SELECT rollno FROM marks WHERE (mar+eng+maths+phy+chem)/5 = (SELECT MAX((mar+eng+maths+phy+chem)/5) FROM marks);
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!