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. js控制html5 audio的暂停、播放、停止

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <meta name ...

  2. sql 参数

    sqlserver :传参数是“@” oracle:是“:” mysql:是“?”

  3. UIScrollView 代理方法

    在使用UIScrollView和它的子类UITableView时,有时需要在不同操作状态下,做不同的响应. 如何截获这些状态,如正在滚动,滚动停止等,使用UIScrollViewDelegate_Pr ...

  4. c#程序为PDF文件填写表单内容

    众所周知,PDF文件一般情况下是无法修改的,如果你有一张现成的PDF表格,这时想通过编程实现从数据库或者动态生成内容去填写这张表格,就会有些问题了,首先我们要解决以下2个重要的问题: 1.如何将内容写 ...

  5. C# ?? 运算符,不能忘记的知识点

    最近项目中有一个bug被测试(是黑盒测试)发现了,跟了老半天代码,才找到这个问题的所在,原来是一个计算表达式中用到了??运算符,才导致了这个错误,下面让我简单讲述一下. C# ?? 运算符 msdn上 ...

  6. dede调用指定栏目的标签

    {dede:type typeid='1'} <a href="[field:typelink /]">[field:typename /]</a> {/d ...

  7. PHP 中 AJAX 中文乱码解决

    最近,在用PHP做一个AJAX功能时,出现了中文乱码.在网上搜索了一上,很多是有说不过试过都不行,我页面的的编码是UTF-8的,MYSQL中也是UTF-8的.以前在.NET中也遇到这样的问题,但只是在 ...

  8. 在Python3.5下安装和测试Scrapy爬网站

    1. 引言 Scrapy框架结构清晰,基于twisted的异步架构可以充分利用计算机资源,是爬虫做大的必备基础.本文将讲解如何快速安装此框架并使用起来. 2. 安装Twisted 2.1 同安装Lxm ...

  9. 基于方法的LINQ语句

    LINQ中的查询方法有两站,一种是使用类似于SQL语句的方式,另一种则是基于方法的语句.基于方法的查询方法使用的是C#中面向对象概念的,主要的方法有: 投影:  Select | SelectMany ...

  10. 折腾gnome3.4

    1.平埔式窗口管理器shellshape 刚开始用都是登录时默认为gnome classic,主要是希望有任务栏,但是为了在这种模式,gnome扩展都没有用了. 而我又在使用shellshape -- ...