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. 移动前端开发之 viewport 的深入理解

    移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上的viewport了,只有明白了viewport的概念以及弄清楚了跟viewport有关的meta标签的使用,才能更好地让我们的网页适配或响 ...

  2. django中的Model模型一:

    在django的框架设计中采用了mtv模型,即Model,template,viewer Model相对于传统的三层或者mvc框架来说就相当对数据处理层,它主要负责与数据的交互,在使用django框架 ...

  3. keycode(来自互联网)

  4. C#中Thread.Join()的理解

    最近在项目中使用多线程,但是对多线程的一些用法和概念还有有些模棱两可,为了搞清楚查阅了一写资料,写下这篇日志加深理解吧. Thread.Join()在MSDN中的解释很模糊:Blocks the ca ...

  5. netCDF

    NetCDF started in 1989 most used in geoscience community array-oriented self-describing header, desc ...

  6. hadoop官网介绍及如何下载hadoop(2.4)各个版本与查看hadoop API介绍

    1.如何访问hadoop官网?2.如何下载hadoop各个版本?3.如何查看hadoop API? 很多同学开发都没有二手资料,原因很简单觉得不会英语,但是其实作为软件行业,多多少少大家会英语的,但是 ...

  7. CSS3动画之旋转魔方盒

    步骤: 1.大盒子里盛放六个子盒子代表立方体的6个面: 2.子盒子开启3d效果  transform-style:preserve-3d; 3.上下面沿X轴旋转90度,一个上移盒子一半高,一个下移盒子 ...

  8. requirejs学习之-- 初始化(一)

    为了规范在项目中使用的javascript代码,我们使用了requirejs框架. 初始阶段,我们在按钮的点击事件中调用创建的模块,代码如下: function button_click() { _t ...

  9. LFS,编译自己的Linux系统 - 包和补丁

    建立工作目录 我们先建立一个工作目录,用于存放下载的源代码和对源代码进行编译. sudo mkdir –v /mnt/lfs/sources sudo chmod –v a+wt /mnt/lfs/s ...

  10. gcc编译通过,运行却显示“段错误”的解决方法

    ​第一次在Liunx上(liunx mint 17)使用gcc编译c文件,竟然提示“找不到stdio.h",经过google后发现执行 sudo apt-get install build- ...