数据库,简单数据查询小命令

数据查询

一、实验目的

本实验的目的是使学生掌握SQL Server查询分析器的使用方法,加深对SQL和SQL语言的查询语句的理解。熟练掌握简单表的数据查询、数据排序和数据连接查询的操作方法。

二、实验内容

)、数据准备

)、练习使用Select语句实现数据查询

1. 单表查询

2. 查询结果排序:——order by 子句

3. 连接查询:——关系数据库中最主要的查询,Where子句

4. 使用带IN谓词的子查询——适用于集合查询

5. 使用带比较运算的子查询

6. 使用带Any, All谓词的子查询

7. 使用带Exists谓词的子查询和相关子查询

8. 使用聚集函数

9. 分组查询

10.集合查询

三、实验源程序(或实验步骤)

use xsgl
select * from student

use xsgl
select * from course

use xsgl
select * from sc

select sno, sname from student

select *  from student

select sname, '出生年份为: ', year(getdate()) - sage, lower(sdept) from student

select sname, '出生年份为: ' 出生, year(getdate())- sage  年份, lower(sdept)  系名from student

select distinct sno from sc

select cno, cname from course where cpno is null

select sno, grade from sc where cno='3' order by grade DESC

select * from student order by sdept ASC, sage DESC

select student.*, sc.* from student, sc where student.sno=sc.sno

select First.cno, Second.pcno  间接先行课from course First, course Second where First.pcno=Second.cno

select * 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 sno from student where sno in (selectsnofrom sc where cno='1')
and sno in (select sno from sc where cno='2')

select * from student where sage<  (select  sage from  student where sname='刘晨')

select sname, sage from student where sage <Any (select sage from student where sdept='IS') 
and sdept<>'IS'

select sname, sage from student where sage <ALL(select sage from student where sdept='IS') and sdept<>'IS'

select sno,sname,sage from student where sage<>all (select sage from student where sdept='CS')

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 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))

Select Count(*)  as 学生总数 from student

select count(distinct sno) as 选课学生总数 from sc

select sum(credit) as 总credit,avg(credit) as 课程平均学分,max(credit) as 最高学分min(credit) as 最低学分 from course

select avg(grade) as 平均成绩,max(grade) as 最高分, min(grade) as 最低分from scwhere cno='1'

select avg(grade) from student, course, sc where student.sno=sc.sno and course.cno=sc.cno and sdept='IS' and cname='数据结构'

Select sdept, Count(*) as 人数 from student group by sdept order by 人数 desc

select sdept,ssex,Count(*) as 人数 from student group by sdept, ssex order by sdept,ssex desc

select sno, sname from student where sno in (select sno from  sc group by (sno) having count(*)>3)

select sno, avg(grade) as 平均成绩,max(grade) as 最高分, min(grade) as 最低分 count(*) as 选课门数 from sc group by sno

select * from student where sdept=’MA’ union select * from student where sdept='IS'

select sno from sc where cno='1' Union select sno from sc where cno='2'

四、实验结果及分析

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值