SELECT sNo, sName, EXTRACT(YEARFROMCURRENT_DATE) - age AS sBirthday FROM student WHERE sName LIKE'钱%' AND sex ='男';
(3)查询所有学分大于3的课程名称;
1 2 3
SELECT cName FROM course WHERE credit >3;
(4)查询所有没有被分配到任何学院的学生姓名;
1 2 3
SELECT sName FROM student WHERE dNo ISnull;
(5)查询所有尚未设置主页的学院名称。
1 2 3
SELECT dName FROM department WHERE homepage ISnull;
二、聚集
(1)查询各个学院的平均年龄;
1 2 3 4
SELECT department.dName AS 学院名称, ROUND(AVG(student.age), 2) AS 平均年龄 FROM student, department GROUPby student.dNo, department.dNo HAVING student.dNo = department.dNo;
(2)查询每个学生选修课程的平均分;
1 2 3 4 5
SELECT student.sName AS 学生姓名, ROUND(AVG(sc.score), 2) AS 选修课程平均分 FROM student, sc GROUPBY sc.sNo, student.sNo HAVING sc.sNo = student.sNo;
(3)查询各课程的平均分;
1 2 3 4 5
SELECT cName AS 课程名称, ROUND(AVG(score), 2) AS 课程平均分 FROM course, sc GROUPBY course.cNo, sc.cNo HAVING course.cNo = sc.cNo;
(4)查询各学院开设的课程门数;
注意开设课程数为0的学院也应该在查询结果中显示
1 2 3 4 5
SELECT dName AS 学院名称, COUNT(cNO) AS 开课数 FROM department LEFTOUTERJOIN course ON department.dNo = course.dNo GROUPBY department.dNo;
(5)查询各门课程选修人数。
注意选修人数为0的课程也应该在查询结果中显示
1 2 3 4 5
SELECT cName AS 课程名称, count(sNo) AS 选课人数 FROM sc RIGHTOUTERJOIN course ON sc.cNo = course.cNo GROUPBY course.cNo
三、多表
(1)查询“信息学院”所有学生学号与姓名;
1 2 3 4 5 6 7
SELECT sNo AS 学号, sName AS 姓名 FROM student WHERE student.dno = ( SELECT dNo FROM department WHERE dName ='信息学院' )
(2)查询“软件学院”开设的所有课程号与课程名称;
1 2 3 4 5 6 7
SELECT cNo AS 课程号, cName AS 课程名称 FROM course WHERE course.dNO = ( SELECT dNo FROM department WHERE dName ='软件学院' )
(3)查询与“陈丽”在同一个系的所有学生学号与姓名;
1 2 3 4 5 6 7 8 9
SELECT sNo AS 学号, sName AS 姓名 FROM student st1 WHERE dNo IN ( SELECT st2.dNO FROM student st2 WHERE sName ='陈丽' LIMIT 1 ) AND sName <>'陈丽';
(4)查询与“张三”同岁的所有学生学号与姓名;
1 2 3 4 5 6 7 8 9
SELECT sNo AS 学号, sName AS 姓名 FROM student st1 WHERE age IN ( SELECT st2.age FROM student st2 WHERE st2.sName ='张三' LIMIT 1 ) AND st1.sName <>'张三'
(5)查询与“张三”同岁且不与“张三”在同一个系的学生学号与姓名;
1 2 3 4 5 6 7 8 9 10 11 12 13 14
SELECT sno AS 学号, sname AS 姓名 FROM student st1 WHERE age IN ( SELECT st2.age FROM student st2 WHERE st2.sName ='张三' LIMIT 1 ) AND dno NOTIN ( SELECT st2.dNo FROM student st2 WHERE st2.sName ='张三' LIMIT 1 );
(6)查询学分大于“离散数学”的所有课程名称;
1 2 3 4 5 6 7 8
SELECT cname AS 课程名称 FROM course c1 WHERE credit > ( SELECT c2.credit FROM course c2 WHERE c2.cname ='离散数学' );
(7)查询选修了课程名为“组合数学”的学生人数;
1 2 3 4 5 6 7 8 9 10 11
SELECTcount(student.sno) AS 学生人数 FROM student WHERE student.sno IN ( SELECT sc.sno FROM sc WHERE sc.cno = ( SELECT course.cno FROM course WHERE course.cname ='组合数学' ) );
(8)查询没有选修“离散数学”的学生姓名;
1 2 3 4 5 6 7 8 9 10 11
SELECT student.sname AS 学生姓名 FROM student WHERE student.sno NOTIN ( SELECT sc.sno FROM sc WHERE sc.cno = ( SELECT course.cno FROM course WHERE course.cname ='离散数学' ) );
(9)查询与“算法设计与分析”、“移动计算”学分不同的所有课程名称;
1 2 3 4 5 6 7 8
SELECT cname AS 课程名称 FROM course WHERE credit NOTIN ( SELECT credit FROM course WHERE cname ='算法设计与分析' OR cname ='移动计算' );
(10)查询平均分大于等于90分的所有课程名称;
1 2 3 4 5 6 7 8
SELECT course.cname AS 课程名称 FROM course WHERE course.cno IN ( SELECT sc.cno FROM sc GROUPBY sc.cno HAVINGAVG(sc.score) >=80 );
(11)查询选修了“离散数学”课程的所有学生姓名与成绩;
1 2 3 4 5 6 7 8 9
SELECT sName AS 姓名, score AS 离散数学成绩 FROM student RIGHTOUTERJOIN sc ON student.sNo = sc.sNo WHERE sc.cNo = ( SELECT course.cNo FROM course WHERE cname ='离散数学' )
(12)查询“王兵”所选修的所有课程名称及成绩;
1 2 3 4 5 6 7 8 9 10
SELECT cName AS 课程名称, score AS 成绩 FROM course RIGHTOUTERJOIN sc ON course.cNo = sc.cNo WHERE sc.sNo IN ( SELECT st.sNo FROM student st WHERE st.sName ='王兵' LIMIT 1 );
(13)查询所有具有不及格课程的学生姓名、课程名与成绩;
1 2 3 4 5 6 7 8 9 10 11
SELECT sName AS 学生姓名, cName AS 课程名称, score AS 成绩 FROM student RIGHTOUTERJOIN sc ON sc.sNo = student.sNo
RIGHTOUTERJOIN course ON course.cNo = sc.cNo
WHERE ( score <60 )
(14)查询选修了“文学院”开设课程的所有学生姓名;
1 2 3 4 5 6 7 8 9 10 11 12 13
SELECT sname FROM student WHERE sno IN ( SELECT sc.sno FROM sc, course WHERE sc.cno = course.cno AND course.dno = ( SELECT dno FROM department WHERE dname ='文学院' ) );
(15)查询“信息学院”所有学生姓名及其所选的“信息学院”开设的课程名称。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
SELECT student.sname, course.cname FROM student, course, sc WHERE student.dno = ( SELECT dno FROM department WHERE dname ='信息学院' ) AND course.dno = ( SELECT dno FROM department WHERE dname ='信息学院' ) AND sc.sno = student.sno AND sc.cno = course.cno;
四、综合
(1)查询所有学生及其选课信息(包括没有选课的学生);
1 2 3 4
SELECT* FROM student LEFTOUTERJOIN sc ON student.sNo = sc.sNo;
(2)查询“形式语言与自动机”先修课的课程名称;
1 2 3 4 5 6 7
SELECT cname FROM course WHERE cno = ( SELECT cpno FROM course WHERE cname ='形式语言与自动机' );
(3)查询“形式语言与自动机”间接先修课课程名称;
1 2 3 4 5 6 7 8 9 10 11
SELECT cname FROM course WHERE cno = ( SELECT cpno FROM course WHERE cno = ( SELECT cpno FROM course WHERE cname ='形式语言与自动机' ) );
(4)查询先修课为编译原理数学的课程名称
1 2 3 4 5
SELECT second.cname FROM course first, course second WHERE first.cno = second.cpno AND first.cname ='编译原理数学'
(5)查询间接先修课为离散数学的课程名称;
1 2 3 4 5 6 7
SELECT third.cName FROM course first, course second, course third WHERE first.cname ='离散数学' AND second.cno = third.cpno AND first.cno = second.cpno;
(6)查询所有没有先修课的课程名称;
1 2 3
SELECT cname FROM course WHERE cpno ISNULL;
(7)查询所有没选修“形式语言与自动机”课程的学生姓名;
1 2 3 4 5 6 7 8 9 10 11
SELECT sname FROM student WHERE sno NOTIN ( SELECT sno FROM sc WHERE cno = ( SELECT cno FROM course WHERE cname ='形式语言与自动机' ) );
(8)查询仅仅选修了离散数学一门课程的学生姓名;
1 2 3 4 5 6 7 8 9 10 11 12 13
SELECT sname FROM student WHERE student.sno IN ( SELECT sno FROM sc GROUPBY sc.sno HAVINGcount(*) =1 ) AND student.sno IN ( SELECT sno FROM sc, course WHERE sc.cno = course.cno AND cname ='离散数学' )
SELECT sname FROM student WHERE sno IN ( SELECT sno FROM sc WHERE cno = ( SELECT cno FROM course WHERE cname ='形式语言与自动机' ) ) AND sno NOTIN ( SELECT sno FROM sc WHERE cno = ( SELECT cpno FROM course WHERE cname ='形式语言与自动机' ) );
(10)查询选修课程总学分大于等于28的学生姓名及其选修课程总学分;
1 2 3 4 5 6 7 8
SELECT student.sname, count(course.credit) AS totalCredit FROM student, course, sc WHERE student.sno = sc.sno AND sc.cno = course.cno GROUPBY student.sno HAVINGcount(course.credit) >=28;
(11)查询选修了3门以上课程且成绩都大于85分的学生学号与姓名;
1 2 3 4 5 6 7
SELECT student.sno, student.sname FROM student, sc WHERE student.sno = sc.sno GROUPBY student.sno HAVINGmin(sc.score) >85 ANDcount(*) >3;
(12)查询恰好选修了3门课并且都及格的学生姓名;
1 2 3 4 5 6 7
SELECT student.sname FROM student, sc WHERE student.sno = sc.sno GROUPBY student.sno HAVINGmin(sc.score) >=60 ANDcount(*) =3;
(13)查询人数多于6的学院名称及其学生人数;
1 2 3 4 5 6
SELECT department.dname, count(*) AS studentNum FROM department, student WHERE department.dno = student.dno GROUPBY department.dno HAVINGcount(*) >6;
(14)查询平均成绩高于王兵的学生姓名;
1 2 3 4 5 6 7 8 9 10 11 12 13
SELECT student.sname FROM student, sc WHERE student.sno = sc.sno GROUPBY student.sno HAVINGavg(sc.score) > ( SELECTavg(sc.score) FROM student, sc WHERE student.sno = sc.sno AND student.sname ='王兵' GROUPBY student.sno LIMIT 1 );
SELECT sname FROM student WHERE sno IN ( SELECT sno FROM sc WHERE cno = ( SELECT cno FROM course WHERE cname ='离散数学' ) ) AND sno IN ( SELECT sno FROM sc WHERE cno = ( SELECT cno FROM course WHERE cname ='编译原理' ) );
(16)查询软件学院离散数学课程平均分;
1 2 3 4 5 6 7 8 9 10 11 12 13 14
SELECTavg(score) AS 平均分 FROM student, sc WHERE student.sNo = sc.sNo AND sc.cNo IN ( SELECT cNo FROM course WHERE cName ='离散数学' ) AND student.dNo IN ( SELECT dNo FROM department WHERE dName ='软件学院' );
(17)查询年龄与“软件学院”所有学生年龄都不相同学生姓名及其年龄和学院;
1 2 3 4 5 6 7 8 9 10 11 12 13
SELECT student.sname, student.age, department.dname FROM student LEFTOUTERJOIN department on student.dno = department.dno WHERE student.age <>ALL ( SELECT age FROM student WHERE dno = ( SELECT dno FROM department WHERE dname ='软件学院' ) AND age ISNOTNULL );
(18)查询各学院选修同一门课人数大于4的学院、课程及选课人数;
1 2 3 4 5 6 7 8 9 10 11
SELECT dp.dName, cs.cName, count(sc.sNo) FROM department dp LEFTOUTERJOIN student st ON st.dNo = dp.dNo LEFTOUTERJOIN sc ON sc.sNo = st.sNo LEFTOUTERJOIN course cs ON cs.cNo = sc.cNo
GROUPBY dp.dNo, cs.cNo HAVINGcount(sc.sNo) >4;
(19)查询仅仅选修了“高等数学”一门课程的学生姓名;(学号、姓名及所在学院名称)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
SELECT s.sno, s.sname, d.dname FROM sc NATURALJOIN student s LEFTJOIN department d ON s.dno = d.dno WHERE s.sno IN ( SELECT sno FROM sc GROUPBY sc.sno HAVINGcount(*) =1 ) AND s.sno IN ( SELECT sno FROM sc, course WHERE sc.cno = course.cno AND cname ='高等数学' );
(20)查询平均学分积小于70分的学生姓名。
1 2 3 4 5 6 7 8 9 10 11 12
SELECT sname FROM student WHEREexists( SELECT student FROM sc, course WHERE sc.cno = course.cno AND sc.sno = student.sno AND sc.score ISNOTNULL GROUPBY sc.sno HAVINGsum(sc.score * course.credit) /sum(course.credit) <70 );
(21)查询选修了“信息学院”开设全部课程的学生姓名。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
SELECT sname FROM student WHERENOTexists( SELECT* FROM course WHERENOTexists( SELECT* FROM sc WHERE sno = student.sno AND cno = course.cno ) AND course.dno = ( SELECT dno FROM department WHERE dname ='信息学院' ) );
SELECT sname FROM student WHERENOTexists( SELECT* FROM course WHERENOTexists( SELECT* FROM sc WHERE sno = student.sno AND cno = course.cno ) AND cno IN ( SELECT sc.cno FROM sc WHERE sc.sno = ( SELECT sno FROM student WHERE sname ='杨佳伟' ) ) ) AND sname <>'杨佳伟';
INSERTINTO book VALUES ('002', '计算机网络', 'James F. Kurose', '978-7-111-59971-5', '002', '7.0', to_date('2018-11-11', 'yyyy-mm-dd'));
(2)将其中一个出版社地址变更一下。
1 2 3
UPDATE publisher SET publisher_address ='上海浦东新区' WHERE publisher_no ='001';
(3)删除所插入数据。
1 2
DELETEFROM book; DELETEFROM press;
3、
(1)创建一个软件学院所有选修了“离散数学”课程的学生视图,并通过视图插入一行数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
CREATEVIEW view_student AS SELECT sno, sname FROM student WHERE dno = ( SELECT dno FROM department WHERE dname ='软件学院') AND sno IN ( SELECT sno FROM sc WHERE cno = ( SELECT cno FROM course WHERE cname ='离散数学')); INSERT INTO view_student VALUES ('2020302877', '项裕顺');
(2)创建一个各门课程平均分视图。
1 2 3 4 5 6
CREATEVIEW avg_score AS SELECT sc.cno, course.cname, avg(sc.score) FROM sc LEFTOUTERJOIN course ON sc.cNo = course.cNo GROUPBY sc.cno, course.cname;