码迷,mamicode.com
首页 > 数据库 > 详细

50.数据库数据查询例题

时间:2018-06-19 17:48:29      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:code   show   统计   课程   var   sum   closed   view   let   

截图

技术分享图片技术分享图片技术分享图片

技术分享图片技术分享图片             

       表名称以此为course,teacher,class,score,student

代码

技术分享图片
calss 表
create table class(
        cid int primary key auto_increment,
        caption varchar(20) not null
        );

insert into class values(1, 三年二班), (2, 三年三班), (3, 一年二班), (4, 二年九班);


teacher表
create table teacher(tid int primary key auto_increment,
            tname varchar(20) not null);

insert into teacher values(1, 张磊老师), (2, 李平老师), (3, 刘海燕老师), (4, 朱云海老师), (5, 李杰老师);


course表
create table course(
        cid int primary key auto_increment,
        cname varchar(20),
        teacher_id int not null,
        foreign key(teacher_id) references teacher(tid)
        on delete cascade
        on update cascade
        );

insert into course values(1, 生物, 1), (2, 物理, 2), (3, 体育, 3), (4, 美术, 2);


student表
create table student(
        sid int primary key auto_increment,
        gender varchar(20) not null,
        class_id int not null,
        foreign key(class_id) references class(cid)
        on delete cascade
        on update cascade,
        sname varchar(20)
        );

insert into student values
        (1, , 1, 理解), (2, , 1, 钢蛋), (3, , 1, 张三), (4, , 1, 张一), (5, , 1, 张二), (6, , 1, 张四), (7, , 2, 铁锤), (8, , 2, 李三), (9, , 2, 李一), (10, , 2, 李二), (11, , 2, 李四), (12, , 3, 如花), (13, , 3, 刘三), (14, , 3, 刘一), (15, , 3, 刘二), (16, , 3, 刘四);


score表
create table score(
        sid int primary key auto_increment,
        student_id int not null,
        foreign key(student_id) references student(sid)    
        on delete cascade
        on update cascade
        course_id int not null,
        foreign key(course_id) references course(sid)    
        on delete cascade
        on update cascade,
        num int not null
        );

insert into score values
        (1, 1, 1, 10), (2, 1, 2, 9), (5, 1, 4, 66), (6, 2, 1, 8), (8, 2, 3, 68), (9, 2, 4, 99), (10, 3, 1, 77), (11, 3, 2, 66), (12, 3, 3, 87), (13, 3, 4, 99), (14, 4, 1, 79), (15, 4, 2, 11), (16, 4, 3, 67), (17, 4, 4, 100), (18, 5, 1, 79), (19, 5, 2, 11), (20, 5, 3, 67), (21, 5, 4, 100), (22, 6, 1, 9), (23, 6, 2, 100), (24, 6, 3, 67), (25, 6, 4, 100), (26, 7, 1, 9), (27, 7, 2, 100), (28, 7, 3, 67), (29, 7, 4, 88), (30, 8, 1, 9), (31, 8, 2, 100), (32, 8, 3, 67), (33, 8, 4, 88), (34, 9, 1, 91), (35, 9, 2, 88), (36, 9, 3, 67), (37, 9, 4, 22), (38, 10, 1, 90), (39, 10, 2, 77), (40, 10, 3, 43), (41, 10, 4, 87), (42, 11, 1, 90), (43, 11, 2, 77), (44, 11, 3, 43), (45, 11, 4, 87), (46, 12, 1, 90), (47, 12, 2, 77), (48, 12, 3, 43), (49, 12, 4, 87), (52, 13, 3, 87);
View Code

作业

2、查询“生物”课程比“体育”课程成绩高的所有学生的学号。
# 所有生物课程,体育课程的人的学号和成绩分别筛选出来作为临时表,根据学号连接两个临时表
select A.student_id,bio,phi from

(select student_id,num as bio from score left join course on score.course_id = course.cid where course.cname = ‘生物‘) as A 

left join
 
(select student_id,num as phi from score left join course on score.course_id = course.cid where course.cname = ‘体育‘) as B

on A.student_id=B.student_id where bio>phi;


3、查询平均成绩大于60分的同学的学号和平均成绩; 
select student_id,avg(num) from score group by student_id having avg(num)>60;

4、查询所有同学的学号、姓名、选课数、总成绩;
select score.student_id,student.sname,count(score.course_id),sum(score.num) from 
score join student on score.student_id=student.sid group by score.student_id;

5、查询姓“李”的老师的个数;
select tname from teacher where tname like ‘李%‘;


6、查询没学过“李平”老师课的同学的学号、姓名;
# 先查取‘李平老师’教的所有课ID,再获取选过课的学生ID
select * from student where sid not in(
select distinct student_id from score where course_id in
(select cid from course left join teacher on course.teacher_id=teacher.tid where tname=‘李平老师‘));


7、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
select student_id,sname from
(select student_id,course_id from score where course_id = 1 or course_id = 2) as B     
left join student on B.student_id = student.sid group by student_id having count(student_id) > 1;

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

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

10、查询有课程成绩小于60分的同学的学号、姓名;
select sid,sname from student where sid in (select distinct student_id from score where num<60);

11、查询没有学全所有课的同学的学号、姓名;
select student_id,sname
        from score left join student on score.student_id = student.sid
        group by student_id HAVING count(course_id) = (select count(1) from course);
 

