SQL语句典型例子

本文介绍了多种SQL查询操作,包括统计学生选修课程数量、筛选特定年龄段学生、按姓名首字母查询、查找无成绩记录的课程、计算平均成绩、识别选修多门课程的课程号以及展示学生选修课程详情。这些查询涵盖了基础的SQL聚合函数、条件过滤和多表联查等实用技巧。
摘要由CSDN通过智能技术生成

1)查询被学生选修的课程有几个?(count)

select count(Cno)

from SC,C

where S.Sno = SC.Sno AND Sname = '张三'

select count(Cno)

from SC

where Sno in (

select Sno

from S

where Sname='张三')

select count(Cno)

from SC,C

where S.Sno = SC.Sno AND Sname = ‘张三’

select count(Cno)

from SC

where Sno in (

select Sno

from S

where Sname=‘张三’)

2)查询年龄在18-19之间的学生的信息?(between)

select *

from S 

where age between 18 and 19

select *

from S

where age between 18 and 19

3)查询姓刘的同学的信息?(like)

select *

from S

where Sname like '刘_'

select *

from S

where Sname like '刘%'

select *

from S

where Sname like ‘刘_’

select *

from S

where Sname like ‘刘%’

_表示一个汉字,%表示字符

4)查询没有选修课的成绩?(is null)

select *

from C

where Cpno is null

select *

from C

where Cpno is null

(其中where Cpno = null格式错误)

5)求各门课学生的平均成绩,显示课程号及相应的平均成绩(AVG,group by)

select Cno,avg(grade)

from Sc

group by Cno

select Cno,avg(grade)

from Sc

group by Cno

6)查询有2个以上的学生选修的课程的课程号(having)

select Cno

from SC

group by Cno

having count(Sno)>2

select Cno

from SC

group by Cno

having count(Sno)>2

7)查询学生选修课程的基本情况,要求显示学生号,课程号,成绩(三表查询)

select Sname,Cname,grade

from S,C,SC

where S.Sno = SC.Sno AND C.Cno = SC.Cno

select Sname,Cname,grade

from S,C,SC

where S.Sno = SC.Sno AND C.Cno = SC.Cno

8)查询了选修全部课程的学生姓名(不存在没有选修课程)

select Sname from S

where not exists(

select * from C

where not exists(

select * from SC

where S.Sno = SC.Sno AND C.Cno = SC.Cno))

select Sname from S

where not exists(

select * from C

where not exists(

select * from SC

where S.Sno = SC.Sno AND C.Cno = SC.Cno))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值