数据库查询,例题备注,可以在以后遇到类似问题,查看查询方法
假设使用如下命令建立了一个grade表:
create table grade(low int,upp int,rank char(1))
insert into grade values(90,100,'A')
insert into grade values(80,89,'B')
insert into grade values(70,79,'C')
insert into grade values(60,69,'D')
insert into grade values(0,59,'E')
--现查询所有同学的Sno、Cno和rank列。
select t1.Sno,t1.Cno,t2.rank
from Score t1
left join grade t2
on t1.degree between t2.low and t2.upp
查询score中选学多门课程的同学中分数为非最高分成绩的记录。
select Sno from Score group by Sno having COUNT(Sno) > 1
select distinct Sno from Score where degree
in (select MAX(degree) from Score group by Cno)
select * from Score where Sno in
(select Sno from Score group by Sno having COUNT(Sno) > 1)
and Sno not in
(select distinct Sno from Score where degree
in (select MAX(degree) from Score group by Cno))
查询“张旭“教师任课的学生成绩。
select * from Teacher where Tname = '张旭'
select * from course
select * from score
select * from Teacher t1 full join Course t2
on t1.Tno = t2.Tno
full join Score t3
on t2.Cno = t3.Cno
where t1.Tname = '张旭'
查询选修某课程的同学人数多于5人的教师姓名。
select Tname from Teacher where Tno in (
select Tno from Course where Cno in
(select Cno from Score group by Cno having COUNT(Cno)>5)
)
查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
select * from Teacher
where (Depart = '计算机系' or Depart = '电子工程系')
and Prof in
(
select Prof from Teacher
group by Prof
having COUNT(*) <2
)
查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
select * from Score where Cno = '3-105' and DEGREE >
(select MAX(Degree) from Score where Cno = '3-245')