(1)查询计算机系学生的修课情况,要求列出学生的名字、所修课的课
程号和成绩。
select SNAME,cno from sc ,student where dept = '计算机系'
(2)查询“信息管理系”修了“计算机文化学”的学生姓名和成绩。
select sname ,grade,dept from
student s join sc on s.sno=sc.sno
join course c on c.cno=sc.cno
where cname = '计算机文化学' and DEPT = '信息管理系'
(3)查询所有选修了Java课程的学生情况,列出学生姓名和所在系。
SELECT SNAME ,DEPT
FROM STUDENT S JOIN SC ON S.SNO=SC.SNO JOIN COURSE C ON SC.CNO=C.CNO
WHERE CNAME = 'Java'
(4)统计每个系的学生的考试平均成绩。
select dept ,AVG(grade) as 平均成绩
FROM STUDENT S JOIN SC ON S.SNO=SC.SNO
GROUP BY Dept
(5)统计计算机系学生每门课程的选课人数、平均成绩、最高成绩和最
低成绩。
select CNO, COUNT(*),AVG(grade),MAX(grade),min(grade)
from student s join sc on s.sno=sc.sno
GROUP BY dept,cno HAVING DEPT = '计算机系'
(6)查询与刘晨在同一个系学习的学生的姓名和所在的系。
SELECT s2.Sname,s2.Dept FROM student s1 JOIN student s2 on s1.Dept=s2.Dept
WHERE s1.Sname='刘晨' AND s2.Sname != '刘晨'
(7)查询与“数据结构”在同一个学期开设的课程的课程名和开课学期。
SELECT s2.Cname,s2.Semester FROM course s1 JOIN course s2 on s1.Semester=s2.Semester
WHERE s1.Cname='数据结构' AND s2.Cname != '数据结构'
(8)查询至少被两个学生选的课程的课程号。
SELECT Cno FROM student s JOIN sc ON s.Sno=sc.Sno
GROUP BY Cno HAVING count(s.Sno)>=2
(9)查询全体学生的选课情况,包括选修了课程的学生和没有选修课程的学生。
SELECT Sname,GROUP_CONCAT(Cname) FROM student LEFT JOIN sc ON student.Sno=sc.Sno LEFT JOIN course ON course.Cno=sc.Cno
GROUP BY student.Sno
(10)查询没人选的课程的课程名。
SELECT Cname FROM course LEFT JOIN sc ON sc.Cno=course.Cno
WHERE sc.Cno IS null
(11)查询计算机系没有选课的学生,列出学生姓名和性别。
SELECT Sname,Ssex FROM student LEFT JOIN sc ON student.Sno=sc.Sno
WHERE sc.Cno IS NULL AND Dept='计算机系'
(12)统计计算机系每个学生的选课门数,包括没有选课的学生。
SELECT Sname,Dept,count(sc.Sno) as 选课门数 FROM student LEFT JOIN sc ON student.Sno=sc.Sno
GROUP BY student.Sno,Dept HAVING Dept='计算机系'
(13)查询信息管理系选课门数少于3门的学生的学号和选课门数,包括没有选课的学生。查询结果按选课门数递增排序。
SELECT student.Sno,count(sc.sno) FROM student LEFT JOIN sc ON student.Sno=sc.Sno
GROUP BY student.Sno,Dept HAVING Dept='信息管理系'AND count(student.Sno)
ORDER BY count(student.Sno)
(14)查询考试成绩最高的三个成绩,列出学号、课程号和成绩。
SELECT student.Sno,sc.Cno,Grade FROM student LEFT JOIN sc ON student.Sno=sc.Sno
ORDER BY Grade DESC
LIMIT 3
(15) 查询Java考试成绩最高的前三名的学生的姓名、所在系和VB考试成绩。
SELECT Sname,Dept,Grade FROM student JOIN sc ON student.Sno=sc.Sno JOIN course on course.Cno=sc.Cno
WHERE Cname='java'
ORDER BY Grade DESC
LIMIT 3
(16)查询选课人数最少的两门课程(不包括没有人选的课程),列出课
程号和选课人数。
SELECT cno ,COUNT(sno) as 人数
from sc
GROUP BY cno
ORDER BY COUNT(sno)
LIMIT 2
(17)查询计算机系选课门数超过2门的学生中,考试平均成绩最高的前
2名(包括并列的情况)学生的学号、选课门数和平均成绩。
SELECT s.Sno,
AVG(Grade)AS 平均成绩 ,
COUNT(*) AS 选课门数
FROM student s JOIN sc ON s.Sno = sc.Sno
WHERE dept = '计算机系'
GROUP BY s.Sno HAVING COUNT(*) > 2
ORDER BY AVG(Grade) DESC
LIMIT 2
(18)将计算机系的学生信息保存到#ComputerStudent局部临时表中。
SELECT Sno,Sname,Ssex,Sage
INTO #ComputerStudent
FROM student WHERE dept = '计算机系'
(19)将选了Java课程的学生的学号及成绩存入永久表Java_Grade中。
select sno , grade
into Java_Grade
from sc join course c on sc.cno = c.cno
where cname = 'java'
(20)统计每个学期开设的课程总门数,将结果保存到永久表Cno_Count
表中
select Cname ,COUNT(*) into Cno_Count
from course
group by Semester
(21)查询第2学期开设的课程名、学分和课程总门数。
select cname ,credit ,COUNT(*)
from course
GROUP BY semester having semester =2
(22)查询考试成绩大于90分的学生的学号和姓名。
select s.sno,sname
from student s join sc on s.sno = sc.sno
where grade >90
(23)查询计算机系选了“C002”课程的学生,列出姓名和性别。
select sname ,ssex
from student s join sc on s.sno = sc.Sno
where cno = 'c002' and dept = '计算机系'
(24)查询选修了“Java”课程的学生的学号和姓名。
select sname ,s.sno
from student s join sc on s.sno = sc.Sno join course c on sc.cno = c.cno
where cname = 'Java'
(25)统计选了Java课程的这些学生的选课门数和平均成绩。
select s.sno ,count(*),avg(grade)
from student s join sc on s.sno = sc.Sno join course c on sc.cno = c.cno
GROUP BY cname
having cname = 'java'
(26)查询选了“JAVA”课程的学生学号、姓名和JAVA成绩。
select student.sno,sname,grade
from student join sc on sc.sno = student.sno
join course on course.cno = sc.cno
where cname='Java';
#(27)查询选了“C004”号课程且成绩高于此课程的平均成绩的学生的学号和成绩。
select sno,grade
from sc
where cno='C004' and grade >
(select AVG(grade)
from sc
where cno='C004');
#(28)查询考试平均成绩高于全体学生的总平均成绩的学生的学号和平均成绩。
select sc.Sno,grade
from sc
where grade >
(select AVG(grade)
from sc
where grade is not null
);
#(29)查询没选“C001”号课程的学生姓名和所在系。
select sname,dept
from student
where sno not in (select sno
from sc
where cno = 'C001');
#(30)查询计算机系没选JAVA课程的学生姓名和性别。
select sname,ssex
from student
where dept='计算机系' and sno not in (select sno
from sc join course on sc.cno = course.cno
where cname='Java'
);
# (31) 查询其他学期开设的课程中比第1学期开设课程的最大学分小的课程名、开课学期和学分。
select cno,Semester,credit
from course
where Credit < (select max(credit)
from coursewhere Semester=1);
#(32)查询至少有一次成绩大于等于90的学生的姓名,所修的课程号和成绩。
select sname,cno,grade
from student join sc as s1 on student.Sno=s1.Sno
where EXISTS (select *from sc as s2 where grade >= 90 and student.Sno=s2.Sno);
#(33)查询比第1学期开设的所有课程的学分都小的其他学期开设的课程名、开课学期和学分。
Select cname,Semester,credit
from course
where credit < all(select credit
from course
where Semester=1) and Semester<>1;
#(34)查询每个学期学分最低的课程的课程名、开课学期和学分。
select cname,Semester,Credit
from course as c1
where credit < all(select credit
from course as c2
where c1.cno<>c2.cno and c1.Semester=c2.Semester);
#(35)查询每门课程考试成绩最高的两个学生的学号以及相应的课程号和成绩。不包括没考试的课程。
select top 2 s1.sno,s1.cno,grade
from sc as s1
where s1.sno in (
select top 2 sno
from sc as s2
order by grade desc
)
order by s1.cno,s1.grade desc;
#(36)查询每门课程中,考试成绩低于该门课程的平均成绩的学生的学号和成绩。
select sno,grade
from sc as s1
where s1.grade <(
select AVG(Grade)
from sc as s2
where grade is not null and s1.Cno=s2.cno );
#(37)查询有最高学分超过本学期平均学分1.5倍的学期。
select Semester
from course as x
GROUP BY Semester
having Max(Credit)>(select AVG(Credit)
from course as y
where x.Semester=y.Semester);
# (38)查询学生姓名、所在系和该学生选的课程门数。
select sname,dept,count(cno) as 课程门数
from student
join sc on student.sno=sc.sno
GROUP BY sname,dept;
#(39)查询课程名、开课学期及选该门课的学生人数、平均成绩。不包括没人选的课程。
select cname,Semester,count(sno),avg(grade)
from course join sc on sc.cno=course.cno
where grade is not null
GROUP BY cname,Semester;
# (40)使用exists查询选了“C002”课程的学生姓名。
select DISTINCT sname
from student join sc as s1 on student.sno=s1.sno
where exists (
select *
from sc as s2
where cno = 'C002' and s2.sno = s1.Sno
);
#(41)使用exists查询选了JAVA课程的学生姓名和所在系。
select DISTINCT sname,dept
from student join sc as x on student.Sno=x.Sno
where exists (select *
from sc as y join course on y.cno=course.cno
where cname='Java' and x.sno=y.Sno
);
#(42)使用exists查询没有选修“C001”课程的学生姓名和所在系。
select sname,dept
from student
where not exists(select *from scwhere cno='C001' and student.sno=sc.sno);
#(43)使用exists查询计算机系没选JAVA的学生姓名和性别。
select sname,ssex
from student
where Dept='计算机系 ' and not exists (select *from sc join course on sc.cno=course.cno
where cname='Java' and sc.Sno=student.sno);
#(44)使用exists查询至少选了全部课程的学生的学号、姓名和所在系。
select sno,sname,dept
from student
where not exists(
select *
from sc as s1
where not exists (
select *
from sc as s2
where s1.sno=s2.sno and student.Sno=s2.Sno
)
);
#(45)使用exists查询至少选了“0811102”学生所选的全部课程的学生的学号和所选的课程号。
select sno,cno
from sc as s1
where not exists (
select *
from sc as s2
where not exists (
select *
from sc as s3
where s3.cno=s2.cno and s1.sno=s3.sno
)and s2.sno='0811102'
)and s1.sno<>'0811102';