and prof not in --“计算机系”与“电子工程系”不同职称的教师Prof (select prof from teacher where depart ='计算机系' intersect
select prof from teacher where depart ='电子工程系')--“计算机系”与“电子工程系”相同职称的教师Prof
-- 30、查询选修编号为“3-105“课程且成绩至少高于一个选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 select cno,sno,degree from score where cno='3-105'--选修编号为“3-105”课程的同学
and degree>any -- 大于任意一个选修编号为“3-245”的同学的成绩 (select degree from score where cno='3-245')--选修编号为“3-245”的同学的成绩 order by degree desc
-- 31、查询选修编号为“3-105“课程且成绩高于所有选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 select cno,sno,degree from score where cno='3-105'--选修编号为“3-105”课程的同学
and degree>all -- 大于所有选修编号为“3-245”的同学的成绩 (select degree from score where cno='3-245')--选修编号为“3-245”的同学的成绩 order by degree desc
数据库
wzm
-- 32、查询所有教师和同学的name、sex和birthday. select sname as name,ssex as sex,sbirthday as birthday from student union
select tname as name,tsex as sex,tbirthday as birthday from teacher
-- 33、查询所有“女”教师和“女”同学的name、sex和birthday. select sname as name,ssex as sex,sbirthday as birthday from student where ssex='女' union
select tname as name,tsex as sex,tbirthday as birthday from teacher where tsex='女'
-- 34、查询成绩比该课程平均成绩低的同学的成绩表。 select * from score as s1 where degree<
(select avg(degree) from score as s2 group by cno having s1.cno=s2.cno)
-- 35、查询所有任课教师的Tname和Depart. select tname,depart from teacher where tno in (select tno from course)-- 课程表中存在的教师编号
-- 36、 查询所有未讲课的教师的Tname和Depart.
数据库
wzm
select tname,depart from teacher where tno in
(select tno from course where cno not in(select cno from course group by cno)) 备注:如果课程表中课程确定不为空,也可以如下编写: select Tname,Depart from Teacher where Tno not in (select Tno from Course)
-- 37、查询至少有2名男生的班号。
select class,ssex,count(ssex) as 男生人数 from student group by class,ssex having ssex='男' and count(ssex)>1; select Class,COUNT(*) from Student where Ssex='男'group by Class having COUNT(*)>=2;
-- 38、查询Student表中不姓“王”的同学记录。 select * from student where sname not like '王%'
-- 39、查询Student表中每个学生的姓名和年龄。
select sname,datediff(year,Sbirthday,current_timestamp)as 年龄 from student; select sname,datediff(year,Sbirthday,getdate()) as 年龄 from student; select sname,datepart(year,getdate())-datepart(year,Sbirthday) as 年龄 from student;
-- 40、查询Student表中最大和最小的Sbirthday日期值。
数据库
wzm
select datepart(year,max(sbirthday))as
max,datepart(year,min(sbirthday)) as min from student; select max(year(sbirthday))as max,min(year(sbirthday)) as min from student;
-- 41、以班号和年龄从大到小的顺序查询Student表中的全部记录。 select * from student order by class desc,Sbirthday
-- 42、查询“男”教师及其所上的课程。
select tname,tsex,cname from teacher left join course on course.tno=teacher.tno where tsex='男'
-- 43、查询最高分同学的Sno、Cno和Degree列。 select student.sno,cno,degree from student join Score on Score.sno=student.sno
where degree=(select max(degree) from score);
-- 44、查询和“李军”同性别的所有同学的Sname.
select sname from student where ssex= --与李军同性别的同学姓名 (select ssex from student where sname='李军')--李军的性别 and sname not in('李军')--从中去除李军
-- 45、查询和“李军”同性别并同班的同学Sname.
数据库 wzm
select sname from student where --与李军同性别并同班的同学姓名 ssex=(select ssex from student where sname='李军')----与李军同性别的同学姓名 and class=(select class from student where sname='李军')----与李军同班的同学姓名 and sname not in ('李军');--从中去除李军
-- 46、查询所有选修“计算机导论”课程的“男”同学的成绩表。 方法1:select degree from score join student on student.sno=score.sno join course on course.cno=score.cno where ssex='男' and cname='计算机导论' 方法2:select degree from score where Sno in(select sno from student where ssex='男')
and cno in(select cno from course where cname='计算机导论')
-- 47、查询出选修课程号为3-245和6-166的课程的学生学号与姓名 select student.Sno,sname from student join score on student.Sno=score.sno where cno in('3-245','6-166')
-- 48、查询出没有选修课程号为3-245和6-166的课程的学生学号与姓名
数据库
wzm
select student.Sno,sname from student join score on student.Sno=score.sno where cno not in('3-245','6-166')
数据库wzm