准备工作
CREATE DATABASE STUDENTS;
CREATE TABLE STUDENT(
SNO VARCHAR(32) PRIMARY KEY NOT NULL,
SNAME VARCHAR(32) NOT NULL,
SSEX VARCHAR(32) NOT NULL,
SBIRTHDAY DATETIME,
CLASS VARCHAR(20)
);
CREATE TABLE COURSE(
CNO VARCHAR(20) PRIMARY KEY NOT NULL,
CNAME VARCHAR(20) NOT NULL,
TNO VARCHAR(20),
FOREIGN KEY(TNO) REFERENCES TEACHER(TNO)
);
CREATE TABLE SCORE(
SNO VARCHAR(20) NOT NULL,
CNO VARCHAR(20) NOT NULL,
DEGREE DECIMAL(4,1),
FOREIGN KEY(SNO) REFERENCES STUDENT(SNO),
FOREIGN KEY(CNO) REFERENCES COURSE(CNO)
);
CREATE TABLE TEACHER(
TNO VARCHAR(20) PRIMARY KEY NOT NULL,
TNAME VARCHAR(20) NOT NULL,
TSEX VARCHAR(20) NOT NULL,
TBIRTHDAY DATETIME,
PROF VARCHAR(20),
DEPART VARCHAR(20)
);
INSERT INTO STUDENT VALUE('108','曾华','男','1977-09-01','95033');
INSERT INTO STUDENT VALUE('105','匡明','男','1975-10-02','95031');
INSERT INTO STUDENT VALUE('107','王丽','女','1976-01-23','95033');
INSERT INTO STUDENT VALUE('101','李军','男','1976-02-20','95033');
INSERT INTO STUDENT VALUE('109','王芳','女','1975-02-10','95031');
INSERT INTO STUDENT VALUE('103','陆军','男','1974-06-03','95031');
INSERT INTO COURSE VALUE('3-105','计算机导论','825');
INSERT INTO COURSE VALUE('3-245','操作系统','804');
INSERT INTO COURSE VALUE('6-166','数字电路','856');
INSERT INTO COURSE VALUE('9-888','高等数学','831');
INSERT INTO SCORE VALUE('103','3-245','86');
INSERT INTO SCORE VALUE('105','3-245','75');
INSERT INTO SCORE VALUE('109','3-245','68');
INSERT INTO SCORE VALUE('103','3-105','92');
INSERT INTO SCORE VALUE('105','3-105','88');
INSERT INTO SCORE VALUE('109','3-105','76');
INSERT INTO SCORE VALUE('101','3-105','64');
INSERT INTO SCORE VALUE('107','3-105','91');
INSERT INTO SCORE VALUE('108','3-105','78');
INSERT INTO SCORE VALUE('101','6-166','85');
INSERT INTO SCORE VALUE('107','6-166','79');
INSERT INTO SCORE VALUE('108','6-166','81');
INSERT INTO TEACHER VALUE('804','李斌','男','1958-12-02','副教授','计算机系');
INSERT INTO TEACHER VALUE('856','张旭','男','1969-03-12','讲师','电子工程系');
INSERT INTO TEACHER VALUE('825','王萍','女','1972-05-05','助教','计算机系');
INSERT INTO TEACHER VALUE('831','刘冰','女','1977-08-14','助教','电子工程系');
#1、 查询Student表中的所有记录的Sname、Ssex和Class列。
SELECT SNAME,SSEX,CLASS FROM STUDENT;
#2、 查询教师所有的单位即不重复的Depart列。
SELECT DISTINCT PROF FROM TEACHER;
#3、 查询Student表的所有记录。
SELECT * FROM STUDENT;
#4、 查询Score表中成绩在60到80之间的所有记录。
SELECT * FROM SCORE WHERE DEGREE BETWEEN 60 AND 80;
SELECT * FROM SCORE WHERE DEGREE>=60 AND DEGREE<=80;
#5、 查询Score表中成绩为85,86或88的记录。
SELECT * FROM SCORE WHERE DEGREE IN(85,86,88);
SELECT * FROM SCORE WHERE DEGREE=85 OR DEGREE=86 OR DEGREE=88;
SELECT * FROM SCORE WHERE DEGREE=85 || DEGREE=86 || DEGREE=88;
#6、 查询Student表中“95031”班或性别为“女”的同学记录。
SELECT * FROM STUDENT WHERE CLASS='95031' OR SSEX='女';
SELECT * FROM STUDENT WHERE CLASS='95031' || 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(*) 95031班学生人数 FROM STUDENT WHERE CLASS='95031';
#10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
SELECT SNO,CNO FROM SCORE WHERE DEGREE=(SELECT MAX(DEGREE) FROM SCORE);
#11、 查询每门课的平均成绩。
SELECT AVG(DEGREE),CNO FROM SCORE GROUP BY CNO;
#12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
SELECT AVG(DEGREE) FROM SCORE GROUP BY CNO HAVING COUNT(CNO)>5 AND CNO LIKE '3%';
#13、查询分数大于70,小于90的Sno列。(注意此处是大于70,小于90,所以不能使用between...and...因为between 70 and 90是大于等于70小于等于90)
#SELECT SNO FROM SCORE WHERE DEGREE BETWEEN 70 AND 90;
SELECT SNO FROM SCORE WHERE DEGREE>70 AND DEGREE<90;
SELECT SNO FROM SCORE WHERE DEGREE>70 && DEGREE<90;
#14、查询所有学生的Sname、Cno和Degree列。
SELECT STU.SNAME,S.CNO,S.DEGREE FROM STUDENT STU,SCORE S WHERE STU.SNO=S.SNO;
SELECT STU.SNAME,S.CNO,S.DEGREE FROM STUDENT STU INNER JOIN SCORE S ON STU.SNO=S.SNO;
#15、查询所有学生的Sno、Cname和Degree列。
SELECT S.SNO,C.CNAME,S.DEGREE FROM SCORE S,COURSE C WHERE S.CNO=C.CNO;
SELECT S.SNO,C.CNAME,S.DEGREE FROM SCORE S INNER JOIN COURSE C ON S.CNO=C.CNO;
#16、查询所有学生的Sname、Cname和Degree列。
SELECT STU.SNAME,C.CNAME,S.DEGREE FROM STUDENT STU,COURSE C,SCORE S WHERE STU.SNO=S.SNO AND S.CNO=C.CNO;
SELECT STU.SNAME,C.CNAME,S.DEGREE FROM STUDENT STU INNER JOIN SCORE S ON STU.SNO=S.SNO INNER JOIN COURSE C ON S.CNO=C.CNO;
#17、查询“95033”班学生的平均分。
SELECT AVG(DEGREE) FROM STUDENT STU,SCORE S WHERE STU.SNO=S.SNO AND STU.CLASS='95033';
SELECT AVG(DEGREE) FROM STUDENT STU INNER JOIN SCORE S ON STU.SNO=S.SNO AND STU.CLASS='95033';
#18、假设使用如下命令建立了一个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');
#现查询所有同学的Sno、Cno和rank列。
SELECT SNO,CNO,RANK FROM SCORE,GRADE WHERE SCORE.DEGREE BETWEEN GRADE.LOW AND GRADE.UPP;
SELECT sno,cno,rank FROM score JOIN grade ON score.Degree BETWEEN grade.low AND grade.upp
#19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
SELECT *
FROM STUDENT S,(#查询成绩表课程是3-105并且成绩大于109号同学,并返回符合条件的所有数据
SELECT *
FROM SCORE
WHERE CNO='3-105' AND DEGREE>( #查询成绩表学号是109课程是3-105的成绩并返回该学生的成绩
SELECT DEGREE
FROM SCORE
WHERE SNO='109' AND CNO='3-105'
)
) NEWTABLE #将返回的数据作为另一张表,并给表取一张别名
WHERE S.SNO=NEWTABLE.SNO;#查询学生表和‘NEWTABLE’表相等学号的信息
#代码相同
SELECT * FROM STUDENT S,(SELECT * FROM SCORE WHERE CNO='3-105' AND DEGREE>(SELECT DEGREE FROM SCORE WHERE SNO='109' AND CNO='3-105')) NEWTABLE WHERE S.SNO=NEWTABLE.SNO;
#20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
SELECT * FROM SCORE S WHERE S.SNO IN(SELECT SNO FROM SCORE GROUP BY SNO HAVING COUNT(SNO)>1) AND S.DEGREE<(SELECT MAX(DEGREE) FROM SCORE SS WHERE S.CNO=SS.CNO);
#21、 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
SELECT * FROM SCORE WHERE CNO='3-105' AND DEGREE>(SELECT DEGREE FROM SCORE WHERE SNO='109' AND CNO='3-105');
#22、查询和学号为107的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
SELECT SNO,SNAME,SBIRTHDAY FROM STUDENT WHERE YEAR(SBIRTHDAY)=(SELECT YEAR(SBIRTHDAY) FROM STUDENT WHERE SNO='107');
#23、查询“张旭“教师任课的学生成绩。
SELECT DEGREE FROM SCORE S WHERE S.CNO IN(SELECT CNO FROM COURSE C,TEACHER T WHERE C.TNO=T.TNO AND T.TNAME='张旭');
SELECT DEGREE FROM SCORE S INNER JOIN COURSE C ON S.CNO=C.CNO INNER JOIN TEACHER T ON T.TNO=C.TNO AND T.TNAME='张旭';
#24、查询选修某课程的同学人数多于5人的教师姓名。
SELECT T.TNAME FROM TEACHER T WHERE T.TNO=(SELECT C.TNO FROM COURSE C WHERE C.CNO=(SELECT S.CNO FROM SCORE S GROUP BY S.CNO HAVING COUNT(SNO)>5));
#25、查询95033班和95031班全体学生的记录。
SELECT * FROM STUDENT WHERE CLASS IN('95033','95031');
SELECT * FROM STUDENT WHERE CLASS='95031' OR CLASS='95033';
SELECT * FROM STUDENT WHERE CLASS='95031' || CLASS='95033';
#26、查询存在有85分以上成绩的课程Cno
SELECT DISTINCT CNO FROM SCORE S WHERE S.DEGREE>'85'
#27、查询出“计算机系“副教授所教课程的成绩表。
SELECT S.DEGREE FROM SCORE S WHERE S.CNO=(SELECT C.CNO FROM COURSE C WHERE C.TNO=(SELECT T.TNO FROM TEACHER T WHERE T.DEPART='计算机系' AND T.PROF='副教授'));
#28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
SELECT Tname,Prof FROM teacher WHERE prof NOT IN (SELECT prof FROM teacher WHERE depart = '计算机系'AND prof IN (SELECT prof FROM teacher WHERE depart ='电子工程系'));
#29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
SELECT S.CNO,S.SNO,S.DEGREE FROM SCORE S WHERE S.CNO='3-105' AND S.DEGREE>(SELECT MAX(S.DEGREE) FROM SCORE S WHERE S.CNO='3-245') ORDER BY S.DEGREE;
#30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
SELECT S.CNO,S.SNO,S.DEGREE FROM SCORE S WHERE S.CNO='3-105' AND S.DEGREE>(SELECT MAX(DEGREE) FROM SCORE WHERE CNO='3-245');
#31、 查询所有教师和同学的name、sex和birthday
SELECT S.SNAME 姓名,S.SSEX 性别,S.SBIRTHDAY 生日 FROM STUDENT S UNION SELECT T.TNAME,T.TSEX,T.TBIRTHDAY FROM TEACHER T;
#32、查询所有“女”教师和“女”同学的name、sex和birthday.
SELECT S.SNAME 姓名,S.SSEX 性别,S.SBIRTHDAY 生日 FROM STUDENT S WHERE S.SSEX='女' UNION SELECT T.TNAME,T.TSEX,T.TBIRTHDAY FROM TEACHER T WHERE T.TSEX='女';
#33、 查询成绩比该课程平均成绩低的同学的成绩表。
SELECT CNO,DEGREE FROM SCORE S WHERE DEGREE<(SELECT AVG(DEGREE) FROM SCORE SS WHERE S.CNO=SS.CNO);
#34、 查询所有任课教师的Tname和Depart.
SELECT T.TNAME,T.DEPART FROM TEACHER T WHERE T.TNO IN (SELECT C.TNO FROM COURSE C WHERE C.CNO IN (SELECT CNO FROM SCORE GROUP BY CNO));
#35 、 查询所有未讲课的教师的Tname和Depart.
SELECT TNAME,DEPART FROM TEACHER WHERE TNO NOT IN(SELECT C.TNO FROM COURSE C WHERE C.CNO IN (SELECT S.CNO FROM SCORE S GROUP BY S.CNO));
#36、查询至少有2名男生的班号。
SELECT CLASS FROM STUDENT WHERE SSEX='男' GROUP BY CLASS HAVING COUNT(*)>=2;
#37、查询Student表中不姓“王”的同学记录。
SELECT * FROM STUDENT WHERE SNAME NOT LIKE '王%';
#38、查询Student表中每个学生的姓名和年龄。
SELECT SNAME,YEAR(GETDATE())-YEAR(SBIRTHDAY) AS '年龄' 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 DISTINCT TNAME,COURSE.CNAME FROM TEACHER INNER JOIN COURSE ON TEACHER.TNO=COURSE.TNO AND TEACHER.TSEX='男';
#42、查询最高分同学的Sno、Cno和Degree列。
SELECT SNO,CNO,DEGREE FROM SCORE WHERE DEGREE IN(SELECT MAX(DEGREE) FROM SCORE);
SELECT SNO,CNO,DEGREE FROM SCORE WHERE DEGREE =(SELECT MAX(DEGREE) FROM SCORE);
#43、查询和“李军”同性别的所有同学的Sname.
SELECT SNAME FROM STUDENT WHERE SSEX IN (SELECT SSEX FROM STUDENT WHERE SNAME='李军');
SELECT SNAME FROM STUDENT WHERE SSEX = (SELECT SSEX FROM STUDENT WHERE SNAME='李军');
#44、查询和“李军”同性别并同班的同学Sname.
SELECT SNAME FROM STUDENT WHERE SSEX IN (SELECT SSEX FROM STUDENT WHERE SNAME='李军') AND CLASS IN (SELECT CLASS FROM STUDENT WHERE SNAME='李军') AND SNAME NOT IN('李军');
SELECT SNAME FROM STUDENT WHERE SSEX = (SELECT SSEX FROM STUDENT WHERE SNAME='李军') AND CLASS = (SELECT CLASS FROM STUDENT WHERE SNAME='李军') AND SNAME <> ('李军');
#45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
SELECT STU.SNAME,STU.SSEX,S.DEGREE
FROM STUDENT STU
INNER JOIN SCORE S ON STU.SNO=S.SNO
INNER JOIN COURSE C ON S.CNO=C.CNO
AND C.CNAME='计算机导论' AND STU.SSEX='男';
微信公众号,感谢关注。持续更新....