Sql基础练习题(题+答案)

这篇博客记录了多个SQL查询实例,包括从lesson和user_info表中筛选不同条件的数据,如学分区间、课程类型、特定学生信息等,还涉及到了表间查询、成绩统计和计算等进阶操作。

讲课刷的题,做个笔记 


1、    查询lesson表中所有考试课课程,显示列名为[课程名称]、[课程类型]
2、    查询lesson表中所有学分大于等于3的课程,显示列名为[课程名称]、[课程学分]
3、    查询user_info表,班级ID不为1004的所有学生数据,显示account、student_name、class_id
4、    查询lesson表中所有学分在[2,3]区间的课程,显示lesson_name , score
5、    查询lesson表中所有学分为1、3的课程,显示lesson_name , score
6、    查询user_info表,班级ID为1002或1003,且性别为女的所有学生数据,显示student_name、class_id,并且按班级ID排序
7、    查询user_info表,查询所有姓何的学生,显示account,student_name
8、    查询user_info表,查询所有名字中带方的学生,显示account,student_name
9、    查询user_info表,查询所有姓陈且名字只有两个字的学生,显示account,student_name

/*
1、	查询lesson表中所有考试课课程,显示列名为[课程名称]、[课程类型]
*/

select lesson_name as "课程名称" , lesson_type as "课程类型" from lesson;

/*
2、	查询lesson表中所有学分大于等于3的课程,显示列名为[课程名称]、[课程学分]
*/

select lesson_name as "课程名称" , score as "课程学分" from lesson where score >= 3;


/*
3、	查询user_info表,班级ID不为1004的所有学生数据,显示account、student_name、class_id
*/
select account , student_name , class_id from user_info  where class_id != 1004;
select account , student_name , class_id from user_info  where class_id <> 1004;

/*
4、	查询lesson表中所有学分在[2,3]区间的课程,显示lesson_name , score
*/
select lesson_name , score  from lesson where score between 2 and 3;

/*
5、	查询lesson表中所有学分为1、3的课程,显示lesson_name , score
*/
select lesson_name , score from lesson where score in (1,3);
select lesson_name , score from lesson where score = 1 or score = 3;

/*
6、	查询user_info表,班级ID为1002或1003,且性别为女的所有学生数据,显示student_name、class_id,并且按班级ID排序
*/
select student_name , class_id from user_info  where sex = '女' and (class_id =1002 or class_id =1003) order by class_id ;
select student_name , class_id from user_info  where sex = '女' and class_id in (1002,1003) order by class_id ;


/*
7、	查询user_info表,查询所有姓何的学生,显示account,student_name
*/
select account , student_name  from user_info  where student_name like "何%" ;

/*
8、	查询user_info表,查询所有名字中带方的学生,显示account,student_name
*/
select account , student_name  from user_info  where student_name like "%方%" ;

/*
9、	查询user_info表,查询所有姓陈且名字只有两个字的学生,显示account,student_name
*/
select account , student_name  from user_info  where student_name like "陈_" ;

* 显示t_class_msg 表 班级全称 为 [班级名称] 例如:2017级信息管理1班

 *  查询所有高等数学(一)的课程成绩加10分显示

 *  给id为208的同学,生日加一天显示,并显示当前时间为now_time 字段

 * 查询user_info 女生的数量

 * 查询lesson_score 计算高等数学(一)的平均分数

 * 查询lesson_score 计算学号为201703024001的各科总成绩
 

/*
 * 显示t_class_msg 表 班级全称 为 [班级名称] 例如:2017级信息管理1班
 */
select concat(grade, '级', class_name , num , '班'  ) as '班级名称' from t_class_msg;

/*
 *  查询所有高等数学(一)的课程成绩加10分显示
 */
select id, score+10 as score from lesson_score where lesson_name ='高等数学(一)';

/*
 *  给id为208的同学,生日加一天显示,并显示当前时间为now_time 字段
 */
select date_add(birthday, interval 1 day) as birthday ,now() as now_time from user_info where id = 208;

/*
 * 查询user_info 女生的数量
 */
select count(1) from user_info where sex = '女';

/*
 * 查询lesson_score 计算高等数学(一)的平均分数
 */
select avg(score) from lesson_score where lesson_name = '高等数学(一)' ;

/*
 * 查询lesson_score 计算学号为201703024001的各科总成绩
 */
select sum(score) from lesson_score where stu_num = '201703024001'; 

* 查询lesson 表中 考试课和考查课各有几门

* 查询lesson 表中 统计每种学分的课程数量,只显示大于等于3的课程信息

* lesson_score 表中 课程成绩数量小于3的学生 显示 学号 和课程数量


 * 查询user_info表中 id 为 245 的同学的成绩信息 显示学生姓名、课程名称、课程分数

 * 查询课程成绩大于80分的同学,显示student_name和lesson_name和score

 * 显示lesson表中所有课程的平均分(lesson_name、score)


/*
* 1、查询lesson 表中 考试课和考查课各有几门
*/
select stu_num '学号' , count(*) '课程数量'  from lesson_score group by stu_num having  count(*) <3

/*
* 2、查询lesson 表中 统计每种学分的课程数量,只显示大于等于3的课程信息

*/
select lesson_type, score, count(*) as 'num' from lesson where score >= 3 group by score  

/*
* 3、lesson_score 表中 课程成绩数量小于3的学生 显示 学号 和课程数量
*/
select lesson_type , count(*) as 'num' from lesson group by lesson_type 


/*
 * 1、 查询user_info表中 id 为 245 的同学的成绩信息 显示学生姓名、课程名称、课程分数
 */

select ui.student_name '学生姓名', ls.lesson_name '课程名称', ls.score '课程分数' 
from user_info ui 
inner join lesson_score ls on ui.id= ls.student_id
where ui.id =245



/*
 * 查询课程成绩大于80分的同学,显示student_name和lesson_name和score
 */
select ui.student_name , ls.lesson_name , ls.score 
from user_info ui left outer join lesson_score ls on ui.id=ls.student_id
where ls.score > 80;

/*
 * 显示lesson表中所有课程的平均分(lesson_name、score)
 */
select lesson.lesson_name ,  avg(ls.score) as score 
from lesson left outer join lesson_score ls on ls.lesson_id =lesson.id 
group by  lesson.id  
order by score desc  ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Matthew.M

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值