--创造学生情况表并添加数据CREATE TABLE student(
Sno VARCHAR(20) PRIMARY KEY,
Sname VARCHAR(20) NOT NULL,
Ssex VARCHAR(20) NOT NULL,
Sbirthday DATETIME,
Class VARCHAR(20)
)
DELETE FROM student;
SELECT * FROM student;
INSERT INTO student VALUES(108,'曾华','男','1977-09-01',95033);
INSERT INTO student VALUES(105,'匡明','男','1975-10-02',95031);
INSERT INTO student VALUES(107,'王丽','女','1976-01-23',95033);
INSERT INTO student VALUES(101,'李军','男','1976-02-20',95033);
INSERT INTO student VALUES(109,'王芳','女','1975-02-10',95031);
INSERT INTO student VALUES(103,'陆君','男','1974-06-03',95031);
--创造课程表并添加数据
CREATE TABLE course(
Cno VARCHAR(20) PRIMARY KEY,
Cname VARCHAR(20) NOT NULL,
Tno VARCHAR(20) NOT NULL
)
SELECT * FROM course;
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);
--创造课程表并添加数据
CREATE TABLE score(
Sno VARCHAR(20) NOT NULL,
Cno VARCHAR(20) NOT NULL,
Degree DECIMAL(4,1)
)
DELETE FROM score ;
SELECT * FROM score;
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);
--创造教师情况表并添加数据
CREATE TABLE teacher(
Tno VARCHAR(20) PRIMARY KEY,
Tname VARCHAR(20) NOT NULL,
Tsex VARCHAR(20) NOT NULL,
Tbirthday DATETIME,
Prof VARCHAR(20),
Depart VARCHAR(20) NOT NULL
);
SELECT * FROM teacher;
INSERT INTO teacher VALUES('804','李诚','男','1985-12-02','副教授','计算机系');
INSERT INTO teacher VALUES('856','张旭','男','1969-03-12','讲师','电子工程系');
INSERT INTO teacher VALUES('825','王萍','女','1972-05-05','助教','计算机系');
INSERT INTO teacher VALUES('831','刘冰','女','1977-08-14','助教','电子工程系');
-- 1、 查询Student表中的所有记录的Sname、Ssex和Class列。
SELECT Sname,Ssex,Class FROM student;
-- 2、 查询教师所有的单位即不重复的Depart列。
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 =85 OR degree =86 OR degree =88;
-- 6、 查询Student表中“95031”班或性别为“女”的同学记录。
-- 以Class降序查询Student表的所有记录。
-- 以Sno升序、Degree降序查询Score表的所有记录。
-- 查询“95031”班的学生人数。
SELECT * FROM student WHERE class = '95031' OR Ssex = '女';
SELECT * FROM student ORDER BY class DESC;
SELECT * FROM score ORDER BY cno ASC,degree DESC ;
SELECT COUNT(class) FROM student WHERE class = '95031' ;
-- 10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
-- 查询每门课的平均成绩。
SELECT sno,cno FROM score WHERE degree =(SELECT MAX(degree) FROM score);
SELECT AVG(degree),cno FROM score GROUP BY cno;
-- 12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
-- 查询分数大于70,小于90的Sno列。
-- 查询所有学生的Sname、Cno和Degree列。
SELECT AVG(degree) FROM score GROUP BY cno HAVING cno LIKE '3%' AND COUNT(cno)>5;
SELECT sno FROM score WHERE degree>70 AND degree<90;
SELECT sname,cno,degree FROM score JOIN student ON score.`Sno` = student.`Sno` ;
-- 15、查询所有学生的Sno、Cname和Degree列。
SELECT sno,cname,degree FROM score JOIN course ON score.`Cno` = course.`Cno`;
-- 16、查询所有学生的Sname、Cname和Degree列。
SELECT sname,cname,degree FROM (student JOIN score ON student.sno = score.`Sno`) JOIN course ON score.`Cno`=course.`Cno`;
-- 17、 查询“95033”班学生的平均分。
-- 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
SELECT AVG(degree) FROM score JOIN student ON score.`Sno` = student.`Sno` WHERE class = '95033';
SELECT * FROM score JOIN student ON score.`Sno` = student.`Sno` WHERE cno = '3-105' AND degree>(SELECT degree FROM score WHERE sno='109' AND cno='3-105' );
-- 20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
SELECT * FROM score WHERE degree <> ALL(SELECT MAX(degree) FROM score GROUP BY cno) AND
sno IN(SELECT sno FROM score GROUP BY sno HAVING COUNT(sno)>1);
-- 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、查询“张旭“教师任课的学生成绩(姓名)。
-- 1、从成绩表 查询学生成绩
--
-- 2、条件是这个课程是 “张旭“教师任课
-- A)从老师表 中查名字叫 张旭 编号
-- B)根据老师编号 在课程表中找到对应的 课程号
SELECT sname,degree FROM student JOIN score ON student.`Sno`=score.`Sno`
WHERE cno =( SELECT cno FROM course WHERE tno = (SELECT tno FROM teacher WHERE tname = '张旭'));-- 查询考计算机导论的学生成绩SELECT degree FROM score JOIN course ON score.cno = course.cno WHERE cname = '计算机导论';-- 查询李诚老师教的课程名称SELECT cname FROM course WHERE tno = (SELECT tno FROM teacher WHERE tname ='李诚');-- 教高等数学的老师是哪个系的SELECT depart FROM teacher WHERE tno =(SELECT tno FROM course WHERE cname ='高等数学');-- 24、查询选修某课程的同学人数多于5人的教师姓名。SELECT tname FROM teacher WHERE tno =(SELECT tno FROM course WHERE cno = (SELECT cno FROM score GROUP BY cno HAVING COUNT(cno)>5)); -- 25、查询95033班和95031班全体学生的记录。SELECT * FROM student WHERE class ='95033' OR class ='95031';-- 26、 查询存在有85分以上成绩的课程Cno.SELECT cno FROM score WHERE degree>85;-- 27、查询出“计算机系“教师所教课程的成绩表。SELECT degree FROM scoreWHERE cno IN (SELECT cno 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> ALL(SELECT degree FROM score WHERE cno ='3-245')ORDER BY degree DESC;-- -- 31、 查询所有教师和同学的name、sex和birthday.SELECT DISTINCT sname AS NAME,ssex AS sex,sbirthday AS birthday FROM student UNION SELECT tname,tsex,tbirthday FROM teacher;-- 32、查询所有“女”教师和“女”同学的name、sex和birthday.SELECT DISTINCT sname AS NAME,ssex AS sex,sbirthday AS birthday FROM student WHERE ssex ='女' UNION SELECT tname,tsex,tbirthday FROM teacher WHERE tsex = '女';-- 33、 查询成绩比该课程平均成绩低的同学的成绩表。SELECT * FROM Score a WHERE a.Degree<(SELECT AVG(Degree) FROM Score b WHERE a.Cno=b.Cno);-- 34、 查询所有任课教师的Tname和Depart.SELECT tname,depart FROM teacher;-- 35、查询所有未讲课的教师的Tname和Depart.SELECT tname,depart FROM teacher WHERE tno =(SELECT tno FROM course LEFT JOIN score ON course.`Cno` = score.`Cno` WHERE degree IS NULL); -- 36、查询至少有2名男生的班号。SELECT class FROM student GROUP BY class HAVING COUNT('男')>1;-- 37、查询Student表中不姓“王”的同学记录。SELECT * FROM student WHERE sname NOT LIKE '王%';-- 38、查询Student表中每个学生的姓名和年龄。SELECT sname,(YEAR(CURRENT_DATE())-YEAR(Sbirthday)) FROM student;-- 39、查询Student表中最大和最小的Sbirthday日期值。SELECT MAX(DATE(sbirthday)),MIN(DATE(sbirthday)) FROM student;-- 40、以班号和年龄从大到小的顺序查询Student表中的全部记录。SELECT * FROM student ORDER BY class,sbirthday DESC;-- 41、查询“男”教师及其所上的课程。SELECT tname,cname FROM teacher JOIN course ON teacher.`Tno`=course.`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 WHERE cno IN (SELECT cno FROM course WHERE cname = '计算机导论') AND sno IN (SELECT sno FROM student WHERE ssex = '男');
mysql
点赞
收藏