数据库查询语句大全
部分一:
Student(Sno,Sname,Sage,Ssex)
SC(Cno,Sno,Grade)
Course(Cno.Cname,Cpno,Ccredit,Teacher)
1.查询w老师教授的课程和课程号
2.查询年纪大于23岁的男生信息
3.查询至少选修W老师的一门课程的女生
Answer:
a)select cnoame
from course
where teacher=’w’
b)select *
from student
where Ssex=’男’ and Sage > 23
c)方法一:
select sname
from student,sc,course
where student.Ssex=’女’
and student.Sno=SC.snosql语句查询不包含
and SCo=courseo
acher=’w’
方法二:
Select sname
from student
where Ssex=’女’ and son in
(select sno from sc where cno in
(select cno from course where teacher=’w’))
部分二:
Student(Sno,Sname,Sage,Ssex)
SC(Cno,Sno,Grade)
Course(Cno.Cname,Cpno,Ccredit,Teacher)
1.统计所有学生选修的课程门数
2.统计每门课程选修的人数
3.求w老师所教授的每门课程的平均成绩和总人数
4.统计选修课程2的同学的平均成绩
5.统计每门课程的学生人数(要求超过2人才统计)要求输出课程号和选修人
数,结果按照人数的降序排序,人数相同的话按照课程号升序排序
6.查询姓名以王开头的所有学生
7.查询学号比王开头的所有学生大,年纪也比他大的学生
8.出年纪大于女生平均年纪的男生
9.出在SC表没有成绩,但是在Student中存在的学生
Answer:
1)Select count (dinstinct cno) from SC //distinct获取不重复字段
2)Select count (sno) as 选修数, cno
from sc
group by cno
3)Select count (SC.sno), SCo, A VG(grade)
from SC
where SCo=Courseo acher=’w’ group by sco
4)Select avg(grade) from SC where cno=’2’
5)Select cno,count(sno) from SC group by cno
having count(sno) >2 order by 2 desc, 1
6)Select * from student where Sname like ‘王%’
7)Select * from student where cast(sno as int (10))>all //cast强制转换类型
(select cast (sno as int (10) ) from student where sname like ‘王%’)
and sage>all(select Sage from student where Sname like ‘王%’)
8)Select * from student where Ssex=’男’and Sage>(select avg(Sage) from where
Ssex=’女’)
9)Select * from student where sno not in(select sno from SC) or sno in (select sno
from SC where grade is null)//更正后
Student(Sno,Sname,Sbirth,Ssex)
SC(Cno,Sno,Grade)
Course(Cno.Cname,Cpno,Ccredit,Teacher)
部分三:
1.查询选修总学分在10学分以下的学生姓名
2.查询各门课程的最高成绩的学生姓名及其成绩
3.查询选修20040744009号学生所选修的全部课程的学生学号(课本110页)
4.查询与‘曾福德‘在同一个系学生的学号、姓名和系别
5.查询其他系中比电子工程系某一学生年龄小的学生姓名和年龄
1)Select SC.sno, dit) as 学分from SC,Course
where SCo=courseo
group by sc.sno
having dit)<10 //sum求和count统计次数或:
Select sname,sno from student where sno in
(select sc.sno from sc,course
where sco=courseo group by sc.sno having sum(course.Credit)<10)
问题:如何简化,还要显示总学分?
把内查询设计为视图chengji_10就可以
或:
select sc.sno
from sc,course
where sco=courseo group by sc.sno having sum(course.Credit)<10
接着直接用:
select s.sname,s.sno,cj.fen from student as s,chengji_10 as cj
where s.sno = cj.sno
还可以显示总学分。
2)方法一:用到SC表的自身连接
Select a.sno ,s.sname,ade
From SC a, student s
ade=(select ade)
from SC b
group by bo
having bo=ao)
and so=a.sno
方法二:视图………
Select cno,max(grade) as bigfen(定义的一个属性[最大成绩])
from SC
group by cno
/
/此结果作为一个视图bigefen
Select SC.sno, binfeno ,bigfen
from SC, bigfen
where SCo=bigfeno ade = bigfen.bigfen 再把上面的结果作为视图bigfen2
接着和Student表格进行连接
Select S.sname, bo, b.bigfen
from student S, bigfen2    b
where S.sno = b.sno
3)利用视图简化查询(全称转换为存在)
Create view course01
as
select cno from sc
where sno=’20040744009’ //存为视图course01
//再从视图从查询
Select sno, sname,
from student where not exists
( select * from course01 where not exists
(select * from SC where SC.sno = student.sno
and SCo = course01o)
)
4)Select sno, sname, sdept from student where sdept=
(select sdept from student where sname=’曾福德’)
5)Select sno, sname, sname, year(Sbirth) as 年纪
From student where year(Sbirth)<any(select (year(getdate()-year(Sbirth) ) )
From student where sdept = ‘电子信息工程’)
常用的SQL语句
1.建表:
Create table Student
( sno char (10) primiary key;/*属性为主码*/
Sanme char(10) unique;/*属性取唯一值*/
Ssex smallint;
Sdept char(20);
Sbirth char(8);
)
;
2.修改表:
Alter table student ADD s-enterance DATE;
3.删除表:
Drop table student CASCADE(级联删除)/RESTRICT(限制)若有关联时不允许删除
单表查询
1、查询全体学生的学号与姓名
Select sno,sname
from student
2、查询全体学生的姓名、学号、所在系
select sname,sno,sdept
from student
3、查询全体学生的详细记录
select *
from student
4、查询全体学生的姓名及年龄(注意:是年龄,不是年月日)
select sname,year(getdate())-year(sbirth) /*当前年份-生日的年份=年龄*/ from student
5、查询全体学生的姓名、出生年份(并以year of birth作为字段名)和所在的院系,要求用小写字母表示所有系名
select sname,year(getdate())-year(sbirth) as year of birth,lower(Sdept)as Sdept from student/*小写字母*/
6、查询选修了课程的学生学号(观察表,有些学生并没有选择任何课程),并思考如何去掉重复的行号
select distinct sno/*DISTINCT取消重复的列*/
from sc
7、查询计算机科学系全体学生的名单
select sname
from student
where sdept=’计算机科学系’
8、查询所有年龄在24岁以下的学生姓名、出生年份以及出生年龄
select sname,year(sbirth) as 出生年份,year(getdate())-year(sbirth) as 年龄from student
where [year(getdate())-year(sbirth)]<24
9、查询考试成绩有不及格的学生的学号
select distinct sno/*排除一个人学生选修多门课程的情况*/
from sc
where grade<60
10、查询年龄在23~24岁(包括23岁和24岁)之间的学生姓名、系别和出生年份;
select sname,sdept,year(sbirth)
from student
where year(sbirth) between 23 and 24;
11、查询年龄不在23~24岁之间的学生姓名、系别和出生年份;
select sname,sdept,year(sbirth)
from student
where year(sbirth) not between 23 and 24;
12、查询计算机科学系、数学系和通信工程系学生的姓名和性别;
select sname,ssex
from student
where sdept=’计算机科学系’ or sdept=’数学系’ or sdept=’通信工程系’;
/*where sdept in(‘计算机科学系’,’数学系’,’通信工程系’)*/
13、查询既不是计算机科学系、也不是数学系和通信工程系学生的姓名和性别;
select sname,ssex
from student
where sdept not in(‘计算机科学系’,’数学系’,’通信工程系’)
14、查询学号前几个数字为2004074400的学生的详细情况
select *
from student
where sno like’2004074400%’
15、查询所有姓王的学生的姓名、学号和性别;
select sname,sno,ssex
from student
where sname like’王%’
16、查询姓“贺”且全名为3个汉字的学生的姓名(思考:若全名为2个汉字的如何查询,得到什么结果)
select sname
from student
where sname like’贺__ __’;
17、查询名字中第2个字为“永”字的学生的姓名和学号
select sname.sno
from student
where sname like ’__永%’;
18、查询所有不姓“贺”的学生的姓名
select sname
from student
where sname not like ‘贺%’
19、查询“数论基础”课程的课程号和学分(理解课本例19中escape的用法)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。