经典SQL练习题
題目地址:http://blog.csdn.net/qaz13177_58_/article/details/5575711
1、 查詢Student表中的所有記錄的Sname、Ssex和Class列。
select sname,ssex,class from STUDENT
2、 查詢教師所有的單位即不重復的Depart列。
select depart from TEACHER group by depart
select distinct depart from teacher
3、 查詢Student表的所有記錄。
select * from student
4、 查詢Score表中成績在60到80之間的所有記錄。
select * from score where degree between 60 and 80
5、 查詢Score表中成績為85,86或88的記錄。
select * from score where degree in(85,86,88)
6、 查詢Student表中“95031”班或性別為“女”的同學記錄。
select * from student where class='95031' or ssex='女'
7、 以Class降序查詢Student表的所有記錄。
select * from student order by class desc
8、 以Cno升序、Degree降序查詢Score表的所有記錄。
select * from score order by cno asc,degree desc
9、 查詢“95031”班的學生人數。
select count(*) from student where class='95031'
10、查詢Score表中的最高分的學生學號和課程號。
select sno,cno from score where degree=(select max(degree) from score)
select sno,cno from score where degree=(select top 1 degree from score order by degree desc)
11、查詢‘3-105’號課程的平均分。
select avg(degree) from score where cno='3-105'
12、查詢Score表中至少有5名學生選修的并以3開頭的課程的平均分數。
select avg(degree) from score where cno like '3%' group by cno having count(sno)>=5
13、查詢最低分大于70,最高分小于90的Sno列。
select sno from score group by sno having min(degree)>70 and max(degree)<90
14、查詢所有學生的Sname、Cno和Degree列。
select s.SNAME,c.CNO,c.DEGREE from score c inner join student s on c.SNO=s.SNO
15、查詢所有學生的Sno、Cname和Degree列。
select s.sno,c.cname,s.degree from score s inner join course c on s.CNO=c.CNO
16、查詢所有學生的Sname、Cname和Degree列。
select st.SNAME,cu.CNAME,sc.DEGREE from student st inner join score sc on st.SNO=sc.SNO inner join course cu on sc.CNO=cu.CNO
17、查詢“95033”班所選課程的平均分。
select avg(degree) from score where sno in(select sno from STUDENT where class='95033')
SELECT AVG(A.DEGREE) FROM SCORE A JOIN STUDENT B ON A.SNO = B.SNO WHERE B.CLASS='95033'
18、假設使用如下命令建立了一個grade表:
create table grade(low int,upp int,rank char(1));
insert into grade values(90,100,'A');
insert into grade values(80,89,'B');
insert into grade values(70,79,'C');
insert into grade values(60,69,'D');
insert into grade values(0,59,'E');
現查詢所有同學的Sno、Cno和rank列。
select sno,cno,(select rank from grade where degree between low and upp) from score
19、查詢選修“3-105”課程的成績高于“109”號同學成績的所有同學的記錄。
select * from score where cno='3-105' and degree>(select degree from score where sno='109' and cno='3-105')
20、查詢score中選學一門以上課程的同學中分數為非最高分成績的記錄。
select * from score where sno in(select sno from score where degree<(select MAX(degree) from score) group by sno having COUNT(*)>1) order by degree desc
21、查詢成績高于學號為“109”、課程號為“3-105”的成績的所有記錄。
select * from score where degree>(select degree from score where sno='109' and cno='3-105')
22、查詢和學號為108的同學同年出生的所有學生的Sno、Sname和Sbirthday列。
select sno,sname,sbirthday from student where Year(sbirthday)=(select Year(sbirthday) from student where sno='108')
23、查詢“張旭“教師任課的學生成績。
select sno,degree from course c inner join teacher t on c.tno=t.tno inner join score s on s.cno=c.cno where tname='張旭'
select sno,degree from score where cno in(select cno from course where tno=(select tno from teacher where tname='張旭'))//感覺這里用in好一點,一個老師可能上多門課
24、查詢選修某課程的同學人數多于5人的教師姓名。
select tname from teacher where tno=(select tno from course where cno in(select cno from score group by cno having count(cno)>5))
select t.tname from teacher t inner join course c on t.tno=c.tno left join score s on c.cno=s.cno group by t.tname having COUNT(*)>5
25、查詢95033班和95031班全體學生的成績記錄。
select * from score where sno in(select sno from student where class in('95033','95031'))
26、查詢存在有85分以上成績的課程Cno.
select cno from score where degree>85 group by cno
select cno from score group by cno having max(degree)>85
27、查詢出“計算機系“教師所教課程的成績表。
select * from score where cno in(select cno from course where tno in(select tno from teacher where depart='計算機系'))
select * from score where cno in (select c.cno from course c join teacher t on c.tno=t.tno and t.depart='計算機系')
28、查詢“計算機系”與“電子工程系“不同職稱的教師的Tname和Prof。
select tname,prof from teacher where depart='計算機系' and prof not in (select prof from teacher where depart='電子工程系');//網上答案,意思有點不對
select TName,Prof from TEACHER where DEPART='計算機系' and PROF not in (select PROF from TEACHER where DEPART='電子工程系') union select TName,Prof from TEACHER where DEPART='電子工程系' and PROF not in (select PROF from TEACHER where DEPART='計算機系')//應該相互職稱不同
29、查詢選修編號為“3-105“課程且成績至少高于選修編號為“3-245”的同學的Cno、Sno和Degree,并按Degree從高到低次序排序。
select cno,sno,degree from score where cno='3-105' and degree>(select min(degree) from score where cno='3-245') order by degree desc
select cno,sno,degree from score where cno='3-105' and degree>any(select degree from score where cno='3-245') order by degree desc
30、查詢選修編號為“3-105”且成績高于選修編號為“3-245”課程的同學的Cno、Sno和Degree。
select cno,sno,degree from score where cno='3-105' and degree>(select max(degree) from score where cno='3-245') order by degree desc
select cno,sno,degree from score where cno='3-105' and degree>all(select degree from score where cno='3-245') order by degree desc
31、查詢所有教師和同學的name、sex和birthday。
select tname as name,tsex as sex,tbirthday as birthday from teacher union select sname as name,ssex as sex,sbirthday as birthday from student
32、查詢所有“女”教師和“女”同學的name、sex和birthday。
select tname as name,tsex as sex,tbirthday as birthday from teacher where tsex='女' union select sname as name,ssex as sex,sbirthday as birthday from student where ssex='女'
33、查詢成績比該課程平均成績低的同學的成績表。
select * from score s inner join (select avg(degree) as avgScore,cno from score group by cno) t on s.CNO=t.CNO where s.DEGREE<t.avgScore
34、查詢所有任課教師的Tname和Depart。
select t.tname,t.depart from course c inner join teacher t on c.tno=t.tno
35 查詢所有未講課的教師的Tname和Depart.。
select tname,depart from course c right join teacher t on c.tno=t.tno where c.cno is null//course左連接就不會有null的值了,必須右連接
select tname,depart from teacher t left join course c on c.tno=t.tno where c.cno is null
36、查詢至少有2名男生的班號。
select class from student where ssex='男' group by class having count(sno)>=2
37、查詢Student表中不姓“王”的同學記錄。
select * from student where sname not like '王%'
38、查詢Student表中每個學生的姓名和年齡。
select sname,datediff(year,sbirthday,getdate()) from student
select sname,year(getdate())-year(sbirthday) from student
39、查詢Student表中最大和最小的Sbirthday日期值。
select max(sbirthday),min(sbirthday) from student
40、以班號和年齡從大到小的順序查詢Student表中的全部記錄。
select * from student order by class desc,sbirthday asc//日期小年齡大
41、查詢“男”教師及其所上的課程。
select * from course c inner join teacher t on c.tno=t.tno where tsex='男'
42、查詢最高分同學的Sno、Cno和Degree列。
select * from score where degree=(select max(degree) from score)
43、查詢和“李軍”同性別的所有同學的Sname。
select sname from student where ssex=(select ssex from student where sname='李軍')
44、查詢和“李軍”同性別并同班的同學Sname。
select sname from student where ssex=(select ssex from student where sname='李軍') and class=(select class from student where sname='李軍')
45、查詢所有選修“計算機導論”課程的“男”同學的成績表。
select * from score s inner join course c on s.cno=c.cno inner join student st on s.sno=st.sno where cname='計算機導論' and ssex='男'
轉載于:https://www.cnblogs.com/T-J-D/p/4118522.html
總結
- 上一篇: 王者风范是谁画的呢?
- 下一篇: WinDbg加载不同版本CLR