python day40 数据库——连表查询和子查询

一、内容回顾

1、查询男生、女生的人数;

select gender,count(gender) as count from student group by gender

2、查询姓“张”的学生名单;

select * from student where sname like'张%'

3、课程平均分从高到低显示

select c.cname,avg(s.num) as avg_num from score as s inner join course as c on c.cid = s.course_id group by c.cname order by avg_num desc

4、查询有课程成绩小于60分的同学的学号、姓名;

select sname,s.sid,sc.num from student s inner join score as sc on s.sid = sc.student_id where sc.num < 60

5、查询至少有一门课与学号为1的同学所学课程相同的同学的学号和姓名;

select distinct st.sname,st.sid from student as st inner join score as s on st.sid = s.student_id where s.course_id in (select course_id from score where student_id = '1')

6、查询出只选修了一门课程的全部学生的学号和姓名;

select st.sname,st.sid from student as st inner join (select student_id,count(course_id) as num from score group by student_id having num = 1) as c on c.student_id = st.sid

7、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

select course_id,max(num) as max_score,min(num) as min_score from score group by course_id

8、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;

select student.sid,sname from (select * from score where course_id = 1) as c1 inner join (select * from score where course_id = 2 ) as c2 on c1.student_id = c2.student_id inner join student on c1.student_id = student.sid where c2.num < c1.num

9、查询“生物”课程比“物理”课程成绩高的所有学生的学号;

select student.sid,sname from (select * from score where course_id = 1) as c1 inner join (select * from score where course_id = 2 ) as c2 on c1.student_id = c2.student_id inner join student on c1.student_id = student.sid where c2.num > c1.num

10、查询平均成绩大于60分的同学的学号和平均成绩;

select student_id,avg(num) as avg_num from score group by student_id having avg_num > 60

11、查询所有同学的学号、姓名、选课数、总成绩;

select st.sname,st.sid,count(s.course_id) as sum_course,sum(s.num) as sum_score from student as st left join score as s on st.sid = s.student_id group by st.sname,st.sid order by st.sid

12、查询姓“李”的老师的个数;

select count(*) as num from teacher where tname like '李%'

13、查询没学过“张磊老师”课的同学的学号、姓名;

select distinct sid,sname from student where sid not in ( select distinct student_id from score where course_id = 1)

14、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名;

select st.sname,s.student_id from score as s inner join (select student_id from score where course_id=1) as s1 on s1.student_id = s.student_id inner join student as st on st.sid = s.student_id where s.course_id = 2

15、查询学过“李平老师”所教的所有课的同学的学号、姓名;

select st.sname,s.student_id from score as s inner join (select student_id from score where course_id=2) as s1 on s1.student_id = s.student_id inner join student as st on st.sid = s.student_id where s.course_id = 4

select * from student select st.sname,st.sid from student as st inner join (select student_id,count(course_id) as num from score where course_id in (select cid from course c inner join teacher t on c.teacher_id = t.tid where t.tname = '李平老师') group by student_id HAVING num =(select count(*) as num from course c inner join teacher t on c.teacher_id = t.tid where t.tname = '李平老师')) as a1 on a1.student_id = st.sid

 

 

1、查询没有学全所有课的同学的学号、姓名;

select st.sname,count(s.course_id) as sum_course from student as st left join score as s on st.sid = s.student_id group by st.sname having sum_course < (select count(*) as sum_course from course)

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

select st.sname,st.sid from student as st inner join (select * from (select student_id,count(course_id) as sum_course from score group by student_id having count(course_id) = 3) a where a.student_id not in (select s.student_id from score as s inner join (select student_id,count(course_id) as sum_course from score group by student_id having count(course_id) = 3) as a1 on a1.student_id=s.student_id where s.course_id not in (select course_id from score where student_id= '002'))) as a1 on a1.student_id = st.sid

3、删除学习“李平”老师课的SC表记录;

delete from score where course_id in (select cid from course c inner join teacher as t on t.tid = c.teacher_id where t.tname = '李平老师')

4、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;

insert into score(student_id,course_id,num) (select distinct student_id,2 as course_id,(select avg(num) as avg_num from score where course_id = 2) as num from score where student_id not in (select student_id from score where course_id = 2))

 

5、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;

select st.sid,st.sname, a1.avg_num as '物理',a2.avg_num as '美术',a3.avg_num as '体育',a4.avg_num as '生物',a5.avg_num as '有效平均分' from student as st left join (select s.student_id,c.cname,avg(num) as avg_num from score as s inner join course c on s.course_id = c.cid where cname = '物理' group by s.student_id,c.cname) a1 on a1.student_id = st.sid left join (select s.student_id,c.cname,avg(num) as avg_num from score as s inner join course c on s.course_id = c.cid where cname = '美术' group by s.student_id,c.cname) a2 on a2.student_id = st.sid left join (select s.student_id,c.cname,avg(num) as avg_num from score as s inner join course c on s.course_id = c.cid where cname = '体育' group by s.student_id,c.cname) a3 on a3.student_id = st.sid left join (select s.student_id,c.cname,avg(num) as avg_num from score as s inner join course c on s.course_id = c.cid where cname = '生物' group by s.student_id,c.cname) a4 on a4.student_id = st.sid left join (select s.student_id,avg(num) as avg_num from score as s inner join course c on s.course_id = c.cid group by s.student_id) a5 on a5.student_id = st.sid where a5.avg_num is not null order by a1.avg_num,a2.avg_num,a3.avg_num

 