12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
select student_id,sname,count(course_id)
from score left join student on score.student_id= student.sid
where student_id !=1 and course_id in (select course_id from score where student_id=1) group by student_id;


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

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


15、删除学习“叶平”老师课的score表记录;
delete from score where course_id in(
		select course.cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname = ‘李平老师‘);

16、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩; 
insert into score(student_id,course_id,num) select sid,2,(select avg(num) from score where course_id=2)
from student where sid not in
(select student_id from score where course_id=2);

17、按平均成绩从低到高显示所有学生的“生物”、“物理”、体育”三门的课程成绩,按如下形式显示: 学生ID,生物,物理,体育,有效课程数,有效平均分;
select sc.student_id,
        (select num from score left join course on score.course_id = course.cid where course.cname = "生物" and score.student_id=sc.student_id) as sy,
        (select num from score left join course on score.course_id = course.cid where course.cname = "物理" and score.student_id=sc.student_id) as wl,
        (select num from score left join course on score.course_id = course.cid where course.cname = "体育" and score.student_id=sc.student_id) as ty,
        count(sc.course_id),
        avg(sc.num)
    from score as sc
    group by student_id desc;    

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
select course_id,max(num),min(num) from score group by course_id;

19、按各科平均成绩从低到高顺序;
# 按照科目分组
select course_id,avg(num) from score group by course_id order by avg(num) asc;	

20、课程平均分从高到低显示(显示任课老师);
select avg(score.num),teacher.tname from course
left join score on course.cid=score.course_id
left join teacher on course.teacher_id=teacher.tid
group by score.course_id;

21、查询各科成绩前三名的记录:(不考虑成绩并列情况) 
 select score.sid,score.course_id,score.num,T.first_num,T.second_num from score left join
    (
    select
        sid,
        (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1) as first_num,
        (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 3,1) as second_num
    from
        score as s1
    ) as T
    on score.sid =T.sid
    where score.num <= T.first_num and score.num >= T.second_num;


22、查询每门课程被选修的学生数;
select course_id,count(student_id) from score group by course_id;

23、查询出只选修了一门课程的全部学生的学号和姓名;
select score.student_id,student.sname from score left join student on student.sid=score.student_id group by course_id having count(course_id)=1;

24、查询男生、女生的人数;
select gender,count(sid) from student group by gender; 


25、查询姓“张”的学生名单;
select * from student where sname like ‘张%‘;


26、查询同名同姓学生名单,并统计同名人数;
select sname,count(1) from student group by sname;

27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
# 按课程分组
select course.cname,avg(num) as avg from score left join course on course.cid=score.sid group by course_id order by avg asc,course_id desc;


28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;
select score.student_id,student.sname,avg(num) from score left join student on student.class_id=score.course_id group by student_id having avg(num)>85;

29、查询课程名称为“生物”,且分数低于60的学生姓名和分数;(居然还能连续使用join)
select student.sname,score.num from score 
left join course on course.cid=score.course_id 
left join student on score.student_id=student.sid
where score.num<60 and course.cname=‘生物‘;


30、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
select score.student_id,student.sname,score.num from score left join student on student.sid=score.student_id where score.course_id=3 and score.num>80; 

31、求选了课程的学生人数(记得去重)
select count(distinct student_id) from score; 

32、查询选修“张磊”老师所授课程的学生中,成绩最高的学生姓名及其成绩;
select student.sname,score.num from student left join score on student.sid=score.student_id where score.course_id in(select course.cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname=‘张磊老师‘)order by num desc limit 1; 

33、查询各个课程及相应的选修人数;
select course.cname,count(1) from score left join course on score.course_id=course.cid group by course_id;


34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
select distinct s1.student_id,s2.course_id,s1.num from score as s1,score as s2 where s1.num = s2.num and s1.course_id != s2.course_id;

35、查询每门课程成绩最好的前两名;
# 以课程分组
# 升序排列order by num desc     
# 筛选limit 0,1

  select score.sid,score.course_id,score.num,T.first_num,T.second_num from score left join
    (
    select
        sid,
        (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1) as first_num,
        (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 1,1) as second_num
    from
        score as s1
    ) as T
    on score.sid =T.sid
    where score.num <= T.first_num and score.num >= T.second_num;

36、检索至少选修两门课程的学生学号;
select student_id from score group by student_id having count(course_id)>1; 

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

select course_id,count(1) from score group by course_id having(1)=(select count(1) from student);


38、查询没学过“叶平”老师讲授的任一门课程的学生姓名;
select student_id,student.sname from score 
left join student on score.student_id = student.sid
where score.course_id not in(select cid from course left join teacher on course.teacher_id = teacher.tid where tname=‘张磊老师‘) group by student_id;

39、查询两门以上不及格课程的同学的学号及其平均成绩;
select student_id,avg(num) from score group by student_id;

select student_id,count(1) from score where num<60 group by student_id having count(1)>=2;


40、检索“004”课程分数小于60,按分数降序排列的同学学号;
select student_id from score where student_id=4 and num<60 order by num desc;

41、删除“002”同学的“001”课程的成绩;
delete from socre where student_id=2 and course_id=1;

  

 

50.数据库数据查询例题

标签:code   show   统计   课程   var   sum   closed   view   let   

原文地址:https://www.cnblogs.com/LearningOnline/p/9191657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!