MySQL练习题

  一、表关系

  二、操作表

1、自行创建测试数据

2、查询“生物”课程比“物理”课程成绩高的所有学生的学号;

3、查询平均成绩大于60分的同学的学号和平均成绩;

4、查询所有同学的学号、姓名、选课数、总成绩;

5、查询姓“李”的老师的个数;

6、查询没学过“叶平”老师课的同学的学号、姓名;

7、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

9、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

10、查询有课程成绩小于60分的同学的学号、姓名;

11、查询没有学全所有课的同学的学号、姓名;

12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;

13、查询至少学过学号为“001”同学所选课程中任意一门课的其他同学学号和姓名;

14、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;

15、删除学习“叶平”老师课的SC表记录;

16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;

17、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;

20、课程平均分从高到低显示(现实任课老师);

21、查询各科成绩前三名的记录:(不考虑成绩并列情况)

22、查询每门课程被选修的学生数;

23、查询出只选修了一门课程的全部学生的学号和姓名;

24、查询男生、女生的人数;

25、查询姓“张”的学生名单;

26、查询同名同姓学生名单,并统计同名人数;

27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;

28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;

29、查询课程名称为“数学”,且分数低于60的学生姓名和分数;

30、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;

31、求选了课程的学生人数

32、查询选修“杨艳”老师所授课程的学生中,成绩最高的学生姓名及其成绩;

33、查询各个课程及相应的选修人数;

34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;

select
	student_id,
	course_id,
	num
from score as s1
where student_id in
(select student_id from score as s2 where s2.sid!=s1.sid and s2.course_id!=s1.course_id and s2.num = s1.num
)

  

35、查询每门课程成绩最好的前两名;

select course.cname,A.first_num,A.second_num from
(
select
        student_id,
	course_id,
	num,
	(select num from score where score.course_id=s1.course_id group by score.num order by num desc limit 0,1)as first_num,
	(select num from score where score.course_id=s1.course_id group by score.num order by num desc limit 1,1)as second_num
-- 	(select student from score where score.num >= ())
from score as s1
)as A
left join student on A.student_id=student.sid
left join course on A.course_id=course.cid
where num<=A.first_num and num>=A.second_num group by A.course_id

  

36、检索至少选修两门课程的学生学号;

select student_id from score group by student_id having count(1)>1

  

37、查询全部学生都选修的课程的课程号和课程名;

select course_id,course.cname from score left join course on score.course_id=course.cid where score.student_id in
(
select student_id from score group by student_id having count(1)=(select count(1) from course)
)
group by course_id

  

38、查询没学过“李平”老师讲授的任一门课程的学生姓名;

select student_id,student.sname from score left join student on score.student_id=student.sid where student_id not in
(
select student_id from score where course_id in (select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname=‘李平老师‘)
)
GROUP BY student_id

  

39、查询两门以上不及格课程的同学的学号及其平均成绩;

select
score.student_id as 学号,
(select avg(num) from score where student_id in (select student_id from score where num<60 GROUP BY student_id having count(1)>1))as 平均分
from score where score.student_id in (select student_id from score where num<60 GROUP BY student_id having count(1)>1) group by score.student_id

  

40、检索“004”课程分数小于60,按分数降序排列的同学学号;

select student_id from score where course_id=4 and num<60 order by num desc

41、删除“002”同学的“001”课程的成绩;

---恢复内容结束---

时间: 2024-08-09 10:42:36

MySQL练习题的相关文章

Python/ MySQL练习题(一)

Python/ MySQL练习题(一) 2.查询"生物"课程比"物理"课程成绩高的所有学生的学号 1 SELECT 2 * 3 FROM 4 ( 5 SELECT 6 * 7 FROM 8 course 9 LEFT JOIN score ON score.course_id = course.cid 10 WHERE 11 course.cname = '生物' 12 ) AS A 13 INNER JOIN ( 14 SELECT 15 * 16 FROM 17

python/MySQL练习题(二)

python/MySQL练习题(二) 21.查询各科成绩前三名的记录:(不考虑成绩并列情况) 1 select score.sid,score.course_id,score.num,T.first_num,T.second_num from score left join 2 ( 3 select 4 sid, 5 (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1

MySQL练习题参考答案

导出现有数据库数据: mysqldump -u用户名 -p密码 数据库名称 >导出文件路径           # 结构+数据 mysqldump -u用户名 -p密码 -d 数据库名称 >导出文件路径       # 结构 导入现有数据库数据: mysqldump -uroot -p密码  数据库名称 < 文件路径 1.创建表 create table class( cid int auto_increment primary key, caption char(10) )engine

老男孩mysql练习题自我学习

登陆数据库 [email protected] ~]#mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.38 MySQL Community Server (GPL) by Remi Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights res

MySql练习题:AB复制《主----从(主)------从》

实验环境:主MySQL服务器 源码安装 IP地址:172.16.1.1从主MySQL服务器 rpm安装IP地址:172.16.1.10从MySQL服务器 rpm安装 IP地址:172.16.1.11 下面开始配置:主MySQL服务器:vi /etc/my.cnf[mysqld]log-bin=a_masterserver-id=1授权给 172.16.1.10 访问权限grant replication slave on *.* to 'slave'@'172.16.1.10' identifi

mysql练习题-查询同时参加计算机和英语考试的学生的信息-遁地龙卷风

(-1)写在前面 文章参考http://blog.sina.com.cn/willcaty. 针对其中的一道练习题想出两种其他的答案,希望网友给出更多回答. (0) 基础数据 student表 +-----+--------+------+-------+------------+--------------+ | id  | name   | sex  | birth | department | address      | +-----+--------+------+-------+--

MySQL练习题及答案

一.现有三张数据库表,分别为部门表.员工表.部门和员工关系表 1.部门表CREATE TABLE `t_dept` ( `id` int(8) NOT NULL AUTO_INCREMENT, `dept_name` varchar(50) DEFAULT NULL COMMENT '部门', PRIMARY KEY (`id`)) CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT='部门表 ID为主键'; 2.员工表CREATE TABLE

MySQL练习题之参考答案

1.创建表结构和数据 /* Navicat Premium Data Transfer Source Server : localhost Source Server Type : MySQL Source Server Version : 50624 Source Host : localhost Source Database : sqlexam Target Server Type : MySQL Target Server Version : 50624 File Encoding :

数据库之mysql篇(5)—— 【转载】mysql练习题

原帖地址:http://www.cnblogs.com/wupeiqi/articles/5748496.html 范例数据sql: /* Navicat Premium Data Transfer Source Server : localhost Source Server Type : MySQL Source Server Version : 50624 Source Host : localhost Source Database : sqlexam Target Server Typ

6.7 如何插外键 mysql 练习题

1命令:alter table 需加外键的表 add constraint 外键名foreign key(需加外键表的字段名) references 关联表名(关联字段名);注意:外键名不能重复 2 建表时加外键     foreign key(o_buyer_id) references s_user(u_id),          foreign key(o_seller_id) references s_user(u_id) 仓库表加练习题 表(一)worker职工表 属性名 数据类型 可