MySQL经典练习题及答案,常用SQL语句练习50题

PHPer 2023-07-20 269 0 0
select a.*,b.s_score as score01, c.s_score as score02 FROM student a JOIN score b ON a.s_id=b.s_id and b.c_id='01' LEFT JOIN score c ON a.s_id=c.s_id and c.c_id='02' or c.c_id = NULL WHERE b.s_score>c.s_score ;
https://www.cnblogs.com/Diyo/p/11424844.html #–1.学生表 #Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 CREATE TABLE `Student` ( `s_id` VARCHAR(20), s_name VARCHAR(20) NOT NULL DEFAULT '', s_brith VARCHAR(20) NOT NULL DEFAULT '', s_sex VARCHAR(10) NOT NULL DEFAULT '', PRIMARY KEY(s_id) ); #–2.课程表 #Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号 create table Course( c_id varchar(20), c_name VARCHAR(20) not null DEFAULT '', t_id VARCHAR(20) NOT NULL, PRIMARY KEY(c_id) ); /* –3.教师表 Teacher(t_id,t_name) –教师编号,教师姓名 */ CREATE TABLE Teacher( t_id VARCHAR(20), t_name VARCHAR(20) NOT NULL DEFAULT '', PRIMARY KEY(t_id) ); /* –4.成绩表 Score(s_id,c_id,s_score) –学生编号,课程编号,分数 */ Create table Score( s_id VARCHAR(20), c_id VARCHAR(20) not null default '', s_score INT(3), primary key(`s_id`,`c_id`) ); #--插入学生表测试数据 #('01' , '赵雷' , '1990-01-01' , '男') insert into Student values('01' , '赵雷' , '1990-01-01' , '男'); insert into Student values('02' , '钱电' , '1990-12-21' , '男'); insert into Student values('03' , '孙风' , '1990-05-20' , '男'); insert into Student values('04' , '李云' , '1990-08-06' , '男'); insert into Student values('05' , '周梅' , '1991-12-01' , '女'); insert into Student values('06' , '吴兰' , '1992-03-01' , '女'); insert into Student values('07' , '郑竹' , '1989-07-01' , '女'); insert into Student values('08' , '王菊' , '1990-01-20' , '女'); #--课程表测试数据 insert into Course values('01' , '语文' , '02'); insert into Course values('02' , '数学' , '01'); insert into Course values('03' , '英语' , '03'); #--教师表测试数据 insert into Teacher values('01' , '张三'); insert into Teacher values('02' , '李四'); insert into Teacher values('03' , '王五'); #--成绩表测试数据 insert into Score values('01' , '01' , 80); insert into Score values('01' , '02' , 90); insert into Score values('01' , '03' , 99); insert into Score values('02' , '01' , 70); insert into Score values('02' , '02' , 60); insert into Score values('02' , '03' , 80); insert into Score values('03' , '01' , 80); insert into Score values('03' , '02' , 80); insert into Score values('03' , '03' , 80); insert into Score values('04' , '01' , 50); insert into Score values('04' , '02' , 30); insert into Score values('04' , '03' , 20); insert into Score values('05' , '01' , 76); insert into Score values('05' , '02' , 87); insert into Score values('06' , '01' , 31); insert into Score values('06' , '03' , 34); insert into Score values('07' , '02' , 89); insert into Score values('07' , '03' , 98); 这里下面的Sql语句复制执行时,在navicat上如果报错,可能是要把换行去掉,在执行 像这样 select a.*,b.s_score as score01, c.s_score as score02 FROM student a JOIN score b ON a.s_id=b.s_id and b.c_id='01' LEFT JOIN score c ON a.s_id=c.s_id and c.c_id='02' or c.c_id = NULL WHERE b.s_score>c.s_score ; select c.*, a.s_score s01, b.s_score s02 FROM score a,score b,student c where a.c_id='01' and b.c_id='02' and a.s_id=b.s_id and a.s_id=c.s_id and a.s_score>b.s_score 上面的两种查询的结果相同,后面这个sql用到了sql语句的自连接练习题和sql语句1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数 select a.*,b.s_score as score01,c.s_score as score02 FROM   student a   JOIN score b ON a.s_id=b.s_id and b.c_id='01'   LEFT JOIN score c on a.s_id=c.s_id and c.c_id='02' or c.c_id = NULL WHERE b.s_score>c.s_score ; 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数 select a.* ,b.s_score as 01_score,c.s_score as 02_score from student a left join score b on a.s_id=b.s_id and b.c_id='01' or b.c_id=NULL join score c on a.s_id=c.s_id and c.c_id='02' where b.s_score<c.s_score -- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩 select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from student b join score a on b.s_id = a.s_id GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)>=60; -- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 -- (包括有成绩的和无成绩的) select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from student b left join score a on b.s_id = a.s_id GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)<60 union select a.s_id,a.s_name,0 as avg_score from student a where a.s_id not in ( select distinct s_id from score); -- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩 select a.s_id,a.s_name,count(b.c_id) as sum_course,sum(b.s_score) as sum_score from student a left join score b on a.s_id=b.s_id GROUP BY a.s_id,a.s_name; -- 6、查询"李"姓老师的数量 select count(t_id) from teacher where t_name like '李%'; -- 7、查询学过"张三"老师授课的同学的信息 select a.* from student a join score b on a.s_id=b.s_id where b.c_id in( select c_id from course where t_id =( select t_id from teacher where t_name = '张三')); -- 8、查询没学过"张三"老师授课的同学的信息 select * from student c where c.s_id not in( select a.s_id from student a join score b on a.s_id=b.s_id where b.c_id in( select c_id from course where t_id =( select t_id from teacher where t_name = '张三'))); -- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息 select a.* from student a,score b,score c where a.s_id = b.s_id and a.s_id = c.s_id and b.c_id='01' and c.c_id='02'; -- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息 select a.* from student a where a.s_id in (select s_id from score where c_id='01' ) and a.s_id not in(select s_id from score where c_id='02') -- 11、查询没有学全所有课程的同学的信息 select s.* from student s where s.s_id in( select s_id from score where s_id not in( select a.s_id from score a join score b on a.s_id = b.s_id and b.c_id='02' join score c on a.s_id = c.s_id and c.c_id='03' where a.c_id='01')) -- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息 select * from student where s_id in( select distinct a.s_id from score a where a.c_id in(select a.c_id from score a where a.s_id='01') ); -- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 select a.* from student a where a.s_id in( select distinct s_id from score where s_id!='01' and c_id in(select c_id from score where s_id='01') group by s_id having count(1)=(select count(1) from score where s_id='01')); -- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名 select a.s_name from student a where a.s_id not in ( select s_id from score where c_id = (select c_id from course where t_id =( select t_id from teacher where t_name = '张三')) group by s_id); -- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 select a.s_id,a.s_name,ROUND(AVG(b.s_score)) from student a left join score b on a.s_id = b.s_id where a.s_id in( select s_id from score where s_score<60 GROUP BY s_id having count(1)>=2) GROUP BY a.s_id,a.s_name -- 16、检索"01"课程分数小于60,按分数降序排列的学生信息 select a.*,b.c_id,b.s_score from student a,score b where a.s_id = b.s_id and b.c_id='01' and b.s_score<60 ORDER BY b.s_score DESC; -- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 select a.s_id,(select s_score from score where s_id=a.s_id and c_id='01') as 语文, (select s_score from score where s_id=a.s_id and c_id='02') as 数学, (select s_score from score where s_id=a.s_id and c_id='03') as 英语, round(avg(s_score),2) as 平均分 from score a GROUP BY a.s_id ORDER BY 平均分 DESC; -- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 --及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 select a.c_id,b.c_name,MAX(s_score),MIN(s_score),ROUND(AVG(s_score),2), ROUND(100*(SUM(case when a.s_score>=60 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 及格率, ROUND(100*(SUM(case when a.s_score>=70 and a.s_score<=80 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 中等率, ROUND(100*(SUM(case when a.s_score>=80 and a.s_score<=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优良率, ROUND(100*(SUM(case when a.s_score>=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优秀率 from score a left join course b on a.c_id = b.c_id GROUP BY a.c_id,b.c_name -- 19、按各科成绩进行排序,并显示排名(实现不完全) -- mysql没有rank函数 select a.s_id,a.c_id, @i:=@i +1 as i保留排名, @k:=(case when @score=a.s_score then @k else @i end) as rank不保留排名, @score:=a.s_score as score from ( select s_id,c_id,s_score from score WHERE c_id='01' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC )a,(select @k:=0,@i:=0,@score:=0)s union select a.s_id,a.c_id, @i:=@i +1 as i, @k:=(case when @score=a.s_score then @k else @i end) as rank, @score:=a.s_score as score from ( select s_id,c_id,s_score
立即注册

更新于:2023-07-20 22:48:15
    您需要登录后才可以评论。 立即注册
    相关内容

    Mysql问题整理

    Yii2数据库报错-SQLSTATE[HY093]: Invalid parameter number: no para...

    Mysql server has gone away 报错原因分析及解决办法

    mysql 警告 could not be resolved: Name or service not known

    Mysql用特殊字符设置密码遇到的问题

    Mysql的函数substring使用注意事项

    MySQL 5.7内存使用分析

    mysql 命令整理

    【mysql】主键、普通索引、唯一索引和全文索引的比较

    没有接收到要导入的数据。可能是文件名没有提交,也可能是文件大小超出 PHP 限...

    Mysql 报错'SQLSTATE[HY000] [1045] Access denied for user 'root'@'...

    php连接mysql报错The server requested authentication method unknown to ...

    Mysql登录提示ERROR 2003 (HY000): Can't connect to MySQL server o...

    Mysql报错SQLSTATE[HY000]: General error: 1366 Incorrect string ...

    全网最详尽的Centos和Windows系统 Mysql8 root忘记密码解决办法

    mysql数据库操作 报错 #1030 - Got error 176 "Read page with wrong ...

    Linux系统彻底卸载MySQL数据库

    Linux下mysql5.7忘记root密码的解决方法

    ERROR 1130 (HY000): Host '127.0.0.1' is not allowed to connect to...

    mysql创建数据类型为JSON时,显示的longtext数据类型

    mysql 修改编码utf8mb4依旧无法保存表情 Incorrect string value: '\x解决办法

    MySQL 中 不等于 会过滤掉 Null 的问题

    msyql备份数据的语句mysqldump使用

    Mysql知识学习

    MySQL 8.0 索引特性1-函数索引

    MySQL字符串函数substring:字符串截取

    Mysql的临时变量取值3例

    mysql查找字符串出现位置

    mysql 更改AUTO_INCREMENT 失败的解决办法

    MYSQL 数据库导入导出命令

    mysql如何判断不包含某个字符串

    Mysql中不要用操作符和函数名等等来命名字段

    ORM 实例教程

    Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别总结

    MySQL 中 datetime 和 timestamp 的区别与选择

    Invalid default value for ‘updated_at‘

    设置MySQL的group_concat_max_len长度为最大值

    MySQL中concat()、concat_ws()、group_concat()函数使用技巧与心得

    mysql的left join和inner join的效率对比,以及如何优化

    mysqli的基本使用

    推荐内容

    分享几个好用的bt搜索bt资源下载网址网站

    影视电影剧集动漫综艺bt资源在线播放网址网站推荐分享

    全网最新bt磁力搜索引擎bt资源bt网站网址分享

    人气美女女优百度指数排行榜(波多野结衣,苍井空,三上悠亚,深田咏美,桥本有菜,北条...

    最强人造人深田咏美,业界的社交女王

    人生起起落落的三上悠亚,成名前的清纯风,怎么搭配出来的?

    业内第一大长腿,桥本有菜的腿究竟有多长

    波多野结衣,岛国业界著作最多的超级劳模

    日本AV界NO.1,苍井空是多少宅男的疯狂?

    美女女优视频混剪 This Girl-Laza Morgan

    怎样使用V2Ray代理和SSTap玩如魔兽世界/绝地求生/LOL台服/战地3/黑色沙漠/彩...

    sstap游戏代理教程 从此玩如魔兽世界/绝地求生/LOL台服/战地3/黑色沙漠/彩虹六...

    影视电影剧集动漫综艺bt资源在线播放网址网站推荐分享