待整理····


45题分页查询


学生选课数据库SQL语句练习题(45个题)

练习题网盘地址:点我

 create database xxb
go
use xxb
go
--表(一)Student (学生表)
create table Student
(
sno char(3) primary key,
sname char(8) not null,
ssex char(2) not null,
sbirthday datetime,
class char(5)
)
go
--表(四)Teacher(教师表)
create table teacher
(
tno char(3) primary key,
tname char(4) not null,
tsex char(2) not null,
tbirthday datetime not null,
prof char(6) not null,
depart varchar(10) not null
)
select * from teacher
go
--表(二)Course(课程表)
create table course
(
cno char(5)primary key,
cname varchar(10) not null,
tno char(3) references teacher(tno) --foreign key(tno) references teacher(tno)
)--references
go
--表(三)Score(成绩表)
create table score
(
sno char(3) references Student(sno) not null,
cno char(5) references course(cno) not null,
degree decimal(4,1)
)
use xxb
go
--向表中插入数据,顺序不能变
insert into Student values('','曾华','男','1977-09-01','')
insert into Student values('','匡明','男','1975-10-02','')
insert into Student values('','王丽','女','1976-01-23','')
insert into Student values('','李军','男','1976-02-20','')
insert into Student values('','王芳','女','1975-02-10','')
insert into Student values('','陆军','男','1974-06-03','') insert into teacher values('','李诚','男','1958-12-02','副教授','计算机系')
insert into teacher values('','张旭','男','1969-03-12','讲师','电子工程系')
insert into teacher values('','王萍','女','1972-05-05','助教','计算机系')
insert into teacher values('','刘冰','女','1977-08-14','助教','电子工程系') insert into course values('3-105','计算机导论','')
insert into course values('3-245','操作系统','')
insert into course values('6-166','数字电路','')
insert into course values('9-888','高等数学','') insert into score values('','3-245','')
insert into score values('','3-245','')
insert into score values('','3-245','')
insert into score values('','3-105','')
insert into score values('','3-105','')
insert into score values('','3-105','')
insert into score values('','3-105','')
insert into score values('','3-105','')
insert into score values('','3-105','')
insert into score values('','6-166','')
insert into score values('','6-166','')
insert into score values('','6-166','')

建库、建表


1、 查询Student表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,class from Student
2、 查询教师所有的单位即不重复的Depart列。 消除重复的行 用distinct
select distinct depart from teacher
3、 查询Student表的所有记录。
select * from Student
4、 查询Score表中成绩在60到80之间的所有记录。
select * from score where degree>60 and degree<80
select * from score where degree between 60 and 80
5、 查询Score表中成绩为85,86或88的记录。
select * from score where degree='85'or degree='86'or degree='88'
6、 查询Student表中“95031”班或性别为“女”的同学记录。
select * from Student where class ='95031' or ssex='女'
7、 以Class降序查询Student表的所有记录。
select * from Student order by class desc --asc是升序排列 默认可不写
8、 以Cno升序、Degree降序查询Score表的所有记录。
select * from score order by cno ,degree desc --先按照cno升序再degree降序显示
9、 查询“95031”班的学生人数。
select COUNT(*) from Student where class='95031'
10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
select top 1 sno,cno,degree from score order by degree desc
11、 查询每门课的平均成绩。(select Brand from Car group by brand )
select AVG(degree) as 每个平均成绩 from score group by cno
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select cno ,AVG(degree) ,count(*)from score where cno like '3%' group by cno having COUNT (*) >=5
13、查询分数大于70,小于90的Sno列。
select sno,degree from score where degree between 70 and 90
14、查询所有学生的Sname、Cno和Degree列。
select sname,cno,degree from student join score on score.sno=student.sno
--先将student表和score表join起来,然后查询需要的列
select sname,cno,degree from student join score on student.sno = score.sno
15、查询所有学生的Sno、Cname和Degree列。
select sno,cname,degree from score join course on course.cno=score.cno select sno,cname,degree from score
join course on score.cno=course.cno

  