6、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

select c.cname,max(s.num) as '最高分',min(s.num) as '最低分' from score s inner join course c on c.cid = s.course_id group by c.cname

7、按各科平均成绩从低到高和及格率的百分数从高到低顺序;

select c.cname,a1.最高分,a1.最低分,(a3.person/a2.sum_p)*100 as 及格率 from course as c left join (select c.cname,max(s.num) as '最高分',min(s.num) as '最低分',count(c.cname) as '总人数' from score s inner join course c on c.cid = s.course_id group by c.cname) a1 on a1.cname = c.cname left join ( select c.cname,count(c.cname) as sum_p from score s inner join course c on c.cid = s.course_id group by c.cname) a2 on a2.cname = c.cname left join (select c.cname,count(c.cname) as person from score s inner join course c on c.cid = s.course_id where s.num > 60 group by c.cname) a3 on a3.cname = c.cname order by a1.最高分,a1.最低分 desc

8、查询各科成绩前三名的记录:(不考虑成绩并列情况)

 

 

9、查询每门课程被选修的学生数;

select c.cname,count(sc.student_id) as sum_student from score sc inner join course c on c.cid =sc.course_id group by c.cname

10、查询同名同姓学生名单,并统计同名人数;

select a.sname,count(a.sname) as sl from (select distinct s.sid,s.sname,1 as 'sl' from student s inner join (select sid,sname from student ) a1 on (a1.sname = s.sname and a1.sid <> s.sid)) a group by a.sname

 

11、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;

select * from (select c.cid,c.cname,avg(num) as avg_num from score as s inner join course c on c.cid = s.course_id group by c.cname ) a order by a.avg_num

12、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;

select s.student_id,st.sname,avg(num) as avg_num from score s inner join student as st on st.sid = s.student_id group by s.student_id,st.sname HAVING avg(num)> 85

13、查询课程名称为“数学”,且分数低于60的学生姓名和分数;

 

select st.sname,s.num from score s inner join course c on s.course_id = c.cid inner join student st on st.sid = s.student_id where c.cname ='体育' and s.num < 60

14、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;

select s.student_id,st.sname,s.num from score s inner join course c on s.course_id = c.cid inner join student st on st.sid = s.student_id where c.cid =003 and s.num > 80

15、求选了课程的学生人数

select count(*) from (select distinct student_id from score) a

16、查询选修“刘海燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩;

select st.sname,s.student_id,s.course_id,num from score as s inner join course c on s.course_id = c.cid inner join teacher t on t.tid = c.teacher_id inner join student st on st.sid = s.student_id where t.tname = '刘海燕老师' and num = (select max(num) from score as s inner join course c on s.course_id = c.cid inner join teacher t on t.tid = c.teacher_id where t.tname = '刘海燕老师' )

 

17、查询各个课程及相应的选修人数;

select c.cname,count(s.student_id) from score s inner join course c on c.cid = s.course_id group by c.cname

18、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;

 

19、查询每门课程成绩最好的前两名;

 

20、检索至少选修两门课程的学生学号;

select student_id,count(course_id) as sum_course from score group by student_id HAVING count(course_id) >=2

21、查询全部学生都选修的课程的课程号和课程名;

select course_id,count(student_id) as sum_stu from score group by course_id HAVING count(student_id) = (select count(*) from student)

22、查询没学过“李平”老师讲授的任一门课程的学生姓名;

select * from student where sid not in (select distinct s.student_id from score s inner join course c on s.course_id = c.cid inner join teacher t on t.tid = c.teacher_id where t.tname = '李平老师')

 

23、查询两门以上不及格课程的同学的学号及其平均成绩;

select a.student_id,count(a.student_id),a1.avg_num from (select student_id,num from score where num < 60) a inner join (select student_id,num,avg(num) as avg_num from score group by student_id) a1 on a.student_id = a1.student_id group by a.student_id HAVING count(a.student_id) >=2

 

24、检索“004”课程分数小于60,按分数降序排列的同学学号;

select * from score where course_id =004 and num < 60 order by num desc

 

25、删除“002”同学的“001”课程的成绩;

delete from score where student_id= 002 and course_id =001

 

二 今日内容

所谓连表
总是在连接的时候创建一张大表,里面存放的是两张表的笛卡尔积
再根据条件进行筛选就可以了

表与表之间的连接方式
内连接 inner join ... on ...
select * from 表1,表2 where 条件;(了解)
select * from 表1 inner join 表2 on 条件
select * from department inner join employee on department.id = employee.dep_id;
select * from department as t1 inner join employee as t2 on t1.id = t2.dep_id;
外连接
左外连接 left join ... on ...
select * from 表1 left join 表2 on 条件
select * from department as t1 left join employee as t2 on t1.id = t2.dep_id;
右外连接 right join ... on ...
select * from 表1 right join 表2 on 条件
select * from department as t1 right join employee as t2 on t1.id = t2.dep_id
全外连接 full join
select * from department as t1 left join employee as t2 on t1.id = t2.dep_id
union
select * from department as t1 right join employee as t2 on t1.id = t2.dep_id;

 

 

 

posted @ 2021-02-23 15:56  小鱼鱼与黄黄  阅读(576)  评论(0编辑  收藏  举报