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