16、查询所有学生的Sname、Cname和Degree列。
第一步写三个表,第二步join起来,第三步筛选需要的列
select sname,cname,degree from Student
join score on score.sno=Student.sno
join course on course.cno=score.cno 17、 查询“95033”班学生的平均分。
select avg(degree) from score
join Student on Student.sno=score.sno
where class='95033'
--或者:
select * from score
select * from Student
select sno from Student where class='95033'
select AVG(degree) from score where sno in ('101','107','108')
select AVG(degree) from score where sno in (select sno from Student where class='95033'
) 18、 假设使用如下命令建立了一个grade表:
create table grade(low int,upp int,rank char(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 * from grade select sno,cno,[rank] from score
join grade on degree between low and upp select sno,cno,[rank] from score
--select * from grade
join grade on score.degree >=low and score.degree<=upp 19、 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。--无关子查询
select distinct * from Student join score on score.sno=Student.sno
select * from Student where sno in(
select sno from score where cno ='3-105'and degree>(select degree from score where sno='109'and cno='3-105')) 先用子查询查询出3-105课程和是109同学的成绩 在拿这个成绩加上3-105条件判断那个同学符合条件 并且筛选出他的学号 在学生表中查找符号条件的学号 输出学生所有信息 20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。 外层条件拿到里层进行比较,符合条件的查询返回
找出选修多门课程的同学的sno;里层查询找出每门课程的最高分,前天条件是里层的cno和外层的cno一样 select sno from score group by sno having COUNT(*)>1
select MAX(degree) from score where 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
) select sno from score where sno!=103 group by sno having COUNT(*)>1
select degree from score where sno in ('101','103','105','107','108','109')
select max(degree) from score
select sno from score where degree=92 select degree from score where sno in (select sno from score where sno!=(select sno from score where degree=(select max(degree) from score) )
group by sno having COUNT(*)>1
) 21、 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select * from score where degree>(select degree from score where sno='109'and cno='3-105')
22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select sno,sname,sbirthday from Student where year(sbirthday)=(select year(sbirthday)from Student where sno='108')
23、查询“张旭“教师任课的学生成绩。
select degree from score where cno=(select cno from course where tno=(select tno from teacher where tname = '张旭')) select * from score
join course on score.cno =course.cno
join teacher on course.tno=teacher.tno
where teacher.tname='张旭' 24、查询选修某课程的同学人数多于5人的教师姓名。
select tname from teacher
join course on teacher.tno=course.tno
where course.cno in(select cno from score group by cno having COUNT(*)>5 )
--或者
select tname from teacher where tno in(select tno from course where cno in(select cno from score group by cno having COUNT (*)>=5)
)
--步骤
select * from teacher
select * from score
select tno from course where cno in(select cno from score group by cno having COUNT (*)>=5)
select * from teacher where tno in(select tno from course where cno in(select cno from score group by cno having COUNT (*)>=5)
) 25、查询95033班和95031班全体学生的记录。
select * from Student where class='95033'or class='95031'
26、 查询存在有85分以上成绩的课程Cno.
select distinct cno from score where degree>85
27、查询出“计算机系“教师所教课程的成绩表。
select * from score
join course on score.cno=course.cno
join teacher on course.tno =teacher.tno
where depart='计算机系'
多余的 where course.tno in(select tno from teacher where depart='计算机系')
28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 不用相关子查询
SELECT * FROM teacher where depart='计算机系' and prof NOT IN(SELECT prof from teacher where depart='电子工程系')
UNION
SELECT * FROM teacher where depart='电子工程系' and prof NOT IN(SELECT prof from teacher where depart='计算机系') --相关子查询
SELECT * FROM teacher t1 where depart='计算机系' AND NOT EXISTS(
SELECT * FROM teacher t2 WHERE t2.depart='电子工程系' AND t1.prof = t2.prof
)
UNION
SELECT * FROM teacher t1 where depart='电子工程系' AND NOT EXISTS(
SELECT * FROM teacher t2 WHERE t2.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 max(degree) from score where cno='3-245') order by degree desc
--步骤:
select * from score
select MAX(degree) from score where cno='3-245'
select * from score where cno='3-105' and degree>(select MAX(degree) from score where cno='3-245') order by degree desc
30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. select * from score
select sno from score where cno in('3-105','3-245') group by sno having COUNT(*)>1
select * from score where sno in(select sno from score where cno in('3-105','3-245') group by sno having COUNT(*)>1
) select degree as a from score where sno in('103','105','109')and cno in ('3-105')
select degree as b from score where sno in('103','105','109')and cno in ('3-245') select * from score a where degree in(select degree as a from score where sno in('103','105','109')and cno in ('3-105')) AND EXISTS(select degree as b from score where sno in('103','105','109')and cno in ('3-245')
) 答案
select *from score s1 where sno in
(
select sno from score where cno in('3-105','3-245')group by sno having COUNT(*)>1
)
and cno='3-105' and degree>
(
select degree from score s2 where sno in
(
select sno from score where cno in('3-105','3-245')group by sno having COUNT(*)>1
)and cno='3-245' and s2.sno=s1.sno
) 31、 查询所有教师和同学的name、sex和birthday.
select sname,ssex,sbirthday from student
union
select tname,tsex,tbirthday from teacher
32、查询所有“女”教师和“女”同学的name、sex和birthday.
select sname,ssex,sbirthday from student where ssex='女'
union
select tname,tsex,tbirthday from teacher where tsex='女'
33、 查询成绩比该课程平均成绩低的同学的成绩表。 select AVG(degree) from score group by cno --对课程分类 取每类课程的平均成绩
select * from score as a where exists (select cno,AVG(degree) from score b group by cno having (a.cno=b.cno and a.degree<AVG(degree))) select *from score s1 where degree<
(
select AVG(degree) from score s2 where s1.cno=s2.cno group by cno
) select * from score a1 where degree<(
select AVG(degree) from score a2 where a1.cno=a2.cno group by cno ) select * from score a where exists ( select cno,AVG(degree) from score b group by cno having (a.sno=b.sno) and (a.degree<avg(degree))) 34、 查询所有任课教师的Tname和Depart.
select tname,depart from teacher select * from teacher a where exists (select * from course b where a.tno=b.tno )
或者
select * from teacher where tno in(select distinct tno from course
)
或者
--------select * from teacher a left join course b on a.tno=b.tno where course.tno is null 35 、 查询所有未讲课的教师的Tname和Depart. 答案之一:1、先把score表中cno分组出来(实际学生上了那些课);2、拿course表中的cno给步骤1的结果做比较 not in (course表中cno是老师要讲的课,跟学生实际上了那些课做比较,得出哪门课程是学生没有上的)3、已经获得未讲的课的编号,把teacher表和course表join起来 从中找到这门课是哪个老师的课程
select tname,depart from teacher
join course on teacher.tno=course.tno
where course.cno=(select cno from course where cno not in(select cno from score group by cno)) 答案2:
select tname,depart from teacher
left join course on teacher.tno=course.tno
left join score on course.cno=score.cno
where score.sno is null 36、查询至少有2名男生的班号。
select class from Student where ssex='男' group by class having COUNT(*)>1--行数>1 说明
select class from student where ssex='男' group by class having COUNT(*)>1
37、查询Student表中不姓“王”的同学记录。
select * from Student where sname like '王%'
select * from student where sname not like '王%'
38、查询Student表中每个学生的姓名和年龄。
select sname, year(getdate())-year(sbirthday) as 年龄 from Student --getdate()是获得当前时间的函数 39、查询Student表中最大和最小的Sbirthday日期值。 select MAX(Sbirthday),MIN(Sbirthday) from Student 40、以班号和年龄从大到小的顺序查询Student表中的全部记录。 select *,year(getdate())-year(sbirthday) as 年龄 from Student order by class desc, sbirthday
41、查询“男”教师及其所上的课程。
select tname,tsex,course.cname from teacher
join course on teacher.tno=course.tno where tsex='男'
42、查询最高分同学的Sno、Cno和Degree列。
select sno,cno,degree from score where degree=(select MAX(degree)from score)
43、查询和“李军”同性别的所有同学的Sname.
select sname,ssex from Student where ssex=(select ssex from Student where sname='李军')
44、查询和“李军”同性别并同班的同学Sname.
select sname,ssex,class from Student where ssex=(select ssex from Student where sname='李军') and class=(select class from Student where sname='李军')
45、查询所有选修“计算机导论”课程的“男”同学的成绩表
select student.sno,course.cno,degree,sname,ssex,class,cname,tno from score
join Student on score.sno=Student.sno
join course on score.cno=course.cno
where cname='计算机导论' and ssex ='男'

  


分页查询

SQL数据库—<10>--查询练习题的更多相关文章

  1. SQL 数据库 子查询、主外键

    子查询,又叫做嵌套查询. 将一个查询语句做为一个结果集供其他SQL语句使用,就像使用普通的表一样,被当作结果集的查询语句被称为子查询. 子查询有两种类型: 一种是只返回一个单值的子查询,这时它可以用在 ...

  2. SQL 数据库 子查询及示例

    子查询,又叫做嵌套查询. 将一个查询语句做为一个结果集供其他SQL语句使用,就像使用普通的表一样,被当作结果集的查询语句被称为子查询. 子查询有两种类型: 一种是只返回一个单值的子查询,这时它可以用在 ...

  3. SQL 数据库 连接查询 变量、if else、while

    一.连接查询:通过连接运算符可以实现多个表查询. 连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 常用的两个链接运算符: 1.join   on(左右连接) 2.uni ...

  4. SQL数据库各种查询建表插入集合-待续持续更新

    创建表 drop table student; DROP table Course; DROP table sc; CREATE TABLE student ( sid integer PRIMARY ...

  5. SQL数据库索引查询

    SELECT IA_ID,IA_Title,IT_ParentID,IA_Content,IA_CreateDate,IA_Author, )))) AS States FROM dbo.InfoAr ...

  6. SQL数据库的查询方法

    简单查询: 一.投影 select * from 表名 select 列1,列2... from 表名 select distinct 列名 from 表名 二.筛选 select top 数字 列| ...

  7. sql数据库中查询第几条到第几条的数据

    通用方法: select top 500 * from (select top 1000 * from UserSearchDatas order by ID) a order by ID desc ...

  8. SQL数据库中查询中加N'' 前缀是什么意思

    It's declaring the string as nvarchar data type, rather than varchar You may have seen Transact-SQL ...

  9. sql数据库 大小查询

    select * from sys.master_files where name='CODA_PRD_Catalog' 12416*8/1024=(m)

