这是在练习中使用的表:

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

转载于:https://www.cnblogs.com/nextschool/p/4129374.html

SQLserver查询练习相关推荐

  1. 看懂SqlServer查询计划(转)

    转自:http://www.cnblogs.com/fish-li/archive/2011/06/06/2073626.html 对于SqlServer的优化来说,可能优化查询是很常见的事情.关于数 ...

  2. 看懂SqlServer查询计划

    原文:看懂SqlServer查询计划 对于SQL Server的优化来说,优化查询可能是很常见的事情.由于数据库的优化,本身也是一个涉及面比较的广的话题, 因此本文只谈优化查询时如何看懂SQL Ser ...

  3. MySql查询系统时间,SQLServer查询系统时间,Oracle查询系统时间

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. MySQL查询系统时间 第一种方法:select current_date: MySQL> s ...

  4. MySQL/sqlserver查询in操作 查询结果按in集合顺序显示

    2019独角兽企业重金招聘Python工程师标准>>> MySQL/sqlserver查询in操作 查询结果按in集合顺序显示 MySQL用下面的语句 select * from i ...

  5. MsSQL SQLServer 查询 表中字段的及类型是否为空

    MsSQL SQLServer 查询 表中字段的及类型是否为空 SELECT   name AS column_name,TYPE_NAME(system_type_id) AS column_typ ...

  6. SQLServer查询某天数据语法

    SQLServer查询当天数据语法 select * from compare_scan where 1=1 and IncomeTime BETWEEN '2021-9-14' AND datead ...

  7. sqlserver查询时间范围

    sqlserver 查询时间范围 sql得到当前系统时间得 日期部分CONVERT(varchar(10),getDate(),120)昨天select convert(varchar(10),get ...

  8. sqlserver查询锁住sql以及解锁

    果果虫 保持饥饿,保持愚蠢! sqlserver:查询锁住sql以及解锁 --查看被锁表: SELECT request_session_id spid, OBJECT_NAME( resource_ ...

  9. SQLserver查询锁表和解锁

    SQLserver查询锁表和解锁 由于写错了一个过滤条件kettle从用友U8数据库中拉取发票明细数据时数据量过大强制停止后仍然在无法查询,怀疑是被锁表了. 查询锁表语句 select request ...

  10. SqlServer查询重复数据

    SqlServer查询重复数据 1.查询单列重复: select * from test where name in (select name from test group by name havi ...

最新文章

  1. Tomcat修改端口、添加域名访问
  2. 从HttpServletRequest获取完整的请求路径
  3. 安装mysql5.3_源码安装mysql5.3.35
  4. avl删除根节点图解_图解 6 种树,你心中有数吗。。。
  5. Windows系统USB转CDC串口驱动限制说明
  6. swing 显示文件下文件_Linux 文件权限详解
  7. 把光标放在EditText中文本最后
  8. html加了文档声明之后页面错乱,为登陆页面扩展和配置设计导入程序
  9. 俄罗斯国家黑客TA505被指攻击金融机构
  10. MySQL版本升级5.6到5.7版本
  11. C# 数据库访问类源代码
  12. python获取快手无水印demo
  13. cad转dxf格式文件太大_如何将DWG DXF互转,一招教你解决难题
  14. 【React】利用Dooringx快速制作H5搭建平台
  15. Oracle PeopleSoft 应用安全加固
  16. 聚名网:华为申请“燃力红”商标,广告语注册商标需要符合哪些条件呢?
  17. 共用体和结构体所占内存大小的计算方法
  18. 消防应急照明和疏散指示系统——集中控制型系统的设计与应用
  19. C. Carrying Conundrum(思维)
  20. 《Scikit-Learn与TensorFlow机器学习实用指南》 第04章 训练模型

热门文章

  1. 来,说说什么是运维人的情怀?
  2. Linux进阶之给nginx设置登录用户验证
  3. python使用queue和线程池
  4. 国产数据库助力民航核心交易系统
  5. hdu--1077--Catching Fish
  6. 数据结构图之三(最短路径--迪杰斯特拉算法)
  7. 博客园文章markdown实现
  8. 在ASP.NET中运用JavaScript重定向页面
  9. QPG分布框架1.1.1
  10. 解决从github下载项目速度过慢