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题的更多相关文章

  1. MySQL 笔记整理(1) --基础架构,一条SQL查询语句如何执行

    最近在学习林晓斌(丁奇)老师的<MySQL实战45讲>,受益匪浅,做一些笔记整理一下,帮助学习.如果有小伙伴感兴趣的话推荐原版课程,很不错. 1) --基础架构,一条SQL查询语句如何执行 ...

  2. sql查询语句如何解析成分页查询?

    我们公司主要mysql存储数据,因此也封装了比较好用mysql通用方法,然后,我们做大量接口,在处理分页查询接口,没有很好分查询方法.sql查询 语句如何解析成“分页查询”和“总统计”两条语句.可能, ...

  3. 15个初学者必看的基础SQL查询语句

    本文由码农网 – 小峰原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 本文将分享15个初学者必看的基础SQL查询语句,都很基础,但是你不一定都会,所以好好看看吧. 1.创建表和数据插 ...

  4. SQL查询语句去除重复行

    1.存在两条完全相同的纪录 这是最简单的一种情况,用关键字distinct就可以去掉 select distinct * from table(表名) where (条件) 2.存在部分字段相同的纪录 ...

  5. WordPress 常用数据库SQL查询语句大全

    在使用WordPress的过程中,我们少不了要对数据库进行修改操作,比如,更换域名.修改附件目录.批量修改文章内容等等.这个时候,使用SQL查询语句可以大大简化我们的工作量. 关于如何操作SQL查询语 ...

  6. (转)经典SQL查询语句大全

    (转)经典SQL查询语句大全 一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql s ...

  7. 转: 从Mysql某一表中随机读取n条数据的SQL查询语句

    若要在i ≤ R ≤ j 这个范围得到一个随机整数R ,需要用到表达式 FLOOR(i + RAND() * (j – i + 1)).例如, 若要在7 到 12 的范围(包括7和12)内得到一个随机 ...

  8. 经典SQL查询语句大全

    一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数 ...

  9. oracle中sql查询语句的执行顺序

    查询语句的处理过程主要包含3个阶段:编译.执行.提取数据(sql查询语句的处理主要是由用户进程和服务器进程完成的,其他进程辅助配合) 一.编译parse 在进行编译时服务器进程会将sql语句的正文放入 ...

随机推荐

  1. T4运行时模板

    可以通过Visual Studio运行时文本模板在您的应用程序在运行时生成文本字符串. 执行应用程序的计算机不必具有 Visual Studio. 运行库模板有时称为"预处理文本模板&quo ...

  2. Glide的加载图片的帮助类,用来把图片圆角或者改成圆形图片

    Glide虽然非常好用但是没找到把图片圆角的方法,所以百度了一个非常不错的加载类自己实现圆角图 感谢原文章作者:http://blog.csdn.net/weidongjian/article/det ...

  3. Windows Phone 学习教程(一)

    http://www.cnblogs.com/webabcd/category/385852.html Windows Phone 7 教程 Windows Phone 8.1 Windows Pho ...

  4. 原创:C sharp 中 Enum的几点小 Tips

    (1)为什么要使用Enum? ♥ enums枚举是值类型,数据直接存储在栈中,而不是使用引用和真实数据的隔离方式来存储.enum student{a,b,c,d,e},其中enum代表student为 ...

  5. Webform服务器控件调用JS

    服务器控件调用JS一.两类JS的触发设计1.提 交之前的JS -- 加js的事件C#处理程序2.提交之后的JS -- 用C#代码向页面上写<script>..</script> ...

  6. codeforces 340E Iahub and Permutations(错排or容斥)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Iahub and Permutations Iahub is so happy ...

  7. Niagara AX连接MySQL数据库

    步骤如下 1.安装Niagara AX (3.8.38): 2.安装MySQL(5.6.25):将MySQL\Connector.J 5.1\mysql-connector-java-5.1.35-b ...

  8. MYSQL操作的一些知识点,持续更新中····

    基本概念——库 1.数据库服务器:库——>表——>行/列 2.cmd下链接: mysql – uroot –proot 3.创建库:create database  php; 3.看数据库 ...

  9. js验证身份证格式

    (function(){ Validate={ data:{ // 加权因子 Wi : [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ...

  10. Knight Moves(BFS,走’日‘字)

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...