随机推荐

  1. JS window对象 返回前一个浏览的页面 back()方法,加载 history 列表中的前一个 URL。 语法: window.history.back();

    返回前一个浏览的页面 back()方法,加载 history 列表中的前一个 URL. 语法: window.history.back(); 比如,返回前一个浏览的页面,代码如下: window.hi ...

  2. 一、表单和ajax中的post请求&&后台获取数据方法

    一.表单和ajax中的post请求&&后台获取数据方法 最近要做后台数据接收,因为前台传来的数据太过于混乱,所以总结了一下前台数据post请求方法,顺便写了下相对应的后台接收方法. 前 ...

  3. Java并发(具体实例)—— 构建高效且可伸缩的结果缓存

    这个例子来自<Java并发编程实战>第五章.本文将开发一个高效且可伸缩的缓存,文章首先从最简单的HashMap开始构建,然后分析它的并发缺陷,并一步一步修复. hashMap版本     ...

  4. 简单说下cookie,LocalStorage与SessionStorage.md

    最近在网上看到有人讨论这三个的一些概念与区别,发现自己也一直没有较好的总结,所以查阅了一些资料来阐述一下. 基本概念 cookie cookie英文意思是小甜饼,是原来的网景公司创造,目前是在客户端存 ...

  5. leetcode 001

    1 Two Sum Difficulty: Easy The Link: https://leetcode.com/problems/two-sum/description/ Description ...

  6. Sass-unitless()函数

    unitless() 函数相对来说简单明了些,只是用来判断一个值是否带有单位,如果不带单位返回的值为 true,带单位返回的值为 false: >> unitless(100) true ...

  7. mongodb 集合操作 (增删改查)

    1.插入: 使用insert或save方法想目标集合插入一个文档: db.person.insert({"name":"ryan","age" ...

  8. Python3.5-20190521-廖老师-自我笔记-单元测试

    执行结果

  9. torchvision.transforms模块介绍

    torchvision.transforms模块 官网地址:https://pytorch.org/docs/stable/torchvision/transforms.html# torchvisi ...

  10. Ubuntu12.04安装配置vncserver

    安装 sudo apt-get install vnc4server 修改配置文件 sudo vim ~/.vnc/xstartup #!/bin/sh # Uncomment the followi ...