SQL常用查询总结_sql常用查询
SQL常用查询总结由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“sql常用查询”。
1)查询全体学生的学号、姓名、所在系 select Sno,Sname,Sdept from Student
2)
查询全体学生的详细信息
select * from Student
3)
查询全体学生的姓名及其出生日期 select Sname,Sbirthday from Student
4)
查询软件工程系全体学生的名单
select * from Student
where Sdept='软件工程系'
5)
查询所有年龄在20岁以下的学生姓名以及年龄(注:GETDATE()函数可获得当前的日期,YEAR()函数可返回日期中的年份)
Select Sname ,(year(getdate())-YEAR(Sbirthday))as age from Student
where(year(getdate())-YEAR(Sbirthday))
6)
查询考试成绩不及格的学生的学号 select Sno from Score where Grade
7)
查询出生日期在1990年1月1日~1995年12月31日之间的学生的姓名、所在系和出生日期
select Sname,Sdept,Sbirthday from Student
where Sbirthday between '1990-1-1 0:00:00' and '1995-12-31 0:00:00'
8)
查询不在信息系、数学系也不在软件工程系学生的姓名和性别
select Sname,sex from Student
where Sdept not in('信息系','数学系','软件工程系')
9)
查询所有姓李且全名为三个汉字的学生的姓名、学号和性别
10)查询姓名中第2个字为“阳”字的学
生的姓名和学号
select Sname,Sno
from Student
where Sname like '%阳%'
11)查询软件工程系年龄在20岁以下的学生姓名
Select Sname ,(year(getdate())-YEAR(Sbirthday))as age from Student
where(year(getdate())-YEAR(Sbirthday))
12)查询选修了3号课程的学生的学号
及其成绩,查询结果按分数的降序排列
select Sno,Grade from Score where Cno=3 order by Grade desc
13)查询全体学生情况,结果按所在系的升序排列,同一系的按年龄降序排列
select * from Student
order by Sdept asc,Sbirthday desc
14)统计学生总人数
select Sno from Student compute count(Sno)
15)查询选修了课程的学生人数
select distinct Student.Sno,Score.Sno from Student,Score
where Student.Sno=Score.Sno compute count(Student.Sno)
16)计算1号课程的学生平均成绩
select Grade from Score where Cno=1 compute avg(Grade)
17)查询选修了1号课程的学生最高分
数
select * from Score where Cno=1 compute max(Grade)
18)求各课程号及相应的选课人数
select Sno,Cno from Score order by Cno
compute count(Sno)by Cno
19)查询选修了3门以上课程的学生学
号
20)查询选修2号课程且成绩在90分以
上的所有学生的学号、姓名
select Student.Sno,Student.Sname from Student,Score
where Student.Sno=Score.Sno and Score.Cno=2 and Score.Grade>90
21)查询每个学生的学号、姓名、选修的课程名和成绩
select Student.Sno,Student.Sname, Course.Cname,Score.Grade from Course,Score,Student where
Student.Sno=Score.Sno
and
Score.Cno=Course.Cno
22)查询所有选修了1号课程的学生姓
名
select distinct Student.Sno,Score.Sno,Student.Sname from Student,Score
where Student.Sno=Score.Sno and Score.Cno=1
23)查询选修了课程名为“数据库”的学
生的学号和姓名
select distinct Student.Sno,Student.Sname from Student,Course,Score
where Student.Sno=Score.Sno and Course.Cname='数据库'