sql语句简单运用

来源:互联网 发布:云资源管理平台软件 编辑:程序博客网 时间:2024/05/25 12:20
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,class from Student
2、 查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher
3、 查询Student表的所有记录。
select * from student
4、 查询Score表中成绩在60到80之间的所有记录。
select * from score where degree between 60 and 80
select * from score where degree>=60 and degree<=80
5、 查询Score表中成绩为85,86或88的记录。
select * from score where degree in (85,86,88)
select * from score where degree =85 or degree=86 or degree=88
6、 查询Student表中“95031”班或性别为“女”的同学记录。
select * from student where class='95031' or ssex = '女'
7、 以Class降序查询Student表的所有记录。
select * from student order by class desc
8、 以Cno升序、Degree降序查询Score表的所有记录。
select * from score order by cno asc,degree desc
9、 查询“95031”班的学生人数。
select count(*) from student where class='95031'
10、查询Score表中的最高分的学生学号和课程号。
select * from score where degree = (select max(degree) from score)
11、查询‘3-105’号课程的平均分。
select avg(degree) from score where cno='3-105'
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select cno,avg(degree)
from score
where cno like '3%'
group by cno
having count(*)>=5
13、查询最低分大于70,最高分小于90的Sno列。
select sno
from score
group by sno
having min(degree)>70 and max(degree)<90
14、查询所有学生的Sname、Cno和Degree列。
select s.Sname,sc.cno,sc.degree
from score sc inner join student s on sc.sno = s.sno
15、查询所有学生的Sno、Cname和Degree列。
select sc.sno,c.cname,sc.degree
from score sc   inner join course c on sc.cno = c.cno
16、查询所有学生的Sname、Cname和Degree列。
select s.sname,c.cname,sc.degree
from score sc inner join student s on sc.sno=s.sno
inner join course c on c.cno = sc.cno
17、查询“95033”班所选课程的平均分。
select sc.cno,avg(sc.degree)
from score sc inner join student s on sc.sno = s.sno
where s.class='95033'
group by sc.cno
18、假设使用如下命令建立了一个grade表:
create table grade(low numeric(3,0),upp   numeric(3),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 sno,CNO,rank from SCORE,grade
where DEGREE between low and upp
19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select * from score where CNO='3-105' and DEGREE>( select DEGREE from SCORE where CNO='3-105' and SNO='109')


select allscore.* from SCORE allscore inner join SCORE score109 on allscore.CNO=score109.CNO
where score109.CNO='3-105' and score109.SNO='109' and allscore.DEGREE>score109.DEGREE


select allscore.* from SCORE allscore,SCORE score109
where allscore.CNO='3-105' and score109.SNO='109' and score109.CNO='3-105' and allscore.DEGREE>score109.DEGREE
23、查询“张旭“教师任课的学生成绩。
select sc.*
from SCORE sc inner join COURSE c on sc.cno = c.CNO
inner join TEACHER t on t.TNO=c.TNO
where t.TNAME = '张旭'
24、查询选修某课程的同学人数多于5人的教师姓名。
select t.TNAME
from COURSE c inner join TEACHER t on t.TNO=c.TNO
where c.CNO in
(
select cno
from SCORE 
group by CNO 
having COUNT(*)>5
)




select t.TNAME from 
COURSE c inner join TEACHER t on t.TNO=c.TNO
inner join (
select cno
from SCORE 
group by CNO 
having COUNT(*)>5
) newscore on newscore.CNO = c.CNO
25、查询95033班和95031班全体学生的记录。
select * from STUDENT  where CLASS in ('95031','95033')
26、查询存在有85分以上成绩的课程Cno.
select distinct cno from SCORE where DEGREE>85
27、查询出“计算机系“教师所教课程的成绩表。
select sc.*
from TEACHER t inner join COURSE c on c.TNO=t.TNO
inner join SCORE sc on c.CNO=sc.CNO
where t.DEPART='计算机系'
36、查询至少有2名男生的班号。
select CLASS
From STUDENT
where SSEX='男'
group by CLASS
having COUNT(*)>=2
37、查询Student表中不姓“王”的同学记录。
select * from STUDENT where SNAME not like '王%'
38、查询Student表中每个学生的姓名和年龄。
select SNAME,YEAR(GETDATE())-YEAR(SBIRTHDAY) from STUDENT--year获取一个时间中的年份
select GETDATE()--获取当前时间
40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
select * from STUDENT order by CLASS desc,YEAR(GETDATE())-YEAR(SBIRTHDAY) desc
41、查询“男”教师及其所上的课程。
select * from TEACHER t inner join COURSE c on t.TNO=c.TNO where t.TSEX='男'
--42、查询最高分同学的Sno、Cno和Degree列。
43、查询和“李军”同性别的所有同学的Sname.
select SNAME from student where ssex = (select ssex from STUDENT where SNAME='李军')
44、查询和“李军”同性别并同班的同学Sname.
select allstudent.* from STUDENT allstudent ,STUDENT lijunstudent
where allstudent.SSEX = lijunstudent.SSEX and allstudent.CLASS=lijunstudent.CLASS
and lijunstudent.SNAME='李军'
45、查询所有选修“计算机导论”课程的“男”同学的成绩表
select sc.*
from SCORE sc inner join COURSE c on sc.CNO=c.CNO
inner join STUDENT s on s.SNO=sc.SNO
where c.CNAME='计算机导论' and s.SSEX='男'