mysql数据库多表查询的基础练习题1

一 ,学生选课系统表的准备

teacher 教师表

CREATE TABLE teacher (
  id int(11) NOT NULL primary key auto_increment,
  name varchar(20) not null unique
 );

student 学生表

CREATE TABLE student (
  id int(11) NOT NULL primary key auto_increment,
  name varchar(20) NOT NULL unique,
  city varchar(40) NOT NULL,
  age int 
) ;

course 课程表

CREATE TABLE course(
  id int(11) NOT NULL primary key auto_increment,
  name varchar(20) NOT NULL unique,
  teacher_id int(11) NOT NULL,
  FOREIGN KEY (teacher_id) REFERENCES teacher (id)
);

studentcource 学生表和课程表的中间关系表·

CREATE TABLE studentcourse (
   student_id int NOT NULL,
   course_id int NOT NULL,
   score double NOT NULL,
   FOREIGN KEY (student_id) REFERENCES student (id),
   FOREIGN KEY (course_id) REFERENCES course (id)
);

向以上四张表中插入数据

insert into teacher values(null,'关羽');
insert into teacher values(null,'张飞');
insert into teacher values(null,'赵云');

insert into student values(null,'小王','北京',20);
insert into student values(null,'小李','上海',18);
insert into student values(null,'小周','北京',22);
insert into student values(null,'小刘','北京',21);
insert into student values(null,'小张','上海',22);
insert into student values(null,'小赵','北京',17);
insert into student values(null,'小蒋','上海',23);
insert into student values(null,'小韩','北京',25);
insert into student values(null,'小魏','上海',18);
insert into student values(null,'小明','广州',20);

insert into course values(null,'语文',1);
insert into course values(null,'数学',1);
insert into course values(null,'生物',2);
insert into course values(null,'化学',2);
insert into course values(null,'物理',2);
insert into course values(null,'英语',3);

insert into studentcourse values(1,1,80);
insert into studentcourse values(1,2,90);
insert into studentcourse values(1,3,85);
insert into studentcourse values(1,4,78);
insert into studentcourse values(2,2,53);
insert into studentcourse values(2,3,77);
insert into studentcourse values(2,5,80);
insert into studentcourse values(3,1,71);
insert into studentcourse values(3,2,70);
insert into studentcourse values(3,4,80);
insert into studentcourse values(3,5,65);
insert into studentcourse values(3,6,75);
insert into studentcourse values(4,2,90);
insert into studentcourse values(4,3,80);
insert into studentcourse values(4,4,70);
insert into studentcourse values(4,6,95);
insert into studentcourse values(5,1,60);
insert into studentcourse values(5,2,70);
insert into studentcourse values(5,5,80);
insert into studentcourse values(5,6,69);
insert into studentcourse values(6,1,76);
insert into studentcourse values(6,2,88);
insert into studentcourse values(6,3,87);
insert into studentcourse values(7,4,80);
insert into studentcourse values(8,2,71);
insert into studentcourse values(8,3,58);
insert into studentcourse values(8,5,68);
insert into studentcourse values(9,2,88);
insert into studentcourse values(10,1,77);
insert into studentcourse values(10,2,76);
insert into studentcourse values(10,3,80);
insert into studentcourse values(10,4,85);
insert into studentcourse values(10,5,83);

二,以上四张表的练习题

1、查询平均成绩大于70分的同学的学号和平均成绩
select student_id ,avg(score) as avgScore
from studentcourse
group by student_id 
having avgScore > 70;
2、查询所有同学的学号、姓名、选课数、总成绩
select stu.id 学号,stu.name 姓名,tmp.courseCount 选课数,tmp.scoreSum 总成绩
from student stu,(select student_id ,count(1) as courseCount,sum(score) as scoreSum
                from studentcourse
                group by student_id) as tmp
where stu.id = tmp.student_id
3、查询学过赵云老师所教课的同学的信息
-- 3.1 根据老师姓名 找 教师工号, 在教师表
select id from teacher 
where name='赵云'
;
-- 3.2 根据教师工号 找 所教课程对应的 课程编号, 在课程表
select id from course
where teacher_id=(select id from teacher 
                  where name='赵云')
;
-- 3.3 根据课程编号 找 上课的学生编号, 在中间表
select student_id from studentcourse
where course_id in (select id from course
                    where teacher_id=(select id from teacher 
                                      where name='赵云'));

-- 3.4 根据学生编号 找 对应的学号和姓名, 在学生表
select * from student
where id in (select student_id from studentcourse
            where course_id in (select id from course
                                where teacher_id=(select id from teacher 
                                                  where name='赵云')));
4、查询没学过关羽老师课的同学的信息
-- 4.1 根据老师姓名 找 教师工号, 在教师表
select id from teacher 
where name='关羽'
;
-- 4.2 根据教师工号 找 所教课程对应的 课程编号, 在课程表
select id from course
where teacher_id=(select id from teacher 
                  where name='关羽')
;
-- 4.3 根据课程编号 找 上课的学生编号, 在中间表
select student_id from studentcourse
where course_id in (select id from course
                    where teacher_id=(select id from teacher 
                                      where name='关羽'));

-- 4.4 根据学生编号 找 对应的学号和姓名, 在学生表
select * from student
where id not in (select student_id from studentcourse
                where course_id in (select id from course
                                    where teacher_id=(select id from teacher 
                                                      where name='关羽')));
 5、查询没有学三门课以上的同学的学号、姓名
-- 5.1 查询每个学生选了几门课
select student_id, count(*)
from studentcourse
group by student_id;
-- 5.2 增加过滤条件 没有学三门课以上 等价于  count(*)<=3,查的是 学号
select student_id
from studentcourse
group by student_id
having count(*)<=3;
-- 5.3 根据学号 查询 学号和姓名, 学生表
select id,name from student
where id in (select student_id
            from studentcourse
            group by student_id
            having count(*)<=3);
6、查询各科成绩最高和最低的分
select c.id,c.name,tmp.maxScore,tmp.minScore
from course c ,(select course_id,max(score) maxScore,min(score) minScore
                from studentcourse sc
                group by sc.course_id) as tmp
where c.id = tmp.course_id
7、查询学生信息和平均成绩
select stu.*,tmp.avgScore 平均成绩
from student stu,(
                    select student_id id,avg(score) avgScore
                    from studentcourse  
                    group by student_id
) as tmp
where stu.id = tmp.id
8、查询上海和北京学生数量
-- 8.1 方法1
select city, count(*)
from student
group by city
having city in ('北京', '上海');

-- 8.2 方法2
select city, count(*)
from student
where city in ('北京', '上海')
group by city
9、查询不及格的学生信息和课程信息
select stu.*,c.*
from student stu,
    course c,
    (select *
    from studentcourse
    where score < 60) as tmp
where stu.id = tmp.student_id and c.id = tmp.course_id
10、统计每门课程的学生选修人数(超过四人的进行统计)
select c.*,tmp.courseCount
from course c,
    (select course_id,count(1) courseCount
    from studentcourse
    group by course_id
    having count(1)>4) as tmp
where c.id = tmp.course_id
thank you!

猜你喜欢

转载自blog.csdn.net/travellingMan/article/details/82659171