MYSQL 查詢語句(No.10) 阿新 • • 發佈:2018-12-30 --1:查詢全體學生的學號和姓名 select sno, sname from student; --2:查詢全體學生的姓名、學號和所在系 select sno, sname,sdept from student; --3: 查詢全體學生的詳細記錄 select * from student --4: 查詢全體學生的姓名及其出生年份 select sname, 2011-sage as 出生年份 from student; --5:查詢全體學生姓名、出生年份和所在系,要求用小寫字母表示所有系名 select sname, 2011-sage as 出生年份,lower(sdept) from student; --6:查詢選修了課程的學生學號 select distinct sno from sc; --7:查詢計算機系(IS)所有學生的名單 select sname from student where sdept = "is"; --8:查詢所有年齡在20以下學生的姓名和年齡 select sname ,sage from student where sage <20; --9: 查詢考試成績有不及格的學生的學號 select distinct sno from sc where grade < 60; --10: 查詢年齡在20-23 (包括20和23)之間的學生的姓名、系別和年齡 select sname,sdept,sage from student where sage>=20 and sage<=23; --11: 查詢資訊系(IS)、數學系(MA)和計算機科學系(CS)學生的姓名和性別 select sname ,ssex from student where sdept = 'is' or sdept='ma' or sdept = 'CS';或 select sname,ssex from student where sdept in("is","ma","cs"); --12: 查詢學號為95001的學生的詳細情況 select * from student where sno = 95001; --13: 查詢所有姓林的學生的姓名、學號和性別 select sname,sno,ssex from student where sname like "林%"; --14: 查詢姓“歐陽”且全名為三個漢字的學生的姓名 select sname from student where sname like "歐陽_"; --15:查詢名字中第二個字為“燕”字的學生姓名和學號 select sname,sno from student where sname like "_燕%"; --16:查詢所有不姓“劉”的學生的姓名 select sname from student where sname not like "^劉%"; --17:查詢課程名為“DB_DESIGN”的課程號的學分 select ccredit from course where cname = "db_design"; --18:查詢缺少成績的學生的學號和相應的課程號(成績欄位值為Null) select sno,cno from sc where grade<=>null; --19: 查詢所有有成績的學生的學號和課程號 select sno ,cno from sc ; --20: 查詢所有計算機系年齡在20以下的學生姓名 select sname from student where sage<20 and sdept="cs"; --21: 查詢選修了3號課程的學生的學號和成績,查詢結果按分數降序排列 select sno,grade from sc where cno =3 order by grade desc; --22: 查詢全體學生情況,查詢結果按所在系的系號升序排列,同一系中的學生按年齡降序排列 select * from student order by sdept,sage desc; --23: 查詢學生總人數 select count(sno) from student; --24: 查詢選修了課程的學生人數 select count(distinct sno) from sc; --25: 計算1號課程的學生的平均成績 select avg(grade) from sc where cno = 1; --26: 計算1號課程的學生的最高成績分數 select max(grade) from sc where cno =1; --27:求各個課程號及相應的選課人數 select distinct cno,count(sno) from sc group by cno; -- 查詢每個學生選修的的課程數 select sno,count(sno) from sc group by sno; --28: 查詢選修了三門以上課程的學生學號 select sno from sc group by sno having count(sno)>3; select sno,count(cno) as kcnum from sc group by sno having kcnum>3; --29:查詢每個學生及其選修課情況 select sno,sname,sage ,sdept ,grade from student left join sc on student.sno=sc.sno; --30:查詢每一門課的間接先行課 select cno, (select c2.cpno from course as c2 where c2.cno=c1.cpno ) as 間接先修課程 from course as c1; --31:選修2號課程且成績在90以上的學生的學號和姓名 select student.sno,sname from student,sc where student.sno = sc.sno and cno=2 and grade>=90; //join連線 select s.sno,sname from student as s join sc on s.sno =sc.sno where cno=2 and grade>=90; --32:查詢每個學生的學號、姓名、選修的課程名及成績 select sno,sc.cno,cname from sc join course using(cno); select s.sno,sname,sage,cname,grade from student as s left join(select sno,sc.cno,cname from sc join course using(cno))as t on s.sno=t.sno; --33:查詢與’林燕芳’在同一個系學習的學生姓名 select sname from student where sdept=(select sdept from student where sname="林燕芳") and sname !="林燕芳"; --34: 查詢其他系中比資訊系某一學生小的學生姓名和年齡 select sname,sage from student where sage<any(select sage from student where sdept="is") and sdept!="is"; --35:查詢所有選修了1號課程的學生的學生姓名 select student.sname from student,sc where student.sno=sc.sno and sc.cno=1; //select sname from student where sno in (select sno from sc where cno=1); --36:查詢選修了全部課程的學生姓名 select sname from student where sno in(select sno from sc group by sno having count(cno)=(select count(*) from course));