SQL查询语句47题
select * from student
select * from score
--select * from grade
select * from course
select * from teacher --1、 查询Student表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,class from Student
--2、 查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher
--3、 查询Student表的所有记录。
select * from student
--4、 查询Score表中成绩在60到80之间的所有记录。
select * from score where degree between 60 and 80
--5、 查询Score表中成绩为85,86或88的记录。
select * from score where degree in (85,86,88)
--6、 查询Student表中“95031”班或性别为“女”的同学记录。
select * from student where class='' or ssex='女'
--7、 以Class降序查询Student表的所有记录。
select * from student order by class desc
--8、 以Cno升序、Degree降序查询Score表的所有记录。
select * from score order by cno,degree desc
--9、 查询“95031”班的学生人数。
select COUNT(*) from student where class=''
--10、查询Score表中的最高分的学生学号和课程号。
select sno,cno from score where degree in (select MAX(degree) from score)
select top 1 sno,cno from score order by degree desc
--11、查询‘3-105’号课程的平均分。
select AVG(degree) from score where cno='3-105'
--12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select AVG(degree) from score where cno in (select cno from score group by cno having COUNT(*)>=5) and cno like '3%'
select AVG(degree) from score where cno like '3%' group by cno having COUNT(*)>=5
--13、查询最低分大于70,最高分小于90的Sno列。
select distinct sno from score
where sno in (select sno from score group by sno having MIN(degree)>70)
and sno in (select sno from score group by sno having MAX(degree)<90) select sno from score group by sno having MAX(degree)<90 and MIN(degree)>70
--14、查询所有学生的Sname、Cno和Degree列。
select sname,cno,degree from student,score where student.sno=score.sno
--15、查询所有学生的Sno、Cname和Degree列。
select student.sno,cname,degree from student
join score on student.sno=score.sno
join course on score.cno=course.cno
--16、查询所有学生的Sname、Cname和Degree列。
select sname,cname,degree from student
join score on student.sno=score.sno
join course on score.cno=course.cno
--17、查询“95033”班所选课程的平均分。
select AVG(degree) from score
where degree in (select degree from score join student on student.sno=score.sno where class='')
--18、假设使用如下命令建立了一个grade表:
--create table grade(low int,upp int,rank varchar(1))
--insert into grade values(90,100,'A')
--insert into grade values(80,89,'B')
--insert into grade values(70,79,'C')
--insert into grade values(60,69,'D')
--insert into grade values(0,59,'E')
--现查询所有同学的Sno、Cno和rank列。
select sno,cno,[rank] from score,grade where degree between low and upp
--19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。//无关子查询
--“109”号同学成绩
--select degree from score where sno='109' and cno='3-105'
select * from student join score on student.sno=score.sno
where cno='3-105' and degree > (select degree from score where sno='' and cno='3-105')
--20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
--select sno from score group by sno having COUNT(*)>1
--select MAX(degree) from score group by cno
select * from score a
where sno in (select sno from score group by sno having COUNT(*)>1)
and
(degree not in (select MAX(degree) from score b where a.cno=b.cno group by cno))--相关子查询 --21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select * from score where degree>(select degree from score where sno='' and cno='3-105')
--22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select sno,sname,sbirthday from student
where YEAR(sbirthday)= YEAR((select sbirthday from student where sno=''))
--23、查询“张旭“教师任课的学生成绩。
--select cno from course where course.tno=(select tno from teacher where tname='张旭')
select degree from score where cno=(select cno from course where course.tno=(select tno from teacher where tname='张旭'))
--24、查询选修某课程的同学人数多于5人的教师姓名。
--select cno from score group by cno having COUNT(*)>5
--select tno from course where cno=(select cno from score group by cno having COUNT(*)>5)
select tname from teacher
where tno=(select tno from course where cno=(select cno from score group by cno having COUNT(*)>5))
--25、查询95033班和95031班全体学生的记录。
select student.sno,sname,ssex,sbirthday,class,course.cno,degree,cname,teacher.tno,tname,tsex,prof,depart from student
join score on student.sno=score.sno
join course on score.cno=course.cno
join teacher on course.tno=teacher.tno
where class='' or class='' order by student.sno
--26、查询存在有85分以上成绩的课程Cno.
select distinct cno from score where degree>=85
--27、查询出“计算机系“教师所教课程的成绩表。
--select tno from teacher where depart='计算机系'
--select cno from course where tno in (select tno from teacher where depart='计算机系')
select * from score
where cno in (select cno from course where tno in (select tno from teacher where depart='计算机系'))
--28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。//查关子查询 exists存在
select * from teacher t1 where depart='计算机系' and not exists
(
select * from teacher t2 where depart='电子工程系' and t1.prof=t2.prof
)
--29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
select cno,sno,degree from score where cno='3-105' and degree>(select MIN(degree) from score where cno='3-245') order by degree desc
--30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.//相关子查询
--select cno,sno,degree from score where cno='3-105' and degree>(select MAX(degree) from score where cno='3-245')
--30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.//相关子查询
select cno,sno,degree from score s1 where cno='3-105' and sno in
(
select sno from score where cno='3-245'
)
and degree>
(
select degree from score s2 where cno='3-245' and sno in
(
select sno from score where cno ='3-105'
)
and s1.sno=s2.sno
)
--31、查询所有教师和同学的name、sex和birthday.
select sname as name,ssex as sex,sbirthday as birthday from student
union
select tname,tsex,tbirthday from teacher
--32、查询所有“女”教师和“女”同学的name、sex和birthday.
select sname as name,ssex as sex,sbirthday as birthday from student where ssex='女'
union
select tname,tsex,tbirthday from teacher where tsex='女'
--33、查询成绩比该课程平均成绩低的同学的成绩表。
select * from score where degree<(select AVG(degree) from score where cno in (select cno from score group by cno)) --34、查询所有任课教师的Tname和Depart.
select tname,depart from teacher
--35 查询所有未讲课的教师的Tname和Depart.
--select cno from course where cno not in (select cno from score)
select tname,depart from teacher
where tno in (select tno from course where cno= (select cno from course where cno not in (select cno from score))) select tname,depart from teacher
--36、查询至少有2名男生的班号。
select class from student where ssex='男' group by class having COUNT(ssex)>=2
--37、查询Student表中不姓“王”的同学记录。
select * from student where sname not like '王%'
--38、查询Student表中每个学生的姓名和年龄。
--(YEAR(GETDATE())-YEAR(sbirthday))
select sname,(YEAR(GETDATE())-YEAR(sbirthday)) from student
--39、查询Student表中最大和最小的Sbirthday日期值。
select MAX(sbirthday),MIN(sbirthday) from student
--40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
select * from student order by class desc,sbirthday
--41、查询“男”教师及其所上的课程。
--select tname from teacher where tsex='男'
--tsex='男' and course.tno in (select tno from teacher where tsex='男')
select tname,cname from teacher
join course on teacher.tno=course.tno
where tsex='男' and course.tno in (select tno from teacher where tsex='男')
--42、查询最高分同学的Sno、Cno和Degree列。
select sno,cno,degree from score where degree in (select MAX(degree) from score)
--43、查询和“李军”同性别的所有同学的Sname.
select sname from student where ssex=(select ssex from student where sname='李军')
--44、查询和“李军”同性别并同班的同学Sname.//也可以用相关子查询
select sname from student where ssex=(select ssex from student where sname='李军') and class=(select class from student where sname='李军')
--45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
select * from score where cno in (select cno from course where cname='计算机导论') and sno in (select sno from student where ssex='男')
--46、查询score表中分数最高的学生的信息。//多层嵌套
select * from student where sno in (select sno from score where degree in (select MAX(degree) from score))
--47、查询score表中的平均分在80分以上的学生信息。//无关查询
--select sno from score group by sno having AVG(degree)>=80
select * from student
where sno in (select sno from score where score.sno in(select sno from score group by sno having AVG(degree)>=80))
SQL查询语句47题的更多相关文章
- MySQL 笔记整理(1) --基础架构,一条SQL查询语句如何执行
最近在学习林晓斌(丁奇)老师的<MySQL实战45讲>,受益匪浅,做一些笔记整理一下,帮助学习.如果有小伙伴感兴趣的话推荐原版课程,很不错. 1) --基础架构,一条SQL查询语句如何执行 ...
- sql查询语句如何解析成分页查询?
我们公司主要mysql存储数据,因此也封装了比较好用mysql通用方法,然后,我们做大量接口,在处理分页查询接口,没有很好分查询方法.sql查询 语句如何解析成“分页查询”和“总统计”两条语句.可能, ...
- 15个初学者必看的基础SQL查询语句
本文由码农网 – 小峰原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 本文将分享15个初学者必看的基础SQL查询语句,都很基础,但是你不一定都会,所以好好看看吧. 1.创建表和数据插 ...
- SQL查询语句去除重复行
1.存在两条完全相同的纪录 这是最简单的一种情况,用关键字distinct就可以去掉 select distinct * from table(表名) where (条件) 2.存在部分字段相同的纪录 ...
- WordPress 常用数据库SQL查询语句大全
在使用WordPress的过程中,我们少不了要对数据库进行修改操作,比如,更换域名.修改附件目录.批量修改文章内容等等.这个时候,使用SQL查询语句可以大大简化我们的工作量. 关于如何操作SQL查询语 ...
- (转)经典SQL查询语句大全
(转)经典SQL查询语句大全 一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql s ...
- 转: 从Mysql某一表中随机读取n条数据的SQL查询语句
若要在i ≤ R ≤ j 这个范围得到一个随机整数R ,需要用到表达式 FLOOR(i + RAND() * (j – i + 1)).例如, 若要在7 到 12 的范围(包括7和12)内得到一个随机 ...
- 经典SQL查询语句大全
一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数 ...
- oracle中sql查询语句的执行顺序
查询语句的处理过程主要包含3个阶段:编译.执行.提取数据(sql查询语句的处理主要是由用户进程和服务器进程完成的,其他进程辅助配合) 一.编译parse 在进行编译时服务器进程会将sql语句的正文放入 ...
随机推荐
- Josn转DataTable(转)
使用UI框架开发的时候就常常用到DataTable转Json的情况,但是最近完成一个微信公众号开发的项目,需要把微信接口传过来的json值作为转为DataTable后绑定到服务器控件上. 在网上找了很 ...
- 字符串在 UNICODE、MBCS编码下面的区别
1:SBCS (single byte character set)单字节字符集.在这种编码格式下,所有字符都用一个字节表示.ASCII码就是单字节字符.用“0”来表示一个字节的结束.2 :Unico ...
- (原+译)win7远程连接ubuntu16.04
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5711214.html 原始网址: http://ubuntuhandbook.org/index.ph ...
- CI框架微信开发-自定义菜单
在CI框架下面实现了自定义菜单功能.写了一个model,一个类库.顺便附带access_token的实现方式 <?php class Makemenu{ public $menustr; pub ...
- 【3】python核心编程 第五章-数字
1.用大写字母 “L”表示长整数 尽管 Python 也支持用小写字母 L 标记的长整型,但是我们郑重推荐您仅使用大写的 “L”, 这样能有效避免数字1 和小写L 的混淆.Python 在显示长整数类 ...
- 用IO流拷贝歌曲
package lianxi; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOEx ...
- Database Go and JSON
在使用Go开发web项目的过程中, 数据库读写操作与JSON格式的输入输出是两块最基础的模块, Go的标准库已经帮我们做了很多, 熟悉database/sql与encoding/json这两个库能帮我 ...
- mysql的client和sever之间通信password的传输方式
本文想要说明的是,当我们用mysql -uroot -p1234567 -h127.0.0.1 -P3306 去连接mysql server时密码是通过什么样的形式传过去的呢? 首先密码这种东西明文传 ...
- Django的痛点
如果一个html里面写了多个<a href = A.html > ,启动A.html也有这个<a herf = B.xml> 这个时候Django怎么访问这些url?
- 动态加载JS过程中如何判断JS加载完成
在正常的加载过程中,js文件的加载是同步的,也就是说在js加载的过程中,浏览器会阻塞接下来的内容的解析.这时候,动态加载便显得尤为重要了,由于它是异步加载,因此,它可以在后台自动下载,并不会妨碍其它内 ...