写sql语句思路--28道关于教师、学生、成绩表的练习题---个人思路
针对学生教师的28道练习题的思路
链接:https://pan.baidu.com/s/1TgqFAe7i0PAkZOm47-Jd0A
提取码:vvi6
部分截图如下:
-- sql 28道练习题答案
-- 1.查询“某1”课程比“某2”课程成绩高的所有学生的学号 SELECT sid,sname FROM student WHERE sid IN ( SELECT a.sid FROM (SELECT s.score,s.sid FROM score s INNER JOIN course c ON c.cid= s.cid WHERE c.cname='语文')AS a, (SELECT s.score,s.sid FROM score s INNER JOIN course c ON c.cid= s.cid WHERE c.cname='英语')AS b WHERE a.sid=b.sid AND a.score>b.score ) -- 外部应用内连接查询1,内部应用内连接查询1+内连接查询2+条件查询(where)and -- 2.查询平均成绩大于60分的同学的学号和平均成绩; SELECT AVG(score),sid FROM score AVG(s.score)>60 -- 错误 SELECT AVG(score),sid FROM score s GROUP BY sid HAVING AVG(s.score)>60 -- 正确 -- 聚合函数,分组查询 -- 3.查询所有同学的学号、姓名、选课数、总成绩 SELECT SUM(score),COUNT(cid),sid FROM score GROUP BY sid -- 总成绩,选课数,学号 SELECT sname,sid sid FROM student GROUP BY sid -- 学号,姓名 SELECT SUM(score),COUNT(cid),s.sid,s.sname sid FROM score sc LEFT JOIN student s ON sc.sid=s.sid GROUP BY s.sid -- 总成绩,选课数,学号,姓名 -- 左外连接查询,分组查询,聚合函数 -- 4.查询姓“李”的老师的个数; SELECT COUNT(tid) FROM teacher WHERE tname='李' -- 聚合函数,条件查询 -- 5.查询没学过“李”老师课的同学的学号、姓名; SELECT c.cid,c.tid,sc.sid FROM course c LEFT JOIN score sc ON c.cid = sc.cid -- 查询课程相同的学生信息 SELECT t.tid,c.cid,t.tname FROM teacher t RIGHT JOIN course c ON t.tid =c.tid WHERE t.tname='李' -- 查询李老师教的课的课程信息 -- 上过李老师课的学生学号 SELECT a.sid FROM (SELECT c.cid,c.tid,sc.sid FROM course c INNER JOIN score sc ON c.cid = sc.cid)AS a, (SELECT t.tid,c.cid,t.tname FROM teacher t INNER JOIN course c ON t.tid =c.tid WHERE t.tname='李')AS b WHERE a.tid = b.tid -- 没上过李老师课的学生学号,姓名 SELECT sid,sname FROM student WHERE sid NOT IN ( SELECT a.sid FROM (SELECT c.cid,c.tid,sc.sid FROM course c RIGHT JOIN score sc ON c.cid = sc.cid)AS a, (SELECT t.tid,c.cid,t.tname FROM teacher t RIGHT JOIN course c ON t.tid =c.tid WHERE t.tname='李')AS b WHERE a.tid = b.tid ) -- 同第一题解题思路+not in 的使用,外部条件查询in+内部右(左,内都可以)连接查询,条件查询where -- 6.查询学过“```”并且也学过“```”课程的同学的学号、姓名; SELECT sc.sid,c.cid FROM course c INNER JOIN score sc ON c.cid=sc.cid WHERE c.cname='语文' -- 学过语文的学生学号 SELECT sc.sid,c.cid FROM course c INNER JOIN score sc ON c.cid=sc.cid WHERE c.cname='英语' -- 学过英语的学生学号 -- 都学过的学号相同 SELECT sid,sname FROM student WHERE sid IN ( SELECT a.sid FROM (SELECT sc.sid,c.cid FROM course c INNER JOIN score sc ON c.cid=sc.cid WHERE c.cname='语文')AS a, (SELECT sc.sid,c.cid FROM course c INNER JOIN score sc ON c.cid=sc.cid WHERE c.cname='英语')AS b WHERE a.sid=b.sid ) -- 外部笛卡尔积查询,条件查询(in), -- 7.查询学过“李”老师所教的所有课的同学的学号、姓名; SELECT c.cid FROM course c INNER JOIN teacher t ON c.tid=t.tid WHERE t.tname='李' -- 查询李老师教的课程 SELECT sc.sid FROM course c INNER JOIN score sc ON c.cid=sc.cid -- 查询学生的学号及所学课程的课程号 SELECT sid,sname FROM student WHERE sid IN( SELECT a.sid FROM (SELECT c.cid,sc.sid FROM course c INNER JOIN score sc ON c.cid=sc.cid)AS a, (SELECT c.cid FROM course c INNER JOIN teacher t ON c.tid=t.tid WHERE t.tname='李')AS b WHERE a.cid=b.cid )
---------------------------------------------------------------------------------------------------
演草纸中思路:
第一题: //语文比数学成绩高的所有学生的学号 应用理解:班级学生应用 1.所有学生的语文成绩:成绩表,课程表,并得到学号 2.所有学生的数学成绩:成绩表,课程表,并得到学号 3.同一个学生sid=sid:a.sid = b.sid 4.语文成绩>数学成绩 where a.score = b.score 1.select a.sid from (select s.score,s.sid from score s inner join course c on c.cid= s.cid where c.cname='语文')as a 2. 3. 4. 从学生表中查找、根据学号查找,求学号, 根据两张表求学号 select sid from student where sid in ( select a.sid from (select s.score,s.sid from score s inner join course c on c.cid= s.cid where c.cname='语文')as a, select b.sid from (select s.score,s.sid from score s inner join course c on c.cid= s.cid where c.cname='数学')as b where a.sid=b.sid and a.score>b.score ) ( SELECT a.sid FROM (SELECT s.score,s.sid FROM score s INNER JOIN course c ON c.cid= s.cid WHERE c.cname='语文')AS a,(SELECT s.score,s.sid FROM score s INNER JOIN course c ON c.cid= s.cid WHERE c.cname='英语')AS b WHERE a.sid=b.sid AND a.score>b.score ) select a.sid from a,b where a.sid=b.sid 2.查询平均成绩大于60分的同学的学号和平均成绩; 平均成绩avg(score)>60 sid 成绩表 select avg(score),sid from score avg(s.score)>60 SELECT AVG(score),sid FROM score s GROUP BY sid HAVING AVG(s.score)>60 3.查询所有同学的学号、姓名、选课数、总成绩 应用:班级教师想要了解学生情况 学号,总成绩,选课数,-成绩表,课程表 姓名 学生表 select sc.sid,sum(score),count(cid) from score sc inner join course c on sc.cid=c.cid (SELECT SUM(score) ssum,COUNT(cid) ccount,sid FROM score GROUP BY sid) as a (SELECT sname,sid FROM student GROUP BY sid)as b 这两个表如何连接呢?有共同的sid,合并共同的,留下不同的,用什么方法, 连接查询 SELECT SUM(score),COUNT(cid),sid sid FROM score left join student group by sid 4.查询姓“李”的老师的个数 select count(tid) from teacher where tname='李' -- 聚合函数,条件查询 5.查询没学过“李”老师课的同学的学号、姓名; 查询上课的学生学号、姓名 select sid,sname 成绩表,学生表 没上过'李'老师的课 上过'李'老师的课 where tname='李' 查询学号: 上过李老师课的学生学号 不在上过'李'老师学生 学号 中查询 上过李老师课的学生学号: select sid from 上过李老师的课 select a.sid from (select sc.sid from course c inner join score sc on c.cid = sc.cid )as a ,(select * from teacher t inner join course c on t.tid =c.tid where t.tname='李') 6.查询学过“语文”并且也学过“英语”课程的同学的学号、姓名; 查询学过语文的同学学号 查询学过英语的同学学号 sid=sid 通过学号from student 得到sname select sid,sname from student where sid in 查询学过语文的同学学号: select sc.sid,c.cid from course c inner join score sc where c.cname='语文' 7.查询学过“李”老师所教的所有课的同学的学号、姓名 查询李老师教的课 select c.cid from course c inner join teacher t on c.tid=t.tid where t.tname='李' select sid,sname from student where sid in -- 根据学号查询学生姓名 select sc.sid from course c inner join score s on c.cid=sc.cid
成就人