① sql語句查詢成績最高的學生
select * from 學生表 where 學生id in (select 學生id from 分數表 where 分數值 = (select Max(分數欄位) from 分數表)),這樣查詢即可。
② 請問SQL 查詢出成績最高分
請問SQL 查詢出成績最高分
select 姓名,課程名,成績 from tablename where 成績=(select max(成績) from tablename)
③ 用SQL選出每個人成績的最高紀錄
查詢每個人最高成績SQL:
第一種:先使用group by和max得到最高分數和學科,然後再查詢一下score表,找到學科和分數都相同的記錄
select b.* from (select max(score) t,course from score group by course) a,score b where a.t=b.score and a.course=b.course
第二種:先得到相同學科的最高分數,再查詢score表,找到最高分數的記錄select * from score a where score=(select max(score) from score where course=a.course)
第三種:score表中,當學科一樣的時候,不存在一條記錄的分數小於其它記錄的分數select * from score a where not exists(select * from score where a.course=course and a.score<score)
④ sql中,表table三個欄位(編號,學號,成績),每個學生考了多次,我怎麼找出每名學生的最高分,寫sql語句
select 學號, 最高分=max(成績)
from table
group by 學號
先按學號對記錄進行分組,使每個學生的多條記錄在一組,然後求每一組中成績的最大值即可。
⑤ 1查詢成績表的總分數,平均分,最低分和最高分。用sql語句怎麼寫
---1. 計算每個人的總成績並排名(要求顯示欄位:姓名,總成績)
select name,sum(cast(score as bigint)) as allscore from stuscore group by name order by allscore desc
---2. 計算每個人的總成績並排名(要求顯示欄位: 學號,姓名,總成績)
select stuid,name,sum(cast(score as bigint)) as allscore from stuscore group by stuid,name order by allscore desc
---3. 計算每個人單科的最高成績(要求顯示欄位: 學號,姓名,課程,最高成績)
SELECT t1.stuid,t1.name,t1.subject,t1.score from stuscore t1,(SELECT stuid,max(score) as maxscore from stuscore group by stuid) t2 where t1.stuid=t2.stuid and t1.score=t2.maxscore
---4. 計算每個人的平均成績(要求顯示欄位: 學號,姓名,平均成績)
select distinct t1.stuid,t1.name,t2.avgscore from stuscore t1,(select stuid,avg(cast(score as bigint)) as avgscore from stuscore group by stuid) t2 where t1.stuid=t2.stuid
---5. 列出各門課程成績最好的學生(要求顯示欄位: 學號,姓名,科目,成績)
select t1.stuid,t1.name,t1.subject,t2.maxscore from stuscore t1,(select subject,max(score) as maxscore from stuscore group by subject) t2 where t1.subject=t2.subject and t1.score=t2.maxscore
---6. 列出各門課程成績最好的兩位學生(要求顯示欄位: 學號,姓名,科目,成績)
select distinct t1.* from stuscore t1 where t1.stuid in(select top 2 stuscore.stuid from stuscore where subject = t1.subject order by score desc)order by t1.subject
---7. 統計報表(要求顯示欄位: 學號,姓名,各科成績,總分,平均成績)
select stuid as 學號,name as 姓名,sum(case when subject='語文' then score else 0 end) as 語文,sum(case when subject='數學' then score else 0 end) as 數學,sum(case when subject='英語' then score else 0 end) as 英語,sum(cast(score as bigint)) as 總分,(sum(cast(score as bigint))/count(*)) as 平均分 from stuscore group by stuid,name order by 總分 desc
---8. 列出各門課程的平均成績(要求顯示欄位:課程,平均成績)
select subject,avg(cast(score as bigint)) as avgscore from stuscore group by subject
---9. 列出數學成績的排名(要求顯示欄位:學號,姓名,成績,排名)
select * from stuscore where subject ='數學' order by score desc
---10. 列出數學成績在2-3名的學生(要求顯示欄位:學號,姓名,科目,成績)
select t3.* from(select top 2 t2.* from (select top 3 name,subject,score,stuid from stuscore where subject='數學' order by score desc) t2 order by t2.score) t3 order by t3.score desc
---11. 求出李四的數學成績的排名
declare @tmp table(pm int,name varchar(50),score int,stuid int)
insert into @tmp select null,name,score,stuid from stuscore where subject='數學' order by score desc
declare @id int
set @id=0;
update @tmp set @id=@id+1,pm=@id
select * from @tmp where name='李四'
---12. 統計各科目及格人數
select subject,
(select count(*) from stuscore where score<60 and subject=t1.subject) as 不及格,
(select count(*) from stuscore where score between 60 and 80 and subject=t1.subject) as 良,
(select count(*) from stuscore where score >80 and subject=t1.subject) as 優
from stuscore t1 group by subject
---13.統計如下:數學:張三(50分),李四(90分),王五(90分),趙六(76分)
declare @s varchar(1000)
set @s=''
select @s =@s+','+name+'('+convert(varchar(10),score)+'分)' from stuscore where subject='數學'
set @s=stuff(@s,1,1,'')
print '數學:'+@s
⑥ sql求各科成績最高分
可以使用MAX函數:求最大值
SELECT 科目,MAX(分數)
FROM 表名
GROUP BY 科目
⑦ 怎麼用SQL語句,查詢考試年份為2004年的外語考試的最高分、最低分、平均分
select max(分數),min(分數),avg(分數) from 考試表 where year(考試時間)=2004 and 科目='外語'
⑧ 在SQL語句中怎麼查詢一個科目的最高分和最低分還有平均分
select max(科目) as '最高分',min(科目) as 最低分,round(avg(科目),2) as '平均分' from 表
round(avg(科目),2) 意思是平均分保留兩位小數,因為在多個科目中就可以出現小數
如果有一科或多科沒有成績使用avg就不正確,應該用以下語句:
select max(科目) as '最高分',min(科目) as 最低分,round(sum(科目)/科目數,2) as '平均分' from 表
因為avg有效果是對已有數據的統計平均。
⑨ 1查詢成績表的總分數,平均分,最低分和最高分。用sql語句怎麼寫
1. 計算每個人的總成績並排名(要求顯示欄位:姓名,總成績)
select name,sum(cast(score as bigint)) as allscore from stuscore group by name order by allscore desc
2. 計算每個人的總成績並排名(要求顯示欄位: 學號,姓名,總成績)
select stuid,name,sum(cast(score as bigint)) as allscore from stuscore group by stuid,name order by allscore desc
3. 計算每個人單科的最高成績(要求顯示欄位: 學號,姓名,課程,最高成績)
SELECT t1.stuid,t1.name,t1.subject,t1.score from stuscore t1,(SELECT stuid,max(score) as maxscore from stuscore group by stuid) t2 where t1.stuid=t2.stuid and t1.score=t2.maxscore
4. 計算每個人的平均成績(要求顯示欄位: 學號,姓名,平均成績)
select distinct t1.stuid,t1.name,t2.avgscore from stuscore t1,(select stuid,avg(cast(score as bigint)) as avgscore from stuscore group by stuid) t2 where t1.stuid=t2.stuid