❶ 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
❷ 怎麼用sql語句做加法得到學生的總成績
select學生.學號as姓名,sum(成績.分數)as總分from學生leftjoin成績on成績.學號=學生.學號groupby學生.學號完全手打,若有疑問直接留言,我會持續關注的,保證好評率~
❸ SQL按學號查詢每人的各科總成績,感激不盡
SELECT A.XH,A.XM,SUM(B.KSCJ) AS ZF,AVG(B.KSCJ) AS PJF
FROM STU_INFO A,XK B
WHERE A.XH=B.XH
AND B.KKNY='20011'
GROUP BY A.XH,A.XM
❹ 查詢每位同學的課程門數、總成績、平均成績」的SQL語句是什麼
SQL語句如下:
SELECT 學號, Count(課程編號) AS 課程總數, Sum(成績) AS 總分數, Avg(成績) AS 平均分
FROM 成績表
GROUP BY 學號;
SQL常用操作語句如下:
選擇:select * from table1 where 范圍
插入:insert into table1(field1,field2) values(value1,value2)
刪除:delete from table1 where 范圍
更新:update table1 set field1=value1 where 范圍
查找:select * from table1 where field1 like 』%value1%』
排序:select * from table1 order by field1,field2 [desc]
總數:select count as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1
❺ SQL求選修課程在兩門以上並且都及格的學生號及總平均分
我只會oracle的寫法,其他資料庫可能會有所區別,請注意。而且就算這種寫法,因為無法實驗可能也有些出入,應該需要調試
select 學生號,avg(分數) from table where 分數>=60 group by 學生號 having count(*)>1
分數>60保證及格,這里首先查詢的是及格的學生,及格的學生如果count(*)>1,那麼他的選課一定是兩門或者兩門以上(我假定的選課表是一個學生一個課程一行)。不過這里有一個問題,假設一個學生選了三門課程,兩門及格一門不及格,那麼是不是需要統計,按照你的需求
「兩門以上並且都及格」,現在這個語句好像與你的要求不是那麼符合。
所以還有一個版本
select table.學生號,avg(table.分數) 平均分 from table,(select 學生號,count(*) 選課數 from table group by 學生號 having 選課數>1)a where table.分數>60 group by 學生號 having table.學生號=a.學生號 and a.選課數=count(*)
❻ 按照人名查出學生的各科成績以及總成績並按總成績排名的sql語句
按照人名查出學生的各科成績以及總成績並按總成績排名的sql語句示例如下:
selectA.name ,
(selectB.scorefromtable_scoreBwhereB.type='數學'andA.id=B.id) as數學 ,
(selectB.scorefromtable_scoreBwhereB.type='語文'andA.id=B.id) as語文,
(selectB.scorefromtable_scoreBwhereB.type='英語'andA.id=B.id)as英語,
(selectSUM(B.score)fromtable_scoreBwhereA.id=B.id)assum_score
fromtable_studentAorderbysum_scoreDESC
以上sql語句首先把學生表和成績表聯合查出每個學生的數學、語文、英語成績,然後通過selectSUM(B.score)fromtable_scoreBwhereA.id=B.id查出每個學生的總成績。
最後orderbysum_scoreDESC實現按總成績倒敘排列。
(6)sql選課考試總成績擴展閱讀
上述sql語句重點是對as關鍵字的使用- Alias(別名),通過使用 SQL,可以為列名稱和表名稱指定別名(Alias)。
表的 SQL Alias 語法
SELECT column_name(s) FROM table_name AS alias_name;
列的 SQL Alias 語法
SELECT column_name AS alias_name FROM table_name;
Alias 實例: 使用表名稱別名
假設我們有兩個表分別是:"Persons" 和 "Proct_Orders"。我們分別為它們指定別名 "p" 和 "po"。
現在,我們希望列出 "John Adams" 的所有定單。
我們可以使用下面的 SELECT 語句:
SELECT po.OrderID, p.LastName, p.FirstName FROM Persons AS p, Proct_Orders AS poWHERE p.LastName='Adams' AND p.FirstName='John'
❼ SQL 統計每個學生的選課門數和考試總成績,求各位大大指教
selecta.姓名.count(c.課程號)as選課門數,sum(c.成績)as總成績from學生表a,課程表b,成績表cwherea.學號=c.學號andb.課程號=c.課程號groupbya.姓名
❽ 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語句查詢各選修課程的最高分、最低分、平均分和選課人數。
SELECT CNAME,CTEACHER, 最高分, 最低分,平均分, 選課人數
FROM
( SELECT MAX(SCGRADE) AS 最高分,
MIN(SCGRADE) AS 最低分,
AVG(SCGRADE) AS 平均分,
COUNT(SNO) AS 選課人數
ROM C
ROUP BY CNO
) AS z