sql记录

  学习数据库一个月,来记录一下数据库命令和用法。

  此文档都是关于sql查询命令,不涉及增、删、改,这三项会在后续,文档持续更新......

  此文档涉及三个表,分别是Student,SC,Course。三个表分别为学生表、选课表,系表,因文档尚不完善三个表会在后续文章给出。不妥之处还请见谅。

 

select *                   /*查询Student表内所有数据*/
from Student

select sname         /*查询Student表内所有学生姓名*/
from Student

select sno              /*查询Student表内所有学生学号*/
from SC

select DISTINCT ssex    /*取消重复行: DISTINCT*/
from Student

 

/*WHERE子句*/

 

 

select sname
from Student    
where sdept='计算机'     

select sname                /*查询Student表内所有年龄大于等于20岁的学生姓名*/
from Student    
where sage>=20

select distinct sno              /*查询考试成绩不及格的学生*/
from SC
where grade<60

   /*划分等级*/

select sno,cno,grade      
case        
where grade>=80 then '优秀'
where grade>=60 then '及格'
else  '不及格'
end
from SC

select sname,sdept,sage                         /*查询20-23岁学生的姓名,系名,年龄*/
from Student
where sage BETWEEN 20 AND 23         /*确定范围: between...and...*/

select sname,sdept,sage
from Student
where sage NOT BETWEEN 20 AND 23         /*确定范围: not between...and...*/

select sname,ssex
from Student
where sdept IN('计算机','数学')        /*确定集合: in查询属性值指定集合的元组*/

select *
from Student
where sname LIKE'王%'                /*模糊查询: like‘<匹配串>’ 【ESCAPE】‘<换码字符>’通配符%和_*/

select cno,ccredit
from Course
where cname LIKE'数据\_构' escape'\'       /*_不再具有通配符意义*/

 

select sno,cno
from SC
where grade IS NOT NULL               /*不为空查询:is not null 只能用is,不能用=*/

select sname
from Student
where sdept='计算机' AND sage>20        /*多重条件查询 AND OR,ANR优先级高于OR*/

select sno,grade            /*排序:ORDER BY;ASC升序,DESC降序;默认升序*/
from SC
ORDER BY grade DESC

select * 
from Student
ORDER BY sdept,sage 

 

 

/*SELECT子句*

 


select COUNT(*)                /*COUNT(*): 统计元组个数*/
from Student

select COUNT(DISTINCT sno)    /*COUNT([distinct|all]<列名>): 统计一列中值的个数*/
from SC

select AVG(DISTINCT grade)    /*AVG([distinct|all]<列名>): 统计一列中值的平均值*/
from SC
where cno='1'                             /*取一号课程班级平均分*/


select cno,COUNT(sno)            /*取各个课程对应选课人数*/
from SC
ORDER BY cno

--查询选修三门以上课程的学生学号
select sno
from SC
ORDER BY sno
having COUNT(*)>3

--查询平均成绩大于等于90分的学生学号和平均成绩
select sno,AVG(grade)
from SC
ORDER BY sno
having AVG(grade)>=90

 

--连接查询

--自然连接

select Student.sno,sname,ssex,sage,sdept,cno,grade
from Student,SC
where Student.sno=SC.sno

--查询选修2号课程切成绩在90分以上所有学生的学号和成绩
select Student.sno,grade
from SC,Student
where Student.sno=SC.sno AND
      SC.cno='2' AND

SC.grade>=90

--自身连接

--查询每一门课程的间接选修课程(起别名)
select first.cno,second.cpno
from Course first,Course second
where first.cno=second.cno

--多表连接

--查询每个学生的学号、姓名、选修课程及成绩
select Student.sno,sname,cname,grade
from Student,SC,Course
where Student.sno=SC.sno AND SC.cno=Course.cno

 

--嵌套查询

--查询与‘刘晨’在同一个系的同学
select sno,sname,sdept
from Student
where sdept IN(
        select sdept
        from Student

where sname='刘晨'

)

 

查询选修了课程名为‘信息系统’的学生学号和姓名

--嵌套
select sno,sname
from Student
where sno IN(
        SELECT sno
        from SC
        where cno IN(
        select cno
        from Course
        where cname='信息系统'
        ))


--连接
select Student.sno,sname
from Student,SC,Course
where Student.sno=SC.sno
and   SC.cno=Course.cno
AND   Course.cname='信息系统'

--查询非计算机系中比计算机系所有学生年龄都小的学生姓名和学生年龄

--一:
select sname,sage
from Student
where sage<All(
        select sage
        from Student
        where sdept='计算机')

AND sdept<>'计算机'

--二:
select sname,sage
from Student
where sage<(select MIN(sage)
            from Student
            where sdept='计算机')
AND sdept<>'计算机'

--找出每个学生超过他自己选修课程平均成绩的课程号
select sno,cno
from SC x
where grade>=(select AVG(grade)
                from SC y
                where y.sno=x.sno)

--查询非计算机系中比计算机系任意一个学生年龄小的学生姓名和年龄
select Sname,sage
from Student
where sage<any(select sage
            from Student
            where sdept='计算机')
and sdept<>'计算机'
select sname,sage
from Student
where sage<(
        select MAX(sage)
        from Student
        where sdept='计算机')
  AND sdept<>'计算机'
        

--查询所有选修1号课程的学生姓名(EXISTS存在)
select sname
from Student
where EXISTS(
        select *
        from SC
        WHERE sno=Student.sno AND cno='1'
)

--查询选修了全部课程的学生的姓名
select sname
from Student
where NOT EXISTS(
  select *
  from Course
  where NOT EXISTS(
        select *
        from SC
        where sno=Student.sno
        and cno=Course.cno)
  
)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值