SQLserver查询练习

这是在练习中使用的表:

--1.select dno from dept where dname=‘数学系‘

select sno,sname from student where dno=(select dno from dept where dname=‘数学系‘);

--2.求选修了课程的学生的学号

select distinct sno from sc ;

--3.求选修了01号课程学生的学号和成绩 ,查询结果按成绩升序排序,成绩相同按学号的降序排序
select sno,grade from sc where cno=‘01 ‘order by grade,sno desc

--4.求选修了01号课程且成绩在80-90之间的学生的学号和成绩,并将成绩乘以0.8输出.

select sno,grade=grade*0.8 from sc where cno=‘01‘ and grade between 80 and 90

--5.查询数学系或计算机系姓张的学生的信息
select * from student where dno in (select dno
from dept where dname in (‘数学系‘,‘计算机系‘));

--6.查看选修了课程,但没有成绩学生的信息.
select * from student where sno in (select sno
from sc where grade is null)

--7.查询学生的学号,成绩,姓名,课程名
select sc.sno,sname,cname,grade from sc,student,course
where sc.sno=student.sno and sc.cno=course.cno

--8.分别实现学生和系的交叉连接,内连接,外连接
--内连接:
select * from student ,dept where student.dno=dept.dno;
--外连接:
select * from student left outer join dept on (student.dno=dept.dno);

--9.选修了高等数学学生的学号和姓名
select sname ,sno from student where sno in (select sno from sc
where cno =(select cno from course
where cname=‘数学‘));

--10.求01号课程的成绩和高于周立波的学生的成绩和学号

select sno,grade from sc where grade > (select grade from
sc where sno=(select sno from student
where sname=‘周立波‘));

--11.求其他系的学生年龄小于计算机系最大年龄的学生

select max(sage) from student where dno=(select dno from dept
where dname=‘计算机系‘);

select sage from student where dno=(select dno from dept
where dname=‘计算机系‘);

select * from student where sage < (select max(sage) from student
where dno=(select dno from dept
where dname=‘计算机系‘)) and dno !=(select dno from dept
where dname=‘计算机系‘);

--12.求其他系中比计算机系中年龄都小的学生

select * from student where sage < all(select sage from student
where dno=(select dno from dept
where dname=‘计算机系‘)) and dno !=(select dno from dept
where dname=‘计算机系‘);

--13.求选修了02号课程学生的姓名
select sname from student where sno in(select sno from sc where cno=‘02‘)
--14.求没有选修01号课程学生的姓名
select sname from student where sno not in (select sno from sc where cno=‘02‘)

--15.查询选修了全部课程的学生的姓名
select sname from student where not exists(select * from course
where not exists (select * from sc
where sc.sno=student.sno and course.cno=sc.cno));

--16.求选修了学号为‘2014005‘学生所选修全部课程的学生的学号和姓名.
select sname ,sno from student where not exists(select * from sc
where not exists (select * from
(select * from sc where sno=‘2014005‘) as newtb
where
newtb.sno=student.sno and sc.cno=newtb.cno));

select distinct sno from sc s1
where not exists
(select * from sc s2 where s2.sno=‘2014005‘and not exists
(select * from sc s3 where s1.sno=s3.sno and s2.cno=s3.cno)
)

select sname,sno from student where sno in(select distinct sno from sc s1
where not exists
(select * from sc s2 where s2.sno=‘2014005‘and not exists
(select * from sc s3 where s1.sno=s3.sno and s2.cno=s3.cno)
))

--注意表之间的顺序
select sname ,sno from student where not exists(select * from
(select cno from sc where sno=‘2014005‘) as newtb
where not exists (select * from sc
where sc.sno=student.sno and sc.cno=newtb.cno));

时间: 2024-08-04 09:31:43

SQLserver查询练习的相关文章

利用SQLServer查询分析器获取存储过程的返回值,检查测试存储过程

1.存储过程没有返回值的情况(即存储过程语句中没有return之类的语句)用方法 int count = ExecuteNonQuery(..)执行存储过程其返回值只有两种情况(1)如果通过查询分析器执行该存储过程,在显示栏中如果有影响的行数,则影响几行count就是几(2)如果通过查询分析器执行该存储过程,在显示栏中如果显示'命令已成功完成.'则count = -1;在显示栏中如果有查询结果,则count = -1总结:A.ExecuteNonQuery()该方法只返回影响的行数,如果没有影响

