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语句的正文放入 ...
随机推荐
- sql 模糊查询带下划线的字段 _
1.SELECT * FROM dbo.tb_Test 2.SELECT * FROM dbo.tb_Test WHERE name LIKE '%c_%' 3.SELECT * FROM dbo.t ...
- commit后数据库干的工作
用户提交commit后,数据库干的工作有: 1,oracle为用户的transaction生成一个SCN号. 2,LGWR把redo buffer中的数据写入到redo log file,同时把SCN ...
- 混合高斯模型和EM算法
这篇讨论使用期望最大化算法(Expectation-Maximization)来进行密度估计(density estimation). 与k-means一样,给定的训练样本是,我们将隐含类别标签用表示 ...
- GIT 实验
服务器环境:linux + git + gitolite(gitolite是什么,说白了就是安装后建了一个仓库,管理员用户可以通过修改并上传配置文件实现GIT仓库及其权限的管理.提醒:别用那个gito ...
- 远程访问TeamTalk的Mysql数据库被拒解决方法
1.A Database Error Occurred 问题如图: 蓝狐给的解答是: 这是访问mysql出错了.解决办法参考:http://www.bkjia.com/jingyan/512248.h ...
- WebSocket 简介
在HTML5规范中,我最喜欢的Web技术就是正迅速变得流行的WebSocket API.WebSocket提供了一个受欢迎的技术,以替代我们过去几年一直在用的Ajax技术.这个新的API提供了一个方法 ...
- win7 奇怪的temp用户
在C:\Users\TEMP 有个temp用户,win+r打开的也是 C:\Users\TEMP>,而不是C:\User\Administrator. 以下文章转自: http://hi.bai ...
- 【转】 linux内核移植和驱动添加(三)
原文网址:http://blog.chinaunix.net/uid-29589379-id-4708909.html 原文地址:linux内核移植和驱动添加(三) 作者:genehang 四,LED ...
- archlinux相关资料整理
Arch linux Arch Linux Wiki Arch linux Wiki Markdown Arch Wiki python continuing ...
- 【转】morgan stanley 电面面经新鲜出炉
楼楼早上上午大概11点接到的电话,一个声音炒鸡好听的GG,说他是来自morgan stanley的,想和我约一下店面时间.我一听,真是戳不及防,掐指一算,online的IKE测试已经过去20几天了吧, ...