文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Mysql整理二 - 常见查询语句面试题(附原表)

2023-09-04 16:32

关注

表结构,创建原表的代码在最后

-- cid课程id; tid老师id; sid学生id;select * from t_mysql_course;select * from t_mysql_score;select * from t_mysql_student;select * from t_mysql_teacher;

1. 查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数

-- 查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数-- 把课程01和课程02单独拉出来,再进行对比,为了确保能对应成功,需要s1.sid = s.sid这个条件-- 为了确保对比的是同一个人,需要有s1.sid = s2.sidselect s.*,s1.score a, s2.score bfromt_mysql_student s,(select * from t_mysql_score where cid = "01") s1,(select * from t_mysql_score where cid = "02") s2wheres1.sid = s2.sid ands1.sid = s.sid ands1.score > s2.score;

2. 查询同时存在 " 01 “课程和” 02 "课程的情况 SELECT

-- 查询同时存在 " 01 “课程和” 02 "课程的情况 select s1.sid from (select * from t_mysql_score where cid = "01") as s1 inner join(select * from t_mysql_score where cid = "02") as s2where s1.sid = s2.sid;

3. 查询存在 " 01 “课程但可能不存在” 02 "课程的情况 ( 不存在时显示为 NULL ) 

-- 查询不存在" 01 “课程但存在” 02 "课程的情况select s.* from t_mysql_score s where sid not in (select sid from t_mysql_score where cid = "01") and s.cid = "02";

4. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

-- 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩select s.sid,s.sname,s2.score from t_mysql_student s, (select sid,avg(score) as score from t_mysql_score group by sid) as s2 where s.sid = s2.sid and s2.score >= 60;

5. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null)

-- 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null)select s.sid,s.sname,s2.num_cid,s2.sum_score from t_mysql_student s,(select s1.sid,count(cid) as num_cid,sum(score) as sum_score from t_mysql_score s1 group by sid) as s2where s.sid = s2.sid;

6. 查询学过「张三」老师授课的同学的信息

-- 查询学过「张三」老师授课的同学的信息select st.sid,st.sname,st.sage,st.ssex from t_mysql_student st,t_mysql_course co, t_mysql_score sc, t_mysql_teacher tewhere te.tname like '张三' and te.tid = co.tid and co.cid = sc.cid and sc.sid = st.sid;

7. 查询没有学全所有课程的同学的信息

-- 查询没有学全所有课程的同学的信息select s.*, COUNT(sc.cid) as count_cid from t_mysql_student s join t_mysql_score sc on s.sid = sc.sid group by sc.sid having COUNT(sc.cid) < (select count(*) from t_mysql_course);

8. 查询没学过"张三"老师讲授的任一门课程的学生姓名

-- 查询没学过"张三"老师讲授的任一门课程的学生姓名select * from t_mysql_student where sid not in (select sc.sid from t_mysql_course co, t_mysql_score sc, t_mysql_teacher tewhere te.tname like '张三' and te.tid = co.tid and co.cid = sc.cid);

9. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

-- 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩select s.sid,s.sname,round(avg(sc.score),2) as score from t_mysql_student s,t_mysql_score scwhere s.sid = sc.sidand sc.score < 60 group by sid having count(sc.cid) >= 2;

10. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息

-- 检索" 01 "课程分数小于 60,按分数降序排列的学生信息select s.sid,s.sname,sc.scorefrom t_mysql_student s,t_mysql_score scwhere s.sid = sc.sidand sc.score < 60 and sc.cid = "01" order by score desc;

11. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

-- 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩select s.sname,s.sid,round(avg(sc.score),2) as avg_score,max(CASE WHEN sc.cid = "01" THEN sc.score END )语文,max(CASE WHEN sc.cid = "02" THEN sc.score END )数学,max(CASE WHEN sc.cid = "03" THEN sc.score END )英语from t_mysql_student s,t_mysql_score scwhere s.sid = sc.sid group by s.sidorder by avg(sc.score) desc;

12. -- 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
-- 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

-- 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90-- 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列selectco.cid, co.cname, max(sc.score) 最高分, min(sc.score) 最低分, round(avg(sc.score),2) 平均分,concat(round(sum(if (sc.score > 60, 1,0))/count(sc.score)*100,2),'%') 及格率from t_mysql_course co,t_mysql_score scwhere co.cid = sc.cidgroup by co.cid;
SUM(IF(sc.score > 60, 1, 0)):这部分计算了成绩大于 60 分的记录的数量。如果某条记录的成绩大于 60 分,那么 IF 函数返回 1,否则返回 0。然后,SUM 函数将这些值相加,得到及格的记录数。COUNT(sc.score):这部分计算了总记录数,无论成绩是否及格。sum(...) / count(sc.score):这部分计算及格的记录数除以总记录数,得到及格率。ROUND(..., 2):这部分使用 ROUND 函数将计算结果保留两位小数。CONCAT(..., '%'):最后,CONCAT 函数将计算结果和百分号字符 "%" 连接在一起,得到一个带百分号的及格率字符串。

原表

SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0;-- ------------------------------ Table structure for t_mysql_course-- ----------------------------DROP TABLE IF EXISTS `t_mysql_course`;CREATE TABLE `t_mysql_course`  (  `cid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '课程编号',  `cname` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '课程名称',  `tid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '教师编号',  PRIMARY KEY (`cid`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '课程信息表' ROW_FORMAT = Dynamic;-- ------------------------------ Records of t_mysql_course-- ----------------------------INSERT INTO `t_mysql_course` VALUES ('01', '语文', '02');INSERT INTO `t_mysql_course` VALUES ('02', '数学', '01');INSERT INTO `t_mysql_course` VALUES ('03', '英语', '03');-- ------------------------------ Table structure for t_mysql_score-- ----------------------------DROP TABLE IF EXISTS `t_mysql_score`;CREATE TABLE `t_mysql_score`  (  `sid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生编号,外键',  `cid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '课程编号,外键',  `score` float NULL DEFAULT 0 COMMENT '成绩',  INDEX `sid`(`sid`) USING BTREE,  INDEX `cid`(`cid`) USING BTREE,  CONSTRAINT `t_mysql_score_ibfk_1` FOREIGN KEY (`sid`) REFERENCES `t_mysql_student` (`sid`) ON DELETE RESTRICT ON UPDATE RESTRICT,  CONSTRAINT `t_mysql_score_ibfk_2` FOREIGN KEY (`cid`) REFERENCES `t_mysql_course` (`cid`) ON DELETE RESTRICT ON UPDATE RESTRICT) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '成绩信息表' ROW_FORMAT = Dynamic;-- ------------------------------ Records of t_mysql_score-- ----------------------------INSERT INTO `t_mysql_score` VALUES ('01', '01', 80);INSERT INTO `t_mysql_score` VALUES ('01', '02', 90);INSERT INTO `t_mysql_score` VALUES ('01', '03', 99);INSERT INTO `t_mysql_score` VALUES ('02', '01', 70);INSERT INTO `t_mysql_score` VALUES ('02', '02', 60);INSERT INTO `t_mysql_score` VALUES ('02', '03', 80);INSERT INTO `t_mysql_score` VALUES ('03', '01', 80);INSERT INTO `t_mysql_score` VALUES ('03', '02', 80);INSERT INTO `t_mysql_score` VALUES ('03', '03', 80);INSERT INTO `t_mysql_score` VALUES ('04', '01', 50);INSERT INTO `t_mysql_score` VALUES ('04', '02', 30);INSERT INTO `t_mysql_score` VALUES ('04', '03', 20);INSERT INTO `t_mysql_score` VALUES ('05', '01', 76);INSERT INTO `t_mysql_score` VALUES ('05', '02', 87);INSERT INTO `t_mysql_score` VALUES ('06', '01', 31);INSERT INTO `t_mysql_score` VALUES ('06', '03', 34);INSERT INTO `t_mysql_score` VALUES ('07', '02', 89);INSERT INTO `t_mysql_score` VALUES ('07', '03', 98);-- ------------------------------ Table structure for t_mysql_student-- ----------------------------DROP TABLE IF EXISTS `t_mysql_student`;CREATE TABLE `t_mysql_student`  (  `sid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生编号',  `sname` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生名称',  `sage` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生年龄',  `ssex` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生性别',  PRIMARY KEY (`sid`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '学生信息表' ROW_FORMAT = Dynamic;-- ------------------------------ Records of t_mysql_student-- ----------------------------INSERT INTO `t_mysql_student` VALUES ('01', '赵雷', '1990-01-01', '男');INSERT INTO `t_mysql_student` VALUES ('02', '钱电', '1990-12-21', '男');INSERT INTO `t_mysql_student` VALUES ('03', '孙风', '1990-12-20', '男');INSERT INTO `t_mysql_student` VALUES ('04', '李云', '1990-12-06', '男');INSERT INTO `t_mysql_student` VALUES ('05', '周梅', '1991-12-01', '女');INSERT INTO `t_mysql_student` VALUES ('06', '吴兰', '1992-01-01', '女');INSERT INTO `t_mysql_student` VALUES ('07', '郑竹', '1989-01-01', '女');INSERT INTO `t_mysql_student` VALUES ('09', '张三', '2017-12-20', '女');INSERT INTO `t_mysql_student` VALUES ('10', '李四', '2017-12-25', '女');INSERT INTO `t_mysql_student` VALUES ('11', '李四', '2012-06-06', '女');INSERT INTO `t_mysql_student` VALUES ('12', '赵六', '2013-06-13', '女');INSERT INTO `t_mysql_student` VALUES ('13', '孙七', '2014-06-01', '女');-- ------------------------------ Table structure for t_mysql_teacher-- ----------------------------DROP TABLE IF EXISTS `t_mysql_teacher`;CREATE TABLE `t_mysql_teacher`  (  `tid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '教师编号',  `tname` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '教师名称',  PRIMARY KEY (`tid`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '教师信息表' ROW_FORMAT = Dynamic;-- ------------------------------ Records of t_mysql_teacher-- ----------------------------INSERT INTO `t_mysql_teacher` VALUES ('01', '张三');INSERT INTO `t_mysql_teacher` VALUES ('02', '李四');INSERT INTO `t_mysql_teacher` VALUES ('03', '王五');SET FOREIGN_KEY_CHECKS = 1;

来源地址:https://blog.csdn.net/moso_wang/article/details/132244865

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