如何为SQLSERVER查询分析器开启事务

很多人在使用SQLSERVER查询分析器的时候会遇到个问题,就是操作正式库的时候,担心增删改万一弄错了该咋办?数据是无价的! 难道我们每次都要去写事务语句去做这个事情吗? SMSS其实已经给我提供了这样一个功能,对查询分析器开启事务,也就是说,你在查询分析器中执行的语句不会立刻提交到数据库,而是在事务中执行,如果你确认操作无误可以COMMIT,发现有误需要更正就立刻ROLLBACK回来,数据完好无损,皆大欢喜.具体怎么用整理如下,给大家做个参考. Step1:打开SSMS,选择工具,选项,如图所

sqlserver查询数据的所有表名和行数

原文:sqlserver查询数据的所有表名和行数 //查询所有表明select name from sysobjects where xtype='u' select * from sys.tables //查询数据库中所有的表名及行数 SELECT a.name AS [TABLE NAME] , b.rows AS [RECORD COUNT] FROM sysobjects AS a INNER JOIN sysindexes AS b ON a.id = b.id WHERE ( a.t

[zhuan]SQLServer查询最近一天,三天,一周,一月,一季度方法

三天 select * from T_news where datediff(day,addtime,getdate())<= 2 and datediff(day,addtime,getdate())>= 0 一周 select * from T_news WHERE (DATEPART(wk, addtime) = DATEPART(wk, GETDATE())) AND (DATEPART(yy, addtime) = DATEPART(yy, GETDATE())) 注意:此时不能用

看懂SqlServer查询计划(转发)

看懂SqlServer查询计划 阅读目录 开始 SQL Server 查找记录的方法 SQL Server Join 方式 更具体执行过程 索引统计信息:查询计划的选择依据 优化视图查询 推荐阅读-MSDN文章 对于SQL Server的优化来说,优化查询可能是很常见的事情.由于数据库的优化,本身也是一个涉及面比较的广的话题,因此本文只谈优化查询时如何看懂SQL Server查询计划.毕竟我对SQL Server的认识有限,如有错误,也恳请您在发现后及时批评指正. 首先,打开[SQL Serve

sqlserver查询所有表的行数的sql语句

原文:sqlserver查询所有表的行数的sql语句 select a.name, b.rows  from sysobjects a inner join sysindexes b on a.id = b.id where a.type = 'u'   and b.indid in (0, 1)order by a.name

SQLServer 查询事务 结束事务 杀死事务 强制结束事务

1.查询事务 select request_session_id as spid,OBJECT_NAME(resource_associated_entity_id) as tableNamefrom sys.dm_tran_lockswhere resource_type='OBJECT'2.结束事务 杀死事务 强制结束事务kill 160 --spid由上面查询出来的spid SQLServer 查询事务 结束事务 杀死事务 强制结束事务

sqlserver查询的结果复制到excel替换掉回车换行

从sqlserver查询统计出的结果复制到excel,如果有回车,换行 ,或回车换行 ,复制到excel显示会乱会错版,查询的时候就替换掉回车换行,复制出来就不会乱了 select top 100 REPLACE(REPLACE(REPLACE(remark, CHAR(13) + CHAR(10), ''),CHAR(13),''),CHAR(10),'') from tablename

看懂SqlServer查询计划

原文:看懂SqlServer查询计划 对于SQL Server的优化来说,优化查询可能是很常见的事情.由于数据库的优化,本身也是一个涉及面比较的广的话题, 因此本文只谈优化查询时如何看懂SQL Server查询计划.毕竟我对SQL Server的认识有限,如有错误,也恳请您在发现后及时批评指正. 首先,打开[SQL Server Management Studio],输入一个查询语句看看SQL Server是如何显示查询计划的吧. 说明:本文所演示的数据库,是我为一个演示程序专用准备的数据库,

sqlserver查询数据库中有多少个表

sqlserver查询数据库中有多少个表 <div class="articalTitle" style="clear:both; line-height:20px; padding-bottom:10px;> SELECT * FROM sysobjects WHERE (xtype = 'U') C = CHECK 约束 D = 默认值或 DEFAULT 约束 F = FOREIGN KEY 约束 L = 日志 FN = 标量函数 IF = 内嵌表函数 P =