3.4.1 单表查询

3.4.1 单表查询

一.选择表中 若干列

查询 全体学生的  学号  和 姓名

select   sno,  sname

from student

查询全体学生的   详细记录

select sno,  sname, ssex,  sage,  sdept

from student

select *

from student

 

查询 全体学生的  学号    姓名   所在系

select sno,sname,sdept

from student

查  全体学生的  姓名     出生年份

select sname, 2004-sage

 from student

select sname, 2004-sage as Sbirthyear

 from student

5  查询 全体学生的姓名  出生年份  所在系,,,,,,(用小写字母  表示 所有的 系名

 

 

select sname,      'Year of Birth:' as Sbirthyear  ,        2016-sage  , lower(sdept) 

 from student

取别名

select sname NAME,      'Year of Birth:' BIRTH  ,        2016-sage BIRTHDAY , lower(sdept) DEPARTMENT

 from student

 

 

 

二.选择表中 若干元组行  P93

6  查询 选修了 课程的   学生学号    distinct.note


select         sno

 

 from sc

 

 

等价

 

select all sno

 

 from sc

 

 

 

 

 

 

select  distinct sno

 

 from sc

 

7   查询 计算机系 全体学生的   名单.note


 

select  sname

 

 from student

 

where sdept='cs'

 

 

 

 

8  查询  所有年龄  在 20岁   以下的 学生姓名  及其年龄.note


 

select  sname,  sage

 

 from student

 

where sage<20

 

查询 考试成绩  有  不及格  学生的  学号.note


select distinct sno 

from sc

where grade < 60

 

 

10 查询 年龄在  20-23岁(包括  20  23)  之间   的 学生的  姓名  系别 年龄.note


 

 

select sname, sdept, sage

 

from student

 

where sage between 20  and 23

 

 

 

 

 

11 查询 年龄    不在在  20-23岁(包括  20  23)  之间   的 学生的  姓名  系别 年龄.note


12 查询 计算机系(CS)   数学系(MA)  信息系(IS)学生  的 姓名 和  性别.note


 

select sname, ssex

 

from student

where sdept in ('cs' , 'ma', 'is')

 

 

 

 

select sname, ssex

 

from student

where sdept='cs' or sdept='ma' or sdept='is'

 

 

 

 

13   查询   不是   计算机系(CS)   数学系(MA)  信息系(IS)学生  的 姓名 和  性别.note


select sname, ssex

 

from student

where sdept not in ('cs' , 'ma', 'is')


 

 

 

 like  14  查询 学号为  200215121的  学生的 详细情况.note


 

 

select *

 

from student

where  sno like '200215121'

 

 

 

 

 

 

not like

 

15  查询 所有  刘 姓   的 学生的   姓名, 学号, 性别.note


 

select *

 

from student

where  sname like  '%'

 

 

 

16 查询 姓 刘  且  全名 为2个汉字  的    学生 的 姓名.note


 查询 姓 "刘"  且  全名 为2汉字  的    学生 的 姓名

 

select *

 

from student

where  sname like '刘_'

 

17 查询 名字中的 第2个字  为  晨 字的    学生的  姓名  和 学号.note


查询 名字中的 第2个字  为 

 

 

 

 

select *

 

from student

where  sname like '_%'

 

 

18--15.note


查询 所有  不是  刘 姓   的 学生的   姓名, 学号, 性别

 

 

19 .note


21  有关空值的  查询.note


某些同学  选修课程后没有参加  考试

 

选课记录,,但是 没有  考试成绩

 

查询  缺少 成绩的学生 的 学号   和    相应的 课程号


 

 

select sno, cno

from sc

 

where grade is null


 


 

22.note


 

select sno, cno

from sc

 

where grade is not null

 

 

23  查询  计算机系 年龄在 20岁 以下  的  学生 的   姓名.note


 

select sname

from student

 

where sdept='cs' and sage<20

 

 

 

三.Order by子句

 

24  查询 选修了 3号课程的学生  的    学号  和  成绩,,(成绩 降序.note


 

 

select sno, grade

from sc

where cno='3'

order by grade desc

 

 

 

25  查询全体学生情况  系号升序asc    年纪 降序    desc.note


四.聚集函数 P98

 

26  查询学生  总人数.note


select count(*)  from student

 

 

27 查询 选修了 课程的 学生 人数.note


 

 

 

28 计算1 号  课程  学生的    平均成绩.note


29 查询 选修了1号 课程 的 学生  最高分数.note


 

 

 

30 查询 学生200215121    选修课程的   总的 学分数.note


 

 

 

 

 

五.Group by子句

 

32  查询 选修 3门 以及 以上  课程  的  学生 学号.note


 

 

select sno from sc 

group by sno

 

having count(*)>=3

 

 

 

 

 

 

31 求各个  课程号   及其  相应的 选课人数.note


 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangchuang2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值