CREATE TABLE student /*第一个表*/
(
sno INT,
sname VARCHAR(5),
ssex VARCHAR(4),
sbirthday DATE,
class VARCHAR(5)
)
INSERT INTO student VALUES (108,'曾华','男','1977-9-1',95033);
INSERT INTO student VALUES (105,'匡明','男','1975-10-2',95031);
INSERT INTO student VALUES (107,'王丽','女','1976-1-23',95033);
INSERT INTO student VALUES (101,'李军','男','1976-2-20',95033);
INSERT INTO student VALUES (109,'王芳','女','1975-2-10',95031);
INSERT INTO student VALUES (103,'陆君','男','1974-6-3',95031);
SELECT * FROM student
CREATE TABLE course/*第二个表*/
(
cno VARCHAR(5) NOT NULL,
cname VARCHAR(10) NOT NULL,
tno VARCHAR(5) NOT NULL
)
INSERT INTO course VALUES ('3-105','计算机导论','825');
INSERT INTO course VALUES ('3-245','操作系统','804');
INSERT INTO course VALUES ('6-166','数字电路','856');
INSERT INTO course VALUES ('9-888','高等数学','831');
SELECT * FROM course
CREATE TABLE score /*第三个表*/
(
sno INT,
cno VARCHAR(10),
degree INT
)
INSERT INTO score VALUES ('103','3-245',86);
INSERT INTO score VALUES ('105','3-245',75);
INSERT INTO score VALUES ('109','3-245',68);
INSERT INTO score VALUES ('103','3-105',92);
INSERT INTO score VALUES ('105','3-105',88);
INSERT INTO score VALUES ('109','3-105',76);
INSERT INTO score VALUES ('101','3-105',64);
INSERT INTO score VALUES ('107','3-105',91);
INSERT INTO score VALUES ('108','3-105',78);
INSERT INTO score VALUES ('101','6-166',85);
INSERT INTO score VALUES ('107','6-166',79);
INSERT INTO score VALUES ('108','6-166',81);
SELECT * FROM score
CREATE TABLE teacher/*第四个表*/
(
tno INT,
tname VARCHAR(10),
tsex VARCHAR(5),
tbirthday DATE,
Prof VARCHAR(5),
depart VARCHAR(10)
)
INSERT INTO teacher VALUES (804,'李成','男','1958-12-2','副教授','计算机系');
INSERT INTO teacher VALUES (856,'张旭','男','1969-3-12','讲师','电子工程系');
INSERT INTO teacher VALUES (825,'王萍','女','1972-5-5','助教','计算机系');
INSERT INTO teacher VALUES (831,'刘冰','女','1977-8-14','助教','电子工程系');
SELECT * FROM score
SELECT * FROM course
SELECT * FROM student
SELECT * FROM teacher
/*1、查询Student表中的所有记录的Sname、Ssex和Class列。*/
SELECT sname,ssex,class FROM student;
/*2、 查询教师所有的单位即不重复的Depart列。*/
SELECT * FROM teacher;
SELECT DISTINCT depart FROM teacher;
/*3、 查询Student表的所有记录。*/
SELECT * FROM student;
/*4、 查询Score表中成绩在60到80之间的所有记录。。*/
SELECT * FROM score WHERE degree>60 AND degree<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 * FROM student WHERE class='95031';
SELECT COUNT(*) AS '95031班的人数' FROM student WHERE class="95031";
/*10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)*/
SELECT sno,cno,MAX(degree) FROM score /*最高分的学生号课程号*/
GROUP BY sno,cno;/*排序*/
/*11、查询每门课的平均成绩*/
SELECT cno,AVG(degree) AS 平均分 FROM score GROUP BY cno;
/*12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。*/
SELECT cno,AVG(degree) FROM score GROUP BY cno HAVING COUNT(sno)>5 AND cno LIKE '3%';
/*13、查询分数大于70,小于90的Sno列。*/
SELECT sno FROM score WHERE degree>70 AND degree<90;
/*14、查询所有学生的Sname、Cno和Degree列。*/
SELECT sname,cno,degree FROM score
JOIN student ON student.sno=score.sno;
/*15、查询所有学生的Sno、Cname和Degree列。*/
SELECT * FROM course;
SELECT * FROM score;
SELECT sno,cname,degree FROM score
JOIN course ON course.cno=score.sno;
/*16、查询所有学生的Sname、Cname和Degree列。*/
SELECT cname,sname,degree FROM score
JOIN course ON course.cno=score.cno
JOIN student ON student.sno=score.sno;
/*17、 查询“95033”班学生的平均分。*/
SELECT * FROM course;
SELECT * FROM student;
SELECT * FROM score;
SELECT AVG(degree) AS '95033平均分'FROM score WHERE sno IN(SELECT sno FROM student WHERE class="95033");
/*18、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。*/
/*20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。*/
SELECT * FROM student WHERE sno =
/*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 * FROM student WHERE YEAR(Sbirthday) LIKE (SELECT YEAR(sbirthday) FROM student WHERE sno='108')AND sno=108;
/*查询“张旭“教师任课的学生成绩(姓名)。*/
SELECT * FROM score WHERE cno IN(SELECT cno FROM course WHERE tno IN(SELECT tno FROM teacher WHERE tname='张旭'));
/*查询考计算机导论的学生成绩*/
SELECT * FROM student WHERE sno IN(SELECT sno FROM score WHERE cno IN(SELECT cno FROM course WHERE cname='计算机导论'));
/*查询李诚老师教的课程名称*/
SELECT * FROM course WHERE tno IN(SELECT tno FROM teacher WHERE tname='李成');
/*教高等数学的老师是哪个系的*/
SELECT * FROM teacher WHERE tno IN(SELECT tno FROM course WHERE cname='高等数学');
/*24、查询选修某课程的同学人数多于5人的教师姓名。*/
SELECT * FROM teacher WHERE tno IN(SELECT tno FROM course WHERE cno IN(SELECT cno FROM score GROUP BY cno HAVING COUNT(sno>5)));
/*25、查询95033班和95031班全体学生的记录。*/
SELECT * FROM student WHERE class IN('95033','95031');
/*26、 查询存在有85分以上成绩的课程Cno.*/
SELECT * FROM score WHERE degree>85;
/*27、查询出“计算机系“教师所教课程的成绩表。*/
SELECT * FROM course WHERE tno IN (SELECT tno FROM teacher WHERE depart='计算机系');
/*29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学 的Cno、Sno和Degree,并按Degree从高到低次序排序。*/
SELECT * FROM score WHERE cno = '3-105' AND degree>(SELECT MAX(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')
/*31、 查询所有教师和同学的name、sex和birthday.*/
SELECT tname,tsex,tbirthday FROM teacher UNION SELECT sname,ssex,sbirthday FROM student;
/*32、查询所有“女”教师和“女”同学的name、sex和birthday.*/
SELECT tname,tsex,tbirthday FROM teacher WHERE tsex='女' UNION SELECT sname,ssex,sbirthday FROM student WHERE ssex='女';
/*33、 查询成绩比该课程平均成绩低的同学的成绩表。*/
SELECT * FROM score WHERE degree<(SELECT AVG(degree) FROM score)
/*34、 查询所有任课教师的Tname和Depart.*/
SELECT tname,depart FROM teacher
/*35、查询所有未讲课的教师的Tname和Depart. */
SELECT * FROM teacher WHERE tno NOT IN(SELECT tno FROM course);
/*36、查询至少有2名男生的班号。*/
SELECT class FROM student GROUP BY class HAVING COUNT(class)>=2
/*37、查询Student表中不姓“王”的同学记录。*/
SELECT * FROM student WHERE sname NOT LIKE'王%'
/*38、查询Student表中每个学生的姓名和年龄。*/
SELECT sname,DATEDIFF(MONTH,'2018.07-12',CURRENT_TIMESTAMP)/12 AS '年龄' FROM student
/*39、查询Student表中最大和最小的Sbirthday日期值。*/
SELECT MAX(sbirthday) AS 最大值,MIN(sbirthday) AS 最小值 FROM student
/*40、以班号和年龄从大到小的顺序查询Student表中的全部记录。*/
SELECT * FROM student GROUP BY class ASC,DATEDIFF(YEAR,sbirthday ,'2014-11-9') ASC
/*41、查询“男”教师及其所上的课程。*/
SELECT cname FROM course WHERE tno IN(SELECT tno FROM teacher WHERE tsex='男')
/*42、查询最高分同学的Sno、Cno和Degree列。*/
SELECT *FROM score WHERE degree IN(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 student WHERE sno IN(SELECT sno FROM score WHERE cno IN(SELECT cno FROM course WHERE cname='计算机导论'))
其中有几个不对的。