日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

经典SQL练习题

發(fā)布時(shí)間:2023/11/27 生活经验 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 经典SQL练习题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目地址:http://blog.csdn.net/qaz13177_58_/article/details/5575711

1、 查詢Student表中的所有記錄的Sname、Ssex和Class列。
select sname,ssex,class from STUDENT
2、 查詢教師所有的單位即不重復(fù)的Depart列。
select depart from TEACHER group by depart
select distinct depart from teacher
3、 查詢Student表的所有記錄。
select * from student
4、 查詢Score表中成績(jī)?cè)?0到80之間的所有記錄。
select * from score where degree between 60 and 80
5、 查詢Score表中成績(jī)?yōu)?5,86或88的記錄。
select * from score where degree in(85,86,88)
6、 查詢Student表中“95031”班或性別為“女”的同學(xué)記錄。
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”班的學(xué)生人數(shù)。
select count(*) from student where class='95031'
10、查詢Score表中的最高分的學(xué)生學(xué)號(hào)和課程號(hào)。
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’號(hào)課程的平均分。
select avg(degree) from score where cno='3-105'
12、查詢Score表中至少有5名學(xué)生選修的并以3開頭的課程的平均分?jǐn)?shù)。
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、查詢所有學(xué)生的Sname、Cno和Degree列。
select s.SNAME,c.CNO,c.DEGREE from score c inner join student s on c.SNO=s.SNO
15、查詢所有學(xué)生的Sno、Cname和Degree列。
select s.sno,c.cname,s.degree from score s inner join course c on s.CNO=c.CNO
16、查詢所有學(xué)生的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、假設(shè)使用如下命令建立了一個(gè)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');
現(xiàn)查詢所有同學(xué)的Sno、Cno和rank列。
select sno,cno,(select rank from grade where degree between low and upp) from score
19、查詢選修“3-105”課程的成績(jī)高于“109”號(hào)同學(xué)成績(jī)的所有同學(xué)的記錄。
select * from score where cno='3-105' and degree>(select degree from score where sno='109' and cno='3-105')
20、查詢score中選學(xué)一門以上課程的同學(xué)中分?jǐn)?shù)為非最高分成績(jī)的記錄。
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、查詢成績(jī)高于學(xué)號(hào)為“109”、課程號(hào)為“3-105”的成績(jī)的所有記錄。
select * from score where degree>(select degree from score where sno='109' and cno='3-105')
22、查詢和學(xué)號(hào)為108的同學(xué)同年出生的所有學(xué)生的Sno、Sname和Sbirthday列。
select sno,sname,sbirthday from student where Year(sbirthday)=(select Year(sbirthday) from student where sno='108')
23、查詢“張旭“教師任課的學(xué)生成績(jī)。
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好一點(diǎn),一個(gè)老師可能上多門課
24、查詢選修某課程的同學(xué)人數(shù)多于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班全體學(xué)生的成績(jī)記錄。
select * from score where sno in(select sno from student where class in('95033','95031'))
26、查詢存在有85分以上成績(jī)的課程Cno.
select cno from score where degree>85 group by cno
select cno from score group by cno having max(degree)>85
27、查詢出“計(jì)算機(jī)系“教師所教課程的成績(jī)表。
select * from score where cno in(select cno from course where tno in(select tno from teacher where depart='計(jì)算機(jī)系'))
select * from score where cno in (select c.cno from course c join teacher t on c.tno=t.tno and t.depart='計(jì)算機(jī)系')
28、查詢“計(jì)算機(jī)系”與“電子工程系“不同職稱的教師的Tname和Prof。
select tname,prof from teacher where depart='計(jì)算機(jī)系' and prof not in (select prof from teacher where depart='電子工程系');//網(wǎng)上答案,意思有點(diǎn)不對(duì)
select TName,Prof from TEACHER where DEPART='計(jì)算機(jī)系' 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='計(jì)算機(jī)系')//應(yīng)該相互職稱不同
29、查詢選修編號(hào)為“3-105“課程且成績(jī)至少高于選修編號(hào)為“3-245”的同學(xué)的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、查詢選修編號(hào)為“3-105”且成績(jī)高于選修編號(hào)為“3-245”課程的同學(xué)的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、查詢所有教師和同學(xué)的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、查詢所有“女”教師和“女”同學(xué)的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、查詢成績(jī)比該課程平均成績(jī)低的同學(xué)的成績(jī)表。
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左連接就不會(huì)有null的值了,必須右連接
select tname,depart from teacher t left join course c on c.tno=t.tno where c.cno is null
36、查詢至少有2名男生的班號(hào)。
select class from student where ssex='男' group by class having count(sno)>=2
37、查詢Student表中不姓“王”的同學(xué)記錄。
select * from student where sname not like '王%'
38、查詢Student表中每個(gè)學(xué)生的姓名和年齡。
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、以班號(hào)和年齡從大到小的順序查詢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、查詢最高分同學(xué)的Sno、Cno和Degree列。
select * from score where degree=(select max(degree) from score)
43、查詢和“李軍”同性別的所有同學(xué)的Sname。
select sname from student where ssex=(select ssex from student where sname='李軍')
44、查詢和“李軍”同性別并同班的同學(xué)Sname。
select sname from student where ssex=(select ssex from student where sname='李軍') and class=(select class from student where sname='李軍')
45、查詢所有選修“計(jì)算機(jī)導(dǎo)論”課程的“男”同學(xué)的成績(jī)表。
select * from score s inner join course c on s.cno=c.cno inner join student st on s.sno=st.sno where cname='計(jì)算機(jī)導(dǎo)論' and ssex='男'

轉(zhuǎn)載于:https://www.cnblogs.com/T-J-D/p/4118522.html

總結(jié)

以上是生活随笔為你收集整理的经典SQL练习题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。