Mysql统计按学⽣姓名分组,总成绩前⼗
mysql面试题学生表学⽣表名为:student, 单科成绩的字段为:subject。学⽣名字为:name
查询单科成绩前⼗:mysql语句为:select * from student order by subject desc limit 10;
查询总分成绩前⼗:mysql语句为:select sum(subject) subject,name from student group by name order by subject desc limit 10;
注:
select sum(subject) subject,name
这句的意思是:sum(subject) subject 把单科成绩加总⽤subject 来命名(就是总成绩),name就是⼀个字段,这⾥只有两个字段。
tvngroup by name order by subject : group by name 的意思按照名字这⼀栏来分组,当然,学⽣成绩表名字有可能是⼀样的,按照学号是最准确的,这⾥只是举个例⼦。
前端三大主流框架排名order by subject 这句的意思是按照总分成绩排序,这⾥的subject 是前⾯重命名总分的意思。
select sum(subject) as countsubject,name from student group by name order by countsubject desc limit 10;
排名
扩展资料:
学⽣成绩表常⽤sql
1. 在表中插⼊符合主键
[sql]
/*成绩表*/
foreach怎么退出循环
CREATE TABLE SC
(
Sid INT REFERENCES Student(Sid), /*学⽣学号*/
Cid INT REFERENCES Course(Cid), /*课程编号*/
Score INT NOT NULL, /*课程分数*/
PRIMARY KEY(Sid,Cid) /*将学⽣学号和课程编号设为复合主键*/ )2. 查询各科成绩最⾼分,最低分以及平均分
[sql]
SELECT c.Cname, MAX(s.Score) AS Max, MIN(s.Score) AS Min, AVG(s.Score) AS Average
maintenance statusFROM Course c JOIN SC s ON c.Cid = s.Cid
GROUP BY c.Cname
/
*此处应注意,若不按照c.Cname进⾏分组,SQL语句会报错,c.Cname在SELECT语句中不合法,因为它并未出现在聚合函数中也没有出现在GROUP BY语句中*/
3. 查询平均成绩⼤于80分的学⽣姓名以及平均成绩
[sql]
开发vb程序的一般步骤SELECT Sname, AVG(Score) AS Average FROM Student JOIN SC
ON Student.Sid=SC.Sid
GROUP BY Sname
HAVING AVG(Score)>80
/*以聚合函数为条件进⾏删选只能在HAVING语句中进⾏,WHERE语句不⽀持聚合函数*/
4. 按总分为学⽣排名,总分相同名次相同
[sql]
SELECT RANK() OVER (ORDER BY SUM(ss.Score) DESC) AS Rank, s.Sname,
ISNULL(SUM(ss.Score),0)
FROM Student s LEFT JOIN SC ss
ON s.Sid = ss.Sid
GROUP BY s.Sname
ORDER BY SUM(ss.Score) DESC
/*RANK()是SQL Server的⼀个built-in函数,语法为
RANK() OVER ( [ partition_by_clause ] order_by_clause ).*/5. 查询总分在100⾄200之间的学⽣姓名及总分
[sql]
SELECT s.Sname,SUM(ss.Score) FROM Student s JOIN SC ss ON s.Sid=ss.Sid
GROUP BY s.Sname HAVING SUM(ss.Score) BETWEEN 100 AND 200
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论