1、查詢“生物”課程比“物理”課程成績高的所有學生的學號;
select * from(
(select * from score where course_id in (select cid from course where cname = '生物')) t1
left join
(select * from score where course_id in (select cid from course where cname = '物理')) t2
on t1.student_id = t2.student_id)
where t1.num > t2.num;
2、查詢平均成績大於60分的同學的學號和平均成績;
# 先查看每個同學的平均分數
select student_id,avg(num) from score group by student_id;
# 在篩選成績大於60分的同學的學號和平均成績;
# select student_id,avg(num) from score group by student_id having avg(num) > 60;
3、查詢所有同學的學號、姓名、選課數、總成績;
# 先查看每個同學的總成績
select student_id,sum(num) from score group by student_id;
# 學生和課程的關系只有成績表中存在,因此要獲取每個學生選擇的課程,需要通過score表
select count(sid),student_id from score group by student_id;
# 將上面兩步合並
select sum(num),count(sid),student_id from score group by student_id;
# 將學生的信息和成績選課情況拼在一起
select sid,sname,sum_num ,count_stu
from student
left join
(select sum(num) sum_num,count(sid) count_stu,student_id from score group by student_id) t2
on sid = student_id;
# 還可以更嚴謹,那些沒有選課的同學選課數和總成績應該是0
select sid,sname,
(
CASE
WHEN sum_num is null THEN 0
ELSE sum_num
END
) as sum_num ,
(
CASE
WHEN count_stu is null THEN 0
ELSE count_stu
END
) as count_stu
from student
left join
(select sum(num) sum_num,count(sid) count_stu,student_id from score group by student_id) t2
on sid = student_id;
4、查詢姓“李”的老師的個數;
# 找到所有姓李的
# 方法一
# select * from teacher where tname like '李%';
# 方法二
# select * from teacher where tname regexp '^李';
# 統計個數
select count(tid) from teacher where tname regexp '^李';
或者
select count(id) from teacher where tname like '李%';
5、查詢沒學過“張磊老師”課的同學的學號、姓名;
# 找到張磊老師的id
select tid from teacher where tname == '張磊老師';
# 找到張磊老師所教課程
select cid from course where teacher_id = (select tid from teacher where tname = '張磊老師');
# 找到所有學習這門課的學生id
select student_id from score where course_id = (select cid from course where teacher_id = (select tid from teacher where tname = '張磊老師'));
# 找到沒有學過這門課的學生對應的學生學號、姓名
select sid,sname from student where sid not in
(select student_id from score where course_id = (select cid from course where teacher_id = (select tid from teacher where tname = '張磊老師'))
);
6、查詢學過“1”並且也學過編號“2”課程的同學的學號、姓名;
# 先查詢學習課程id為1的所有學生
select * from score where course_id = 1;
# 先查詢學習課程id為2的所有學生
select * from score where course_id = 2;
# 把這兩張表按照學生的id 內連接起來 去掉只學習某一門課程的學生
select t1.student_id from
(select student_id from score where course_id = 1) t1
inner join
(select student_id from score where course_id = 2) t2
on t1.student_id = t2.student_id
# 根據學號在學生表中找到對應的姓名
select sid,sname from student where sid in (select t1.student_id from (select student_id from score where course_id = 1) t1 inner join (select student_id from score where course_id = 2) t2 on t1.student_id = t2.student_id);
7、查詢學過“李平老師”所教的所有課的同學的學號、姓名;
#找到李平老師的tid
select tid from teacher where tname ='李平老師';
# 找到李平老師教的所有課程cid
select cid from course where teacher_id in (select tid from teacher where tname ='李平老師');
# 找到李平老師教的所有課程數
select count(cid) from course where teacher_id in (select tid from teacher where tname ='李平老師');
# 找到所有學習李平老師課程的學生
select * from score where course_id in ( select cid from course where teacher_id in (select tid from teacher where tname ='李平老師'));
# 查看所有學習李平老師課程的學生選課數
select student_id,count(course_id) from score where course_id in ( select cid from course where teacher_id in (select tid from teacher where tname ='李平老師')) group by student_id;
# 找到所有選擇了李平老師所有課程的學生id
select student_id from (
select student_id,count(course_id) course_count from score where course_id in ( select cid from course where teacher_id in (select tid from teacher where tname ='李平老師')) group by student_id) t1
where t1.course_count =
(select count(cid) from course where teacher_id in (select tid from teacher where tname ='李平老師'));
# 找到學生的其他信息
select sid,sname from student where sid in (
select student_id from (
select student_id,count(course_id) course_count from score where course_id in ( select cid from course where teacher_id in (select tid from teacher where tname ='李平老師')) group by student_id) t1
where t1.course_count =
(select count(cid) from course where teacher_id in (select tid from teacher where tname ='李平老師'))
);
8、查詢課程編號“2”的成績比課程編號“1”課程低的所有同學的學號、姓名;
# 先找到每個學生的課程編號“1”的和課程編號“2”的成績組成一張表
select t1.student_id from (select num num2,student_id from score where course_id = 2) t2 inner join (select student_id,num num1 from score where course_id = 1) t1 on t1.student_id = t2.student_id
# 再找到課程編號“2”的成績比課程編號“1”課程低的所有學生的學號
select t1.student_id from (select num num2,student_id from score where course_id = 2) t2 inner join (select student_id,num num1 from score where course_id = 1) t1 on t1.student_id = t2.student_id where num2 < num1
# 再找到所有學生的學號、姓名
select sid,sname from student where sid in(select t1.student_id from (select num num2,student_id from score where course_id = 2) t2 inner join (select student_id,num num1 from score where course_id = 1) t1 on t1.student_id = t2.student_id where num2 < num1);
9、查詢有課程成績小於60分的同學的學號、姓名;
# 先查詢成績小於60分的同學的學號
select distinct student_id from score where num < 60;
# 再查詢有課程成績小於60分的同學的學號、姓名
select sid,sname from student where sid in (select distinct student_id from score where num < 60);
10、查詢至少有一門課與學號為1的同學所學課程相同的同學的學號和姓名;
# 先看看學號為1的同學都學了哪些課程
select course_id from score where student_id = 1
# 找到學習 學號為1的同學所學課程 的學號
select distinct student_id from score where course_id in (select course_id from score where student_id = 1);
# 找到學習 學號為1的同學所學課程 的學號\姓名
select sid,sname from student where sid in (select distinct student_id from score where course_id in (select course_id from score where student_id = 1));
11、課程平均分從高到低顯示
select course_id,avg(num) avg_num from score group by course_id order by avg_num desc;
12、查詢出只選修了一門課程的全部學生的學號和姓名;
# 查詢出只選修了一門課程的全部學生的學號
select student_id,count(student_id) from score group by student_id having count(student_id) =1;
# 查詢出只選修了一門課程的全部學生的學號和姓名;
select sid,sname from student where sid in (select student_id from score group by student_id having count(student_id) =1);
13、查詢男生、女生的人數;
select gender,count(sid) from student group by gender;
14、查詢姓“張”的學生名單;
select * from student where sname like '張%';
15、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分;
# 查詢成績的最高分
select course_id c1,max(num) from score group by course_id
# 查詢成績的最低分
select course_id c1,min(num) from score group by course_id
# 查詢成績的最高分和最低分拼接
select * from ( (select course_id c1,max(num) from score group by course_id) t1 inner join (select course_id c2,min(num) from score group by course_id) t2 on t1.c1 = t2.c2 );
# 格式整理
select t1.c1,t1.max_num,t2.min_num from ( (select course_id c1,max(num) max_num from score group by course_id) t1 inner join (select course_id c2,min(num) min_num from score group by course_id) t2 on t1.c1 = t2.c2 );
