MYSQL 查询语句(No.10)
生活随笔
收集整理的這篇文章主要介紹了
MYSQL 查询语句(No.10)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
--1:查詢?nèi)w學(xué)生的學(xué)號和姓名
select sno, sname from student;
--2:查詢?nèi)w學(xué)生的姓名、學(xué)號和所在系
select sno, sname,sdept from student;
--3: 查詢?nèi)w學(xué)生的詳細(xì)記錄
select * from student
--4: 查詢?nèi)w學(xué)生的姓名及其出生年份
select sname, 2011-sage as 出生年份 from student;
--5:查詢?nèi)w學(xué)生姓名、出生年份和所在系,要求用小寫字母表示所有系名
select sname, 2011-sage as 出生年份,lower(sdept) from student;
--6:查詢選修了課程的學(xué)生學(xué)號
select distinct sno from sc;
--7:查詢計(jì)算機(jī)系(IS)所有學(xué)生的名單
select sname from student where sdept = "is";
--8:查詢所有年齡在20以下學(xué)生的姓名和年齡
select sname ,sage from student where sage <20;
--9: 查詢考試成績有不及格的學(xué)生的學(xué)號
select distinct sno from sc where grade < 60;
--10: 查詢年齡在20-23 (包括20和23)之間的學(xué)生的姓名、系別和年齡
select sname,sdept,sage from student where sage>=20 and sage<=23;
--11: 查詢信息系(IS)、數(shù)學(xué)系(MA)和計(jì)算機(jī)科學(xué)系(CS)學(xué)生的姓名和性別
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: 查詢學(xué)號為95001的學(xué)生的詳細(xì)情況
select * from student where sno = 95001;
--13: 查詢所有姓林的學(xué)生的姓名、學(xué)號和性別
select sname,sno,ssex from student where sname like "林%";
--14: 查詢姓“歐陽”且全名為三個漢字的學(xué)生的姓名
select sname from student where sname like "歐陽_";
--15:查詢名字中第二個字為“燕”字的學(xué)生姓名和學(xué)號
select sname,sno from student where sname like "_燕%";
--16:查詢所有不姓“劉”的學(xué)生的姓名
select sname from student where sname not like "^劉%";
--17:查詢課程名為“DB_DESIGN”的課程號的學(xué)分
select ccredit from course where cname = "db_design";
--18:查詢?nèi)鄙俪煽兊膶W(xué)生的學(xué)號和相應(yīng)的課程號(成績字段值為Null)
select sno,cno from sc where grade<=>null;
--19: 查詢所有有成績的學(xué)生的學(xué)號和課程號
select sno ,cno from sc ;
--20: 查詢所有計(jì)算機(jī)系年齡在20以下的學(xué)生姓名
select sname from student where sage<20 and sdept="cs";
--21: 查詢選修了3號課程的學(xué)生的學(xué)號和成績,查詢結(jié)果按分?jǐn)?shù)降序排列
select sno,grade from sc where cno =3 order by grade desc;
--22: 查詢?nèi)w學(xué)生情況,查詢結(jié)果按所在系的系號升序排列,同一系中的學(xué)生按年齡降序排列
select * from student order by sdept,sage desc;
--23: 查詢學(xué)生總?cè)藬?shù)
select count(sno) from student;
--24: 查詢選修了課程的學(xué)生人數(shù)
select count(distinct sno) from sc;
--25: 計(jì)算1號課程的學(xué)生的平均成績
select avg(grade) from sc where cno = 1;
--26: 計(jì)算1號課程的學(xué)生的最高成績分?jǐn)?shù)
select max(grade) from sc where cno =1;
--27:求各個課程號及相應(yīng)的選課人數(shù)
select distinct cno,count(sno) from sc group by cno;
-- 查詢每個學(xué)生選修的的課程數(shù)
select sno,count(sno) from sc group by sno;
--28: 查詢選修了三門以上課程的學(xué)生學(xué)號
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:查詢每個學(xué)生及其選修課情況
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以上的學(xué)生的學(xué)號和姓名
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:查詢每個學(xué)生的學(xué)號、姓名、選修的課程名及成績
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:查詢與’林燕芳’在同一個系學(xué)習(xí)的學(xué)生姓名
select sname from student where sdept=(select sdept from student where sname="林燕芳") and sname !="林燕芳";
--34: 查詢其他系中比信息系某一學(xué)生小的學(xué)生姓名和年齡
select sname,sage from student where sage<any(select sage from student where sdept="is") and sdept!="is";
--35:查詢所有選修了1號課程的學(xué)生的學(xué)生姓名
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:查詢選修了全部課程的學(xué)生姓名
select sname from student where sno in(select sno from sc group by sno having count(cno)=(select count(*) from course));
總結(jié)
以上是生活随笔為你收集整理的MYSQL 查询语句(No.10)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: v-model的实现原理
- 下一篇: 【Mysql SQLZOO练习命令练习】