Sql語句查詢成績大全(Mysql,sqlserver,oracle)常遇筆試題


 

數據結構:



 Mysql

-- 查詢各個學生的總分 由高到低排序
select name, sum(score) as Totalscore from student group by name ORDER BY Totalscore DESC;
-- 平均分低於80分的學生及各科成績
select name, 
sum(case WHEN course='語文' then score ELSE 0 END)  語文,
sum(case WHEN course='數學' then score ELSE 0 END) 數學,

avg(score) as Totalscore from student group by name HAVING avg(score)<80;
-- 各科平均分
select course, avg(score) as Totalscore from student group by course;
-- 刪除總成績最高的學生 此句在mysql中不支持 請參考Sqlserver
--  delete from student where name in(SELECT name FROM (select name, avg(score) as Totalscore from student group by name) as t ORDER BY t.Totalscore DESC limit 1); -- 語文大於80分的 減五分 此句在mysql中不支持 請參考Sqlserver
 update student set score=score-5  
  WHERE  name (
 select name from student  where score>80 and course='語文'  
 ) AND  course='語文'

-- 某一位學生的某一門成績排名
select name,course,score,
(select count(*) from student t1 where course ='數學' and t1.score > t2.score)+1 as 名次 from student t2 where course ='數學' and name = '學渣許老師兒' order by score desc;
-- 統計
select name ,
sum(case WHEN course='語文' then score ELSE 0 END)  語文,
sum(case WHEN course='數學' then score ELSE 0 END) 數學,
sum(score) 總成績,
avg(score) 平均分 from student  group by name;

-- 每門課都大於80分的學生
select distinct name from student where name not in (select distinct name from student where score<=80);

--統計
 select course 課程,sum(case when score between 0 and 59 then 1 else 0 end) as  低於60不及格(個),
 sum(case when score between 60 and 80 then 1 else 0 end) as 60至80良(個),
 sum(case when score between 81 and 100 then 1 else 0 end) as 80值100優秀(個) from student
 group by course; 
   

 

 

Sqlserver

-- 查詢各個學生的總分 由高到低排序
select name, sum(score) as Totalscore from student group by name ORDER BY Totalscore DESC;

-- 平均分低於80分的學生及各科成績
select name,sum(case WHEN course='語文' then score ELSE 0 END)  語文,
sum(case WHEN course='數學' then score ELSE 0 END) 數學, avg(score) as Totalscore from student group by name  HAVING avg(score)<80;

-- 各科平均分
select course, avg(score) as Totalscore from student group by course;

-- 刪除總成績最高的學生
delete from student where name in
(SELECT TOP 1 name FROM (select name, avg(score) as Totalscore from student group by name) as t ORDER BY t.Totalscore DESC); -- 語文大於80分的 減五分 update student set score=score-5 WHERE name in ( select name from student where score>80 and course='語文' ) AND course='語文' -- 查詢某一位學生的某一門成績排名 select name,course,score,
(select count(*) from student t1 where course ='數學' and t1.score > t2.score)+1 as 名次 from student t2 where course ='數學' and name = '學渣許老師兒' order by score desc; -- 統計 select name , sum(case WHEN course='語文' then score ELSE 0 END) 語文, sum(case WHEN course='數學' then score ELSE 0 END) 數學, sum(score) 總成績, avg(score) 平均分 from student group by name; -- 每門課都大於80分的學生 select distinct name from student where name not in (select distinct name from student where score<=80); --統計 select course as 課程,sum(case when score between 0 and 59 then 1 else 0 end) as 低於60不及格(個), sum(case when score between 60 and 80 then 1 else 0 end) as 60至80良(個), sum(case when score between 81 and 100 then 1 else 0 end) as 80值100優秀(個) from student group by course; -- 統計是否及 SELECT course as 課程, SUM(CASE WHEN score>=60 THEN 1 ELSE 0 END) as 及格, SUM(CASE WHEN score>=60 THEN 0 ELSE 1 END) as 不及格 FROM student GROUP BY course; 

Oracle

-- 查詢各個學生的總分 由高到低排序
select "name",sum("score") as Totalscore from "student" group by "name" ORDER BY Totalscore DESC;

-- 平均分低於80分的學生及各科成績
select "name", 
sum(case WHEN "course"='語文' then "score" ELSE 0 END)  語文,
sum(case WHEN "course"='數學' then "score" ELSE 0 END) 數學, 
avg("score") as average from  "student" group by "name"  HAVING avg("score")<80;

-- 各科平均分
select "course", avg("score") as  average from "student" group by "course";

-- 刪除總成績最高的學生
delete from "student" where "name" in(
SELECT "name" FROM(select "name", avg("score") as Totalscore from 
"student" group by "name" ORDER BY Totalscore DESC) where rownum = 1 ); -- 語文大於80分的 減五分 update "student" set "score"="score"-5 WHERE "name" in ( select "name" from "student" where "score">80 and "course"='語文' ) AND "course"='語文' -- 查詢某一位學生的某一門成績排名 select "name","course","score",(select count(*) from "student" t1 where "course" ='數學' and t1."score" > t2."score")+1 as 名次 from "student" t2 where "course" ='數學' and "name" = '學渣許老師兒' order by "score" desc; -- 統計 select "name" , sum(case WHEN "course"='語文' then "score" ELSE 0 END) 語文, sum(case WHEN "course"='數學' then "score" ELSE 0 END) 數學, sum("score") 總成績, avg("score") 平均分 from "student" group by "name"; -- 每門課都大於80分的學生 select distinct "name" from "student" where "name" not in (select distinct "name" from "student" where "score"<=80); -- 統計是否及格 SELECT "course" as 課程, SUM(CASE WHEN "score">=60 THEN 1 ELSE 0 END) as 及格, SUM(CASE WHEN "score">=60 THEN 0 ELSE 1 END) as 不及格 FROM "SYSTEM"."student" GROUP BY "course";

 

 查詢語句大體一致  分為三塊只為方便查看 練習使用 未做優化 如發現問題 勞煩指教


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM