mysql查询语句
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mysql查询语句,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含14936字,纯文字阅读大概需要22分钟。
内容图文
```mysql
Mysql_作业
1、自行创建测试数据;
# 创建数据库:school_db
create database school_db charset utf8;
# 使用school_db数据库
use school_db;
# 班级表:class
create table class(
cid int primary key auto_increment,
caption char(10) not null unique key,
grade_id int default 1
);
insert class(caption,grade_id) values
('一年一班',1),
('二年一班',2),
('三年一班',3),
('四年一班',4),
('五年一班',5),
('六年一班',6),
;
# 学生表
create table student(
sid int primary key auto_increment,
sname char(10) not null unique key,
gender enum('男','女'),
class_id int not null
);
insert student(sname,gender,class_id) values
('乔丹','女',1),
('艾弗森','女',1),
('科比','男',2),
('奥尼尔','男',4),
('姚明','男',5),
('麦迪','男',6),
('斯科技','男',1),
('詹姆斯','男',2),
('韦德','女',3),
('费舍尔','男',1),
('保罗','男',4),
('邓肯','男',4),
('吉诺比利','女',5),
('罗斯','女',6),
('霍华德','女',5),
('梅西','男',2),
('刘翔','男',3),
('张三','男',4),
('张四','女',4)
;
# 老师表:teacher
create table teacher(
tid int primary key auto_increment,
tname char(10) not null unique key
);
insert teacher(tname) values
('Alex'),
('张三'),
('李四'),
('王五')
;
# 课程表:course
create table course(
cid int primary key auto_increment,
cname char(10) not null,
teacher_id int not null
);
insert into course(cname,teacher_id) values
('语文',1),
('数学',2),
('英语',3),
('生物',4),
('物理',1),
('化学',2),
('政治',4),
('体育',4)
;
# 成绩表:score
create table score(
sid int primary key auto_increment,
student_id int not null,
course_id int not null,
score int not null
);
insert into score(student_id,course_id,score)
values
('1','1',60),
('1','2',80),
('1','3',89),
('1','4',90),
('2','1',80),
('2','3',90),
('3','2',81),
('4','3',98),
('5','1',90),
('5','2',100),
('5','3',98),
('5','4',97),
('5','5',95),
('5','6',99),
('6','1',72),
('6','5',80),
('7','5',40),
('7','6',87),
('8','5',80),
('9','1',81),
('10','2',30),
('10','3',65),
('10','4',80),
('11','1',67),
('11','2',81),
('11','3',38),
('11','4',78),
('12','2',28),
('12','3',98),
('13','5',95),
('14','4',81),
('14','5',82),
('15','1',78),
('16','3',68),
('16','4',79),
('17','1',83)
;
# 年级表
create table class_grade(
gid int primary key auto_increment,
gname char(10) not null
);
insert into class_grade(gname) values
('一年级'),
('二年级'),
('三年级'),
('四年级'),
('五年级'),
('六年级')
;
# 班级责任表 teach2cls
create table teach2cls(
tcied int primary key auto_increment,
tid int not null,
cid int not null
);
insert into teach2cls(tid,cid) values
(2,1),
(2,2),
(3,3),
(3,4),
(4,5),
(4,6)
;
2、查询学生总人数;
select count(sid) from student;
3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;
select
sid,sname
from
student
where
sid in
(select
student_id
from
score
inner join
course
on score.course_id=course.cid
where cname='生物' and score>=60 or cname='物理' and score>=60
group by
student_id
having
count(student_id)=2)
;
4、查询每个年级的班级数,取出班级数最多的前三个年级;
select
gname,
count(cid)
from
class
right join
class_grade
on
class.grade_id=class_grade.gid
group by
gid
order by
count(cid) desc
limit
3
;
5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;
select student.sid,student.sname,avg(t1.score)
from student,score t1
where student.sid=t1.student_id
group by t1.student_id
having
t1.student_id=(select t3.student_id
from score t3
group by t3.student_id
order by avg(t3.score) desc
limit 1
)
or
t1.student_id=(select t3.student_id
from score t3
group by t3.student_id
order by avg(t3.score) asc
limit 1
)
;
6、查询每个年级的学生人数;
select gid,gname,sum(student_num) as student_num from
class_grade
left join
(select cid,caption,count(sid) as student_num,grade_id from class
join student
on class.cid=student.class_id
group by
cid
having
count(sid))
as t_1
on
grade_id=gid
group by
gid
;
7、查询每位学生的学号,姓名,选课数,平均成绩;
select
student.sid,
student.sname,
count(score.sid) as course_count,
avg(score.score) as avg_scorte
from student left join score on student.sid=score.student_id
group by
student.sid
;
8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;
# 最高分
select student.sid,student.sname,t1.cname,max(t1.score),min(t1.score)
from
student
left join
(select
*
from
score
left join
course
on
score.course_id=course.cid
) as t1
on
student.sid=t1.student_id
where
student.sid=2
;
9、查询姓“李”的老师的个数和所带班级数;
select
count(tid) as '李_count',
sum(courses_count) as courses_count
from
(select teacher.tid,
tname,
count(cid) as courses_count
from
teacher
left join
teach2cls
on
teacher.tid=teach2cls.tid
group by
teacher.tid
having
tname like '李%') as t1;
10、查询班级数小于5的年级id和年级名;
select
class_grade.gid,
class_grade.gname
from
class_grade
left join
class
on
class_grade.gid=class.grade_id
group by
class_grade.gid
having
count(gid) < 5
;
11、查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级),示例结果
如下;
select
cid,caption,grade_id,
case
when grade_id between 1 and 2 then '低'
when grade_id between 3 and 4 then '中'
when grade_id between 5 and 6 then '高'
else null end as '年级级别'
from
class;
12、查询学过“张三”老师2门课以上的同学的学号、姓名;
select student.sid,student.sname
from
student
where sid in
(select score.sid
from
score
where
course_id
in
(select
course.cid
from
teacher,course
where
course.teacher_id=teacher.tid
and
teacher.tname='张三')
group by
student_id
having
count(score.sid)>2)
;
13、查询教授课程超过2门的老师的id和姓名;
select
teacher.tid,
teacher.tname
from
teacher
where tid in
(select course.teacher_id
from
course
group by
teacher_id
having
count(cid)>2);
14、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;
select sid,sname from student where sid in
(select
score.student_id
from
score
where
course_id in (1,2)
group by
student_id
having
count(course_id)=2
);
15、查询没有带过高年级的老师id和姓名;
select tid,tname from teacher where tid in
(select tid from teach2cls where cid not between 5 and 6);
16、查询学过“张三”老师所教的所有课的同学的学号、姓名;
select sid,sname from student
where sid in
(select student_id from score
where course_id in
(select cid from course
inner join teacher on teacher.tid=course.teacher_id
where tname='张三'))
;
17、查询带过超过2个班级的老师的id和姓名;
select tid,tname from teacher
where tid in
(select tid from teach2cls
group by tid
having count(cid) >2);
18、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
select sid,sname from student
where sid in
(select t1.student_id from
(select student_id,score from score
where course_id=1
group by student_id) as t1,
(select student_id,score from score
where course_id=2
group by student_id) as t2
where t1.student_id=t2.student_id and t1.score>t2.score);
19、查询所带班级数最多的老师id和姓名;
select teacher.tid,tname from
teacher,
(select tid from teach2cls
group by tid
order by count(cid) desc
limit
1) as t1
where t1.tid=teacher.tid
;
20、查询有课程成绩小于60分的同学的学号、姓名;
select sid,sname from student
where sid in
(select student_id from score
where score<60);
21、查询没有学全所有课的同学的学号、姓名;
select student.sid,student.sname from student,course,score
where course.cid=score.course_id and student.sid=score.student_id
group by student.sid
having count(course.cid)>=3;
22、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;
select student.sid,student.sname from student
where sid in
(select student_id from score
where course_id in
(select course_id from score
where student_id=1));
23、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;
select student.sid,student.sname from student
where sid!=1 and sid in
(select student_id from score
where course_id in
(select course_id from score
where student_id=1));
24、查询和“2”号同学学习的课程完全相同的其他同学的学号和姓名;
select score.student_id from
score,
(select score.student_id,score.course_id from
score,
(select course_id from score where student_id=2) as A
where score.course_id=A.course_id and score.course_id!=2
group by score.student_id
having count(*)=(select count(*) from score where student_id=2)) as B
where score.student_id=B.student_id
group by score.student_id
having count(*)=(select count(*) from score where student_id=2)
;
25、删除学习“张三”老师课的score表记录;
delete from score where score.course_id in
(select cid from teacher,course
where teacher.tid=course.teacher_id and teacher.tname='张三');
26、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“2”课程的同学学号;②插入“2”号课
程的平均成绩;
insert score(student_id,course_id,score)
values(
(select sid from student
where sid not in
(select score_c.student_id from score as score_c
where score_c.course_id=2
group by score_c.student_id)
),
2,
(select avg(score_c.score) from score as score_c
where score_c.course_id=2)
);
27、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,
数学,英语,课程数和平均分;
select student.sid as '学生ID',
(select s_b.score from score s_b
left join course c_a
on s_b.course_id=c_a.cid
where c_a.cname='语文' and s_b.student_id=s_a.student_id
) as '语文',
(select s_b.score from score s_b
left join course c_a
on s_b.course_id=c_a.cid
where c_a.cname='数学' and s_b.student_id=s_a.student_id
) as '数学',
(select s_b.score from score s_b
left join course c_a
on s_b.course_id=c_a.cid
where c_a.cname='英语' and s_b.student_id=s_a.student_id
) as '英语',
count(s_a.course_id) as '课程数',
avg(s_a.score) as '平均分'
from student left join score as s_a
on student.sid=s_a.student_id
group by student.sid
order by avg(s_a.score) desc
;
28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
select
course_c.cid as '课程ID',
(select score
from score
where score.course_id=course_c.cid
order by score desc
limit 1) as '最高分',
(select score
from score
where score.course_id=course_c.cid
order by score asc
limit 1) as '最低分'
from course as course_c
;
29、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
select
course.cid,
avg(score_c.score) as avg_score,
((select count(*)
from score as score_c2
where score_c.course_id=score_c2.course_id and score_c2.score>=60) / count(*)
) as pass_rate
from course left join score as score_c
on course.cid=score_c.course_id
group by course.cid
order by avg_score desc,pass_rate desc
;
30、课程平均分从高到低显示(现实任课老师);
select
t1.cid,
t1.avg_score,
teacher.tname
from
teacher,
(select
course.cid,
avg(score_c.score) as avg_score,
course.teacher_id
from course left join score as score_c
on course.cid=score_c.course_id
group by course.cid) as t1
where teacher.tid = t1.teacher_id
order by avg_score desc
;
31、查询各科成绩前三名的记录(不考虑成绩并列情况) ;
select *,count(t1.student_id)
from score t1 left join score t2
on t1.course_id=t2.course_id and t1.score < t2.score
group by
t1.course_id,
t1.student_id,
t1.score
having
count(t1.student_id) <=2;
;
32、查询每门课程被选修的学生数;
select
course.cname,
(select count(t1.course_id)
from score t1
where t1.course_id=t.course_id) as "学生数"
from course
left join score t
on course.cid=t.course_id
group by course.cid
;
33、查询选修了2门以上课程的全部学生的学号和姓名;
select student_id,student.sname,count(student_id) as 'courses_num'
from score,student
where score.student_id=student.sid
group by student_id
having count(student_id)>2
;
34、查询男生、女生的人数,按倒序排列;
select gender,count(gender) as '人数'
from student
group by gender
order by 人数 desc;
35、查询姓“张”的学生名单;
select * from student where sname like '张%';
36、查询同名同姓学生名单,并统计同名人数;
select
t1.sname,
count(t1.sname)
from student t1,student t2
where t1.sid!=t2.sid and t1.sname=t2.sname
group by t1.sname
;
37、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
select course.cid,course.cname,avg(score.score) as avg_score
from course left join score
on course.cid=score.course_id
group by course.cid
order by avg_score asc,course.cid desc;
38、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
select student.sname,score.score
from score,student,course
where
score.student_id=student.sid
and course.cid=score.course_id
and course.cname='数学'
and score.score<60;
39、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;
select student.sid,student.sname
from student
where student.sid in
(
select score.student_id
from score
where score.course_id=3
and score.score>80
);
40、求选修了课程的学生人数
select count(*)
from
(select
*
from score
group by score.student_id) as t;
41、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;
select
student.sname,t5.score
from
(select *
from
(select t2.student_id,t2.score,t1.cid
from score t2
inner join
(
select cid
from
teacher
inner join
course
on teacher.tid=course.teacher_id
where teacher.tname='王五'
) as t1
on t2.course_id=t1.cid) as t3
where t3.student_id in ((select t2.student_id
from score t2
inner join
(
select cid
from
teacher
inner join
course
on teacher.tid=course.teacher_id
where teacher.tname='王五'
) as t1
on t2.course_id=t1.cid
order by score desc
limit 1),
(select t2.student_id
from score t2
inner join
(
select cid
from
teacher
inner join
course
on teacher.tid=course.teacher_id
where teacher.tname='王五'
) as t1
on t2.course_id=t1.cid
order by score asc
limit 1))) as t5,student
where t5.student_id=student.sid
;
42、查询各个课程及相应的选修人数;
select course.cname,count(course.cid) as num
from course left join score
on course.cid=score.course_id
group by course.cid
;
43、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
select
s1.student_id as stu1,
s2.student_id as stu2,
s1.course_id as cid_1,
s2.course_id as cid_2,
s2.score as score_2,
s1.sid,
s2.sid
from score s1,score s2
where
s1.course_id!=s2.course_id
and s1.score=s2.score
and s1.sid>s2.sid
;
44、查询每门课程成绩最好的前两名学生id和姓名;
select
student.sid,
student.sname
from
(select t1.student_id
from score t1 left join score t2
on t1.course_id=t2.course_id and t1.score < t2.score
group by
t1.course_id,
t1.student_id,
t1.score
having
count(t1.student_id) <=1) as A,
student
where student.sid=A.student_id
;
45、检索至少选修两门课程的学生学号;
select
score.student_id
from
score
group by
score.student_id
having count(score.student_id)>=2;
46、查询没有学生选修的课程的课程号和课程名;
select
course.cid,
course.cname
from
course
left join
score
on course.cid=score.course_id
where score.sid is null;
47、查询没带过任何班级的老师id和姓名;
select
teacher.tid,
teacher.tname
from teacher left join teach2cls
on teacher.tid=teach2cls.tid
where teach2cls.tcied is null
;
48、查询有两门以上课程超过80分的学生id及其平均成绩;
select
score.student_id,avg(score.score) as avg_score
from
score
where score.score>80
group by score.student_id
having count(score.student_id)>2
;
49、检索“3”课程分数小于60,按分数降序排列的同学学号;
select
score.student_id
from score
where score.course_id=3 and score.score<60
order by score.student_id desc;
50、删除编号为“2”的同学的“1”课程的成绩;
update score set score=null where student_id=2 and course_id=1;
51、查询同时选修了物理课和生物课的学生id和姓名;
select
student.sid,
student.sname
from student
where sid in
(select
score.student_id
from
score inner join course
on score.course_id=course.cid and (course.cname='物理' or course.cname='生物')
group by score.student_id
having count(score.student_id)=2);
内容总结
以上是互联网集市为您收集整理的mysql查询语句全部内容,希望文章能够帮你解决mysql查询语句所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。