原文鏈接:https://blog.csdn.net/weixin_44675384/article/details/89226786
數(shù)據(jù)源一:
查詢問(wèn)題:
1镀迂、 查詢Student表中的所有記錄的Sname像街、Ssex和Class列寓辱。
2搓译、 查詢教師所有的單位即不重復(fù)的Depart列。
3乳乌、 查詢Student表的所有記錄果复。
4、 查詢Score表中成績(jī)?cè)?0到80之間的所有記錄底挫。
5、 查詢Score表中成績(jī)?yōu)?5脸侥,86或88的記錄建邓。
6、 查詢Student表中“95031”班或性別為“女”的同學(xué)記錄湿痢。
7涝缝、 以Class降序查詢Student表的所有記錄扑庞。
8、 以Cno升序拒逮、Degree降序查詢Score表的所有記錄罐氨。
9、 查詢“95031”班的學(xué)生人數(shù)滩援。
10栅隐、 查詢Score表中的最高分的學(xué)生學(xué)號(hào)和課程號(hào)。(子查詢或者排序)
11玩徊、 查詢每門課的平均成績(jī)租悄。
12、查詢Score表中至少有5名學(xué)生選修的并以3開頭的課程的平均分?jǐn)?shù)恩袱。
13泣棋、查詢分?jǐn)?shù)大于70,小于90的Sno列畔塔。
14潭辈、查詢所有學(xué)生的Sname、Cno和Degree列澈吨。
15把敢、查詢所有學(xué)生的Sno、Cname和Degree列谅辣。
16修赞、查詢所有學(xué)生的Sname、Cname和Degree列
17桑阶、 查詢“95033”班學(xué)生的平均分柏副。
18、 假設(shè)使用如下命令建立了一個(gè)grade表:
create table grade(low int(3),upp int(3),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列搓扯。
19、 查詢選修“3-105”課程的成績(jī)高于“109”號(hào)同學(xué)成績(jī)的所有同學(xué)的記錄包归。
20、查詢score中選學(xué)多門課程的同學(xué)中分?jǐn)?shù)為非最高分成績(jī)的記錄铅歼。
21公壤、查詢成績(jī)高于學(xué)號(hào)為“109”、課程號(hào)為“3-105”的成績(jī)的所有記錄椎椰。
22厦幅、查詢和學(xué)號(hào)為108的同學(xué)同年出生的所有學(xué)生的Sno、Sname和Sbirthday列慨飘。
23确憨、查詢“張旭“教師任課的學(xué)生成績(jī)译荞。
24、查詢選修某課程的同學(xué)人數(shù)多于5人的教師姓名休弃。
25吞歼、查詢95033班和95031班全體學(xué)生的記錄。
26塔猾、 查詢存在有85分以上成績(jī)的課程Cno.
27篙骡、查詢出“計(jì)算機(jī)系“教師所教課程的成績(jī)表。
28丈甸、查詢“計(jì)算機(jī)系”與“電子工程系“不同職稱的教師的Tname和Prof糯俗。
29、查詢選修編號(hào)為“3-105“課程且成績(jī)至少高于選修編號(hào)為“3-245”的同學(xué)的Cno睦擂、Sno和Degree,并按Degree從高到低次序排序得湘。
30、查詢選修編號(hào)為“3-105”且成績(jī)高于選修編號(hào)為“3-245”課程的同學(xué)的Cno顿仇、Sno和Degree.
31淘正、 查詢所有教師和同學(xué)的name、sex和birthday.
32夺欲、查詢所有“女”教師和“女”同學(xué)的name跪帝、sex和birthday.
33、 查詢成績(jī)比該課程平均成績(jī)低的同學(xué)的成績(jī)表些阅。
34伞剑、 查詢所有任課教師的Tname和Depart.
35 、 查詢所有未講課的教師的Tname和Depart.
36市埋、查詢至少有2名男生的班號(hào)黎泣。
37、查詢Student表中不姓“王”的同學(xué)記錄缤谎。
38抒倚、查詢Student表中每個(gè)學(xué)生的姓名和年齡。
39坷澡、查詢Student表中最大和最小的Sbirthday日期值托呕。
40、以班號(hào)和年齡從大到小的順序查詢Student表中的全部記錄频敛。
41项郊、查詢“男”教師及其所上的課程。
42斟赚、查詢最高分同學(xué)的Sno着降、Cno和Degree列。
43拗军、查詢和“李軍”同性別的所有同學(xué)的Sname.
44任洞、查詢和“李軍”同性別并同班的同學(xué)Sname.
45蓄喇、查詢所有選修“計(jì)算機(jī)導(dǎo)論”課程的“男”同學(xué)的成績(jī)表。
–第一題 查詢Student表中的所有記錄的Sname交掏、Ssex和Class列妆偏。
select Sname,Ssex,Class from student
–第二題 查詢教師所有的單位即不重復(fù)的Depart列。
select distinct Depart from Teacher
–第三題 查詢Student表的所有記錄耀销。
select * from student
–第四題 查詢Score表中成績(jī)?cè)?0到80之間的所有記錄楼眷。
select * from Score where Degree between 60 and 80
–第五題 查詢Score表中成績(jī)?yōu)?5,86或88的記錄熊尉。
select * from Score where Degree in (‘85’,‘86’,‘88’)
–第六題 查詢Student表中“95031”班或性別為“女”的同學(xué)記錄罐柳。
select * from student where Class=‘95031’ or Ssex=‘女’
–第七題 以Class降序查詢Student表的所有記錄。
select * from student order by Class desc
–第八題 以Cno升序狰住、Degree降序查詢Score表的所有記錄张吉。
select * from Score order by Cno asc,Degree desc
–第九題 查詢“95031”班的學(xué)生人數(shù)。
select count() from student where Class=‘95031’ --可以換成主鍵值
–第十題 查詢Score表中的最高分的學(xué)生學(xué)號(hào)和課程號(hào)催植。(子查詢或者排序)
select SNO,CNO from Score where Degree=(select MAX(Degree) from Score)
–第十一題 查詢每門課的平均成績(jī)肮蛹。
select Cno,AVG(Degree) as 平均分 from Score group by Cno
select Cname from Course where Cno in (select Cno a from Score group by Cno)
union
select Cno,AVG(Degree) from Score group by Cno
–第十二題 查詢Score表中至少有5名學(xué)生選修的并以3開頭的課程的平均分?jǐn)?shù)。
select AVG(Degree ) from Score where Cno like ‘3%’ group by Cno having COUNT(Cno)>4
–第十三題 查詢分?jǐn)?shù)大于70创南,小于90的Sno列伦忠。
select sno from Score where Degree between 70 and 90
–第十四題 查詢所有學(xué)生的Sname、Cno和Degree列稿辙。
select Sname,Cno,Degree from student join Score on student.Sno=Score.Sno
–第十五題 查詢所有學(xué)生的Sno昆码、Cname和Degree列。
select Sno,Cname,degree from Score join Course on Course.Cno=Score.Cno
–第十六題 查詢所有學(xué)生的Sname邻储、Cname和Degree列赋咽。
select student.Sname,Cname,degree from student join Score on student.Sno=Score.Sno join Course on Course.Cno=Score.Cno
–第十七題 查詢“95033”班學(xué)生的平均分。
select AVG(Degree) from Score where Sno in (select Sno from student where Class=‘95033’)
select AVG(Degree) from Score,student where student.Sno=Score.Sno and Class=‘95033’
–第十八題 假設(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,Degree,[RANK] from grade join Score on Score.Degree between low and upp
select Sno,Cno,Degree,[RANK] from Score,grade where Degree between low and upp
–第十九題 查詢選修“3-105”課程的成績(jī)高于“109”號(hào)同學(xué)成績(jī)的所有同學(xué)的記錄。
select * from student,Score where Score.Cno=‘3-105’ and student.Sno=Score.Sno and Score.Degree>(select Degree from Score where Cno=‘3-105’ and Sno=‘109’)
–第二十題 查詢score中選學(xué)多門課程的同學(xué)中分?jǐn)?shù)為非最高分成績(jī)的記錄宦赠。
select * from Score a where Degree <(select MAX(degree) from Score b where a.Cno=b.Cno) and Sno in(select Sno from Score group by Sno having count(*)>1)
–第二十一題 查詢成績(jī)高于學(xué)號(hào)為“109”陪毡、課程號(hào)為“3-105”的成績(jī)的所有記錄。
select * from student,Score where student.Sno=Score.Sno and Score.Degree>(select Degree from Score where Cno=‘3-105’ and Sno=‘109’)
–第二十二題 查詢和學(xué)號(hào)為107的同學(xué)同年出生的所有學(xué)生的Sno勾扭、Sname和Sbirthday列缤骨。
select Sno,Sname,Sbirthday from student where year(student.Sbirthday)=(select year(Sbirthday) from student where Sno=‘107’)
–第二十三題 查詢“張旭“教師任課的學(xué)生成績(jī)。
–select Degree from Score,Teacher,Course where Teacher.Tname=‘張旭’ and Teacher.Tno=Course.Tno and Course.Cno=Score.Cno
select Sno,Cno,Degree from Score where Cno in (select Cno from Course where Tno in (select Tno from Teacher where Tname=‘張旭’))
–第二十四題 查詢選修某課程的同學(xué)人數(shù)多于5人的教師姓名尺借。
select Tname from Teacher where Tno in (select Tno from Course where Cno in (select Cno from Score group by Cno having COUNT(*)>5) )
–第二十五題 查詢95033班和95031班全體學(xué)生的記錄。
select * from student where Class=‘95033’ or Class=‘95031’
–第二十六題 查詢存在有85分以上成績(jī)的課程Cno.
select distinct cno from Score where Degree>85
–第二十七題 查詢出“計(jì)算機(jī)系“教師所教課程的成績(jī)表精拟。
select sno,Cno ,Degree from Score where Cno in (select Cno from Course where Tno in (select tno from Teacher where Depart=‘計(jì)算機(jī)系’))
–第二十八題 查詢“計(jì)算機(jī)系”與“電子工程系“不同職稱的教師的Tname和Prof燎斩。使用相關(guān)子查詢
select Tname,Prof from Teacher a where Prof not in(select Prof from Teacher b where a.Depart!=b.Depart)
–第二十九題 查詢選修編號(hào)為“3-105“課程且成績(jī)至少高于選修編號(hào)為“3-245”課程的同學(xué)的Cno虱歪、Sno和Degree,并按Degree從高到低次序排序。
select Cno,Sno,Degree from Score a where (select Degree from Score b where Cno=‘3-105’ and b.Sno=a.Sno)>=(select Degree from Score c where Cno=‘3-245’ and c.Sno=a.Sno) order by Degree desc
select * from Score where Cno=‘3-105’ and Degree >any(select Degree from Score where Cno=‘3-245’)
–第三十題 查詢選修編號(hào)為“3-105”課程且成績(jī)高于選修編號(hào)為“3-245”課程的同學(xué)的Cno栅表、Sno和Degree.
select Cno,Sno,Degree from Score a where (select Degree from Score b where Cno=‘3-105’ and b.Sno=a.Sno)>(select Degree from Score c where Cno=‘3-245’ and c.Sno=a.Sno)
–第三十一題 查詢所有教師和同學(xué)的name笋鄙、sex和birthday.
select distinct Sname as name,Ssex as sex,Sbirthday as birthday from student
union
select distinct Tname as name,Tsex as sex,Tbirthady as birthday from Teacher
–第三十二題 查詢所有“女”教師和“女”同學(xué)的name、sex和birthday.
select distinct Sname as name,Ssex as sex,Sbirthday as birthday from student where Ssex=‘女’
union
select distinct Tname as name,Tsex as sex,Tbirthady as birthday from Teacher where Tsex=‘女’
–第三十三題 查詢成績(jī)比該課程平均成績(jī)低的同學(xué)的成績(jī)表怪瓶。
select Sno,Cno,Degree from Score a where a.Degree<(select AVG(Degree) from Score b where a.Cno=b.Cno)
–第三十四題 查詢所有任課教師的Tname和Depart.
select Tname,Depart from Teacher where Tname in (select distinct Tname from Teacher,Course,Score where Teacher.Tno=Course.Tno and Course.Cno=Score.Cno)
select Tname,Depart from Teacher where tno in (select tno from course where Cno in (select distinct Cno from Score))
–第三十五題 查詢所有未講課的教師的Tname和Depart.
select Tname,Depart from Teacher where Tname not in (select distinct Tname from Teacher,Course,Score where Teacher.Tno=Course.Tno and Course.Cno=Score.Cno)
–第三十六題 查詢至少有2名男生的班號(hào)萧落。
select Class FROM student where Ssex=‘男’ group by Class having COUNT(*)>1
–第三十七題 查詢Student表中不姓“王”的同學(xué)記錄。
select * from student where Sname not like (‘王%’)
–第三十八題 查詢Student表中每個(gè)學(xué)生的姓名和年齡洗贰。
select Sname,YEAR(GETDATE())-year(Sbirthday) from student
–第三十九題 查詢Student表中最大和最小的Sbirthday日期值找岖。
select MAX(Sbirthday) as 最大,MIN(Sbirthday) as 最小 from student
–第四十題 以班號(hào)和年齡從大到小的順序查詢Student表中的全部記錄。
select * from student order by Class desc,Sbirthday asc
–第四十一題 查詢“男”教師及其所上的課程敛滋。
select Tname,Cname from Teacher,Course where Tsex=‘男’ and Teacher.Tno=Course.Tno
–第四十二題 查詢最高分同學(xué)的Sno许布、Cno和Degree列。
select Sno,Cno,Degree from Score where degree=(select MAX(Degree)from Score)
select top 1* from Score order by Degree desc
–第四十三題 查詢和“李軍”同性別的所有同學(xué)的Sname.
select Sname from student where Ssex=(select Ssex from student where Sname=‘李軍’) and Sname not in (‘李軍’)
–第四十四題 查詢和“李軍”同性別并同班的同學(xué)Sname.
select Sname from student where Ssex=(select Ssex from student where Sname=‘李軍’) and Sname not in (‘李軍’) and Class=(select Class from student where Sname=‘李軍’)
–第四十五題 查詢所有選修“計(jì)算機(jī)導(dǎo)論”課程的“男”同學(xué)的成績(jī)表绎晃。
select Sno,Degree from Score where Sno in (select Sno from student where Ssex=‘男’) and Cno in (select Cno from Course where Cname=‘計(jì)算機(jī)導(dǎo)論’)
建表語(yǔ)句:
DROP TABLE IF EXISTS course;
CREATE TABLE course (
Cno varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
Cname varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
Tno varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (Cno) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-Records of course
INSERT INTO course VALUES ('1-111', '體育' , '999');
INSERT INTO course VALUES ('3-105', '計(jì)算機(jī)導(dǎo)論', '825');
INSERT INTO course VALUES ('3-245', '操作系統(tǒng)' , '804');
INSERT INTO course VALUES ('6-166', '數(shù)字電路' , '856');
INSERT INTO course VALUES ('9-888', '高等數(shù)學(xué)' , '831');
DROP TABLE IF EXISTS grade;
CREATE TABLE grade (
low int(11) NULL DEFAULT NULL,
upp int(11) NULL DEFAULT NULL,
rank char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
– Records of grade
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');
DROP TABLE IF EXISTS score;
CREATE TABLE score (
Sno varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
Cno varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
Degree varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (Sno) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-Records of course
INSERT INTO score VALUES ('107', '3-105', '91');
INSERT INTO score VALUES ('109', '3-245', '68');
INSERT INTO score VALUES ('110', '1-111', '100');
DROP TABLE IF EXISTS student;
CREATE TABLE student (
Sno varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
Sname varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
Ssex varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
Sbirthday varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
Class varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (Sno) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
– Records of student
INSERT INTO student VALUES ('101', '劉爭(zhēng)', '男', '1976', '95033');
INSERT INTO student VALUES ('105', '匡明', '男', '1975', '95031');
INSERT INTO student VALUES ('107', '王麗', '女', '1976', '95033');
INSERT INTO student VALUES ('108', '曾華', '男', '1977', '95033');
DROP TABLE IF EXISTS teacher;
CREATE TABLE teacher (
Tno varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
Tname varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
Tsex varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
Tbirthday varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
Prof varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
Depart varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (Tno) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
– Records of teacher
INSERT INTO teacher VALUES ('804', '李成', '男', '1958', '副教授','計(jì)算機(jī)系');
INSERT INTO teacher VALUES ('825', '王平', '女', '1972', '助教', '計(jì)算機(jī)系');
INSERT INTO teacher VALUES ('831', '劉兵', '女', '1977', '助教', '電子工程系');
INSERT INTO teacher VALUES ('856', '張旭', '男', '1969', '講師', '電子工程系');