数据库查询积累

时间:2022-09-18 11:43:19

创建数据库:create table teddy(name varchar(20),age int,habbit varchar(50));定义关系模式

 

1.找出工资最低的老师姓名

select tname,salary from teacher
where salary <=all
(select salary from teacher);

2.找出001号课程不是最高的所有学生的学号

select s# from sc
where c#='001' and score <some
(select score from sc where c#='001' )

3.找出所有课程都不及格的学生姓名。

select sname from student
where 60>
all (select score from sc where s# =student.s#)

***4.列出没学过李明老师任何一门课的学生

select * from student
where not exists
(select * from teacher,sc,course
where sc.c#=course.c# and course.t#=teacher.t# and tname='李明'
and s#=student.s#)

 

将外查询表的每一行,代入内查询作为检验,如果内查询返回的结果取非空值,则EXISTS子句返回TRUE,这一行行可作为外查询的结果行,否则不能作为结果。

***5.列出至少学过98030101学生所学全部课程的学生的学号===》不存在一门课程该学生没有学过

select distinct s# from sc
where not exists
(select * from sc sc1 where sc.s#='98030101' and
not exists (select * from sc sc2 where c#=sc1.c# and s#=sc.s# ));

6.求薪水有差额(>0)的任意两位教师的薪水差额

select t1.tname n1,t2.tname n2,t1.salary-t2.salary

from teacher t1,teacher t2

where t1.salary>t2.salary;

7.根据年龄求出生年份,现在是2015

select sage,2015-sage+1 as birth from student;

***8.求不及格课程超过两门的学生学号

错在哪?

select s#,score from  sc where score<60 group by s#  应一对一

8.求学过001号或者学过002号课程的学生