经典SQL练习题(一)

》关于MySQL的基础语法可参考:ytraister的码云课件菜鸟教程SQL
》本文练习题来自该文章:经典SQL练习题

做题前先要下载好MySQL,并开启服务
关于如何下载安装参考之前编写的blog:MySQL的windows安装方法

cmd 管理员权限 >net start mysql # 启动mysql服务
>mysql -uroot -p # 运行/登录mysql
>show databases; # 查看所有数据库
>create database 数据库名称 # 创建某个数据库
>use 数据库名称; # 使用某个数据库

本人对练习题中的 sql导入语句 进行整合,自行复制导入库即可,下载链接:蓝奏云

如果遇到语法错误,如:group by语法报错时,如下错误:
MySQL_group by报错 [ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause]
则参考该文修改下配置:
https://www.cnblogs.com/wuaihua/p/12969684.html

》各数据表运行查询结果如下:

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| course         |
| score          |
| student        |
| teacher        |
+----------------+

mysql> select * from course;
+-------+------------+--------+
| CNO   | CNAME      	| TNO |
+-------+------------+--------+
| 3-105 | 计算机导论 	| 825 |
| 3-245 | 操作系统  		| 804 |
| 6-166 | 数据电路   	| 856 |
| 9-888 | 高等数学   	| 100 |
+-------+------------+--------+

mysql> select * from score;
+-----+-------+--------+
| SNO | CNO   | DEGREE |
+-----+-------+--------+
| 103 | 3-245 |   86.0 |
| 105 | 3-245 |   75.0 |
| 109 | 3-245 |   68.0 |
| 103 | 3-105 |   92.0 | 
| 105 | 3-105 |   88.0 |
| 109 | 3-105 |   76.0 |
| 101 | 3-105 |   64.0 |
| 107 | 3-105 |   91.0 |
| 108 | 3-105 |   78.0 |
| 101 | 6-166 |   85.0 |
| 107 | 6-106 |   79.0 |
| 108 | 6-166 |   81.0 |
+-----+-------+--------+

mysql> select * from student;
+-----+-------+------+------------+-------+
| SNO | SNAME | SSEX | SBIRTHDAY  | CLASS |
+-----+-------+------+------------+-------+
| 108 | 曾华  || 1977-09-01 | 95033 |
| 105 | 匡明  || 1975-10-02 | 95031 |
| 107 | 王丽  || 1976-01-23 | 95033 |
| 101 | 李军  || 1976-02-20 | 95033 |
| 109 | 王芳  || 1975-02-10 | 95031 |
| 103 | 陆君  || 1974-06-03 | 95031 |
| 104 | 高园  || 1977-01-03 | 95031 |
+-----+-------+------+------------+-------+

mysql> select * from teacher;
+-----+-------+------+------------+--------+----------------+
| TNO | TNAME | TSEX | tbirthday  | PROF   	| DEPART     	|
+-----+-------+------+------------+--------+----------------+
| 804 | 李诚  || 1958-12-02 | 副教授 	| 计算机系   	|
| 856 | 张旭  || 1969-03-12 | 讲师  	| 电子工程系 	|
| 825 | 王萍  || 1972-05-05 | 助教   	| 计算机系   	|
| 831 | 刘冰  || 1977-08-14 | 助教   	| 电子工程系 	|
+-----+-------+------+------------+--------+----------------+

》本文选取其中具有代表性、容易出错,且自己也不会的几道题(附有本人编写的语句和答案)

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数

自编写:
select cno,round(avg(degree),2) avg from score group by cno having cno like '3-%' and count(sno)>=5;
答案:
select avg(degree),cno from score where cno like '3%' group by cno having count(sno)>= 5;

  • 运行结果和答案一致,答案更加简便

16、查询所有学生的Sname、Cname和Degree列

自编写:
select stu.sname,c.cname,sc.degree from student stu left join score sc on stu.sno=sc.sno left join course c on sc.cno=c.cno order by c.cname desc, sc.degree desc;
答案:
select a.sname,b.cname,c.degree from student a join (course b,score c) on a.sno=c.sno and b.cno =c.cno;

  • 运行结果和答案一致,答案更加简便

19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录

自编写:
select * from score where degree>(select degree from score where cno='3-105' and sno=109) and cno='3-105' order by degree desc;
答案:
select a.* from score a join score b where a.cno='3-105' and a.degree>b.degree and b.sno='109' and b.cno='3-105';

  • 运行结果和答案一致,答案更加简便

20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录(不会!!!!!!)

答案:
select * from score a where a.sno in(select sno from score group by sno having count(cno) > 1) and a.degree < (select max(degree) from score b where b.sno = a.sno) order by cno;

22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列(不会!!!!!!)

答案:
select s1.sno,s1.sname,s1.sbirthday from student s1 where s1.sno!='108' and year(sbirthday)=( select year(sbirthday) from student s2 where s2.sno='108' );

  • 剔除了sno=108的同学(查询不包括他本人)

23、查询“张旭“教师任课的学生成绩

自编写:
select * from score where cno=(select cno from course where tno=(select tno from teacher where tname='张旭'));
答案:
select cno,sno,degree from score where cno=(select x.cno from course x,teacher y where x.tno=y.tno and y.tname='张旭');

24、查询选修某课程的同学人数多于5人的教师姓名

自编写:
select tname from teacher where tno=(select tno from course where cno=(select cno from score group by cno having count(cno)>5));
答案:
select teacher.tname from teacher,course,score where score.cno = (select score.cno from score group by score.cno having count(*) >= 5) and course.cno = score.cno and course.tno = teacher.tno group by teacher.tname;

  • 自编写的结果可行,看看答案写法;而该答案剪切自评论区的,答案和自己写的一样

29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序

自编写:
select degree from score where cno='3-105' and degree>(select degree from score where cno='3-245');
# 报错: Subquery returns more than 1 row
# 上网查,该报错表示子查询返回了多行数据,修改后如下:
select degree from score where cno='3-105' and degree>any(select degree from score where cno='3-245');
# 根据题目要求,改为:
select cno,sno,degree from score where cno='3-105' and degree>any(select degree from score where cno='3-245') order by degree desc;
答案:
跟自己的一样`

30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree

自编写:
select a.* from (select * from score where cno='3-105') a inner join (select * from score where cno='3-245') b on a.sno=b.sno and a.degree>b.degree;
在这里插入图片描述

31、查询所有教师和同学的name、sex和birthday(不会!!!!!!)

答案:
select sname as name, ssex as sex, sbirthday as birthday from student union select tname as name, tsex as sex, tbirthday as birthday from teacher;

32、查询所有“女”教师和“女”同学的name、sex和birthday(不会!!!!!!)

答案:
select s.sname name,s.ssex sex,s.sbirthday birthday from student s where s.ssex='女' union select t.tname name,t.tsex sex,t.tbirthday birthday from teacher t where t.tsex='女';

33、查询成绩比该课程平均成绩低的同学的成绩表

自编写:
select * from score where cno in (select distinct(cno) from score) and degree<(select round(avg(degree),2) avg from score where cno in (select distinct(cno) from score));
# 出现sno=107的同学刚好6-106课程成绩=79.0,且是等于平均分的,也被算进去了...
答案:
写法一:
select * from score a where degree<(select avg(degree) from score b where a.cno=b.cno);
写法二:
select a.* from score a,(select cno,round(avg(degree),2) avg from score group by cno) b where a.cno=b.cno and a.degree<b.avg;

36、查询至少有2名男生的班号

自编写:
select class from student group by class having count(ssex='男')>=2;
答案:
select class from student a where ssex='男' group by class having count(ssex)>1;

  • 我的答案更好点

38、查询Student表中每个学生的姓名和年龄(不会!!!!!!)

答案:
select sname,year(now())-year(sbirthday) as age from student;

  • 没想到用year()now() 之前也很少用过,现在知道了

39、查询Student表中最大和最小的Sbirthday日期值

自编写:
select sbirthday from student where sbirthday = (select max(sbirthday) from student) or sbirthday=(select min(sbirthday) from student);
答案:
select sname,sbirthday as birthday from student where sbirthday=(select max(sbirthday) from student) union select sname,sbirthday as birthday from student where sbirthday=(select min(sbirthday) from student);

  • 结果一样,答案的写法更加严谨(用到合并union)

41、查询“男”教师及其所上的课程

自编写:
select t.tno,t.tname,t.tsex,c.cname from teacher t inner join course c where t.tno=c.tno and t.tsex='男';
答案:
select a.tname,b.cname from teacher a join course b using(tno) where a.tsex='男';

结果一样,答案用到两表等值连接的简便写法:using() 类似a.tno=b.tno

  • 拓展:
    但如果用select * 来查全部数据时,使用a.tno=b.tno结果会有两个tno;而使用using()的结果不会出现两个tno。具体可以执行以下语句:
    select * from teacher t inner join course c where t.tno=c.tno and t.tsex='男';
    select * from teacher a join course b using(tno) where a.tsex='男';

42、查询最高分同学的Sno、Cno和Degree列

自编写:
select cno,max(degree) from score group by cno having max(degree);
# 没筛选出sno...
答案:
# 答案只给出所有成绩中的一个最高分,而不是各科成绩的最高分(题目也有歧义),最后自己整理的结果:
select a.* from score a,(select cno,max(degree) max from score group by cno) b where a.cno=b.cno and a.degree=b.max order by cno;

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SQL是高级的非过程化编程语言,是沟通数据库服务器和客户端的重要工具,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以,具有完全不同底层结构的不同数据库系统,可以使用相同的SQL语言作为数据输入与管理的SQL接口。 它以记录集合作为操作对象,所有SQL语句接受集合作为输入,返回集合作为输出,这种集合特性允许一条SQL语句的输出作为另一条SQL语句的输入,所以SQL语句可以嵌套,这使它具有极大的灵活性和强大的功能,在多数情况下,在其他语言中需要一大段程序实现的功能只需要一个SQL语句就可以达到目的,这也意味着用SQL语言可以写出非常复杂的语句。    结构化查询语言(Structured Query Language)最早是IBM的圣约瑟研究实验室为其关系数据库管理系统SYSTEM R开发的一种查询语言,它的前身是SQUARE语言。SQL语言结构简洁,功能强大,简单易学,所以自从IBM公司1981年推出以来,SQL语言得到了广泛的应用。如今无论是像Oracle、Sybase、DB2、Informix、SQL Server这些大型的数据库管理系统,还是像Visual Foxpro、PowerBuilder这些PC上常用的数据库开发系统,都支持SQL语言作为查询语言。    美国国家标准局(ANSI)与国际标准化组织(ISO)已经制定了SQL标准。ANSI是一个美国工业和商业集团组织,负责开发美国的商务和通讯标准。ANSI同时也是ISO和International Electrotechnical Commission(IEC)的成员之一。ANSI 发布与国际标准组织相应的美国标准。1992年,ISO和IEC发布了SQL国际标准,称为SQL-92。ANSI随之发布的相应标准是ANSI SQL-92。ANSI SQL-92有时被称为ANSI SQL。尽管不同的关系数据库使用的SQL版本有一些差异,但大多数都遵循 ANSI SQL 标准。SQL Server使用ANSI SQL-92的扩展集,称为T-SQL,其遵循ANSI制定的 SQL-92标准。    SQL语言包含4个部分:    数据定义语言(DDL),例如:CREATE、DROP、ALTER等语句。    数据操作语言(DML),例如:INSERT(插入)、UPDATE(修改)、DELETE(删除)语句。    数据查询语言(DQL),例如:SELECT语句。    数据控制语言(DCL),例如:GRANT、REVOKE、COMMIT、ROLLBACK等语句。    SQL语言包括三种主要程序设计语言类别的语句:数据定义语言(DDL),数据操作语言(DML)及数据控制语言(DCL)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值