1.表的结构
课程表:课程编号cid,课程名称canme,老师tid,
教师表:教师tid,教师姓名tname
分数表:学生student_sid=,课程 cours_id,,分数score
学生表:学生sid,学生姓名sname,学生性别sex,出生年月sbrith
2.查询
2.1、自己写的正确的查询sql
34、查询课程名称为"数学",且分数低于60的学生姓名和分数
course student score
sql
SELECT
s.sname,sc.score
FROM
course cs,score sc,student s
WHERE
cs.cname='高数'
AND
cs.cid=sc.cours_id
AND
s.sid=sc.student_sid
AND
sc.score<60
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
sql
SELECT
s.sid,
s.sname,
AVG(score)
FROM
score sc ,student s
WHERE
s.sid=sc.student_sid
AND
score<60
GROUP BY
sc.student_sid
HAVING
count(cours_id)>=2
2.2、自己写的错误的查询sql
22、23、41、42、43
1、查询"1"课程比"2"课程成绩高的学生的信息及课程分数
sql
方式一
SELECT
c.*,
a.score s01,
b.score s02
FROM
score a,score b,student c
WHERE
a.cours_id='1'
AND
b.cours_id='2'
AND
a.student_sid=b.student_sid
AND
a.score>b.score
AND
a.student_sid=c.sid;
sql
方式二行转列
-- 方式二行转列
分析:行 01 01 70
01 02 90
行转成列
01 70 90
步骤1.先写框架
SELECT
FROM
WHERE
步骤2.再从分数表取学号
步骤3.CASE WHEN .. THEN.. ELSE NULL END
步骤4.MAX
步骤5.GROUP BY
步骤6.临时表t,条件课程01>课程02,再与学生表连接(条件是临时表的id跟学生表的id一致)
SELECT
s.*,
t.s01,
t.s02
FROM
(SELECT
a.student_sid,
max(CASE WHEN a.cours_id='1' then a.score END) s01,
max(CASE WHEN a.cours_id='2' then a.score END) s02
FROM
score a
GROUP BY
a.student_sid) t,student s
WHERE
t.s01>t.s02
AND t.student_sid=s.sid;
2、查询"1"课程比"2"课程成绩低的学生的信息及课程分数
sql
方法一
SELECT
s.*,
a.score s01,
b.score s02
FROM
score a,score b,student s
WHERE
a.cours_id='1'
AND b.cours_id='2'
AND a.student_sid=b.student_sid
AND a.score<b.score
AND s.sid=a.student_sid
sql
方法二:
SELECT
c.*,
t.s01,
t.s02
FROM
(SELECT
s.student_sid,
max(CASE WHEN s.cours_id='1' THEN s.score ELSE NULL END) s01,
max(CASE WHEN s.cours_id='2' THEN s.score ELSE NULL END) s02
FROM
score s
GROUP BY
s.student_sid) t ,student c
WHERE
t.student_sid=c.sid
AND
t.s01<t.s02
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
sql
方式一:
SELECT
sc.student_sid,
s.sname,
avg(score) avg_s
FROM
score sc,student s
WHERE
s.sid=sc.student_sid
GROUP BY
sc.student_sid
HAVING
avg_s>=60
方式二:
SELECT
s.student_sid,
(select
sname
FROM
student s01
WHERE
s.student_sid=s01.sid
) sname,
avg(score) avg_s
FROM
score s
GROUP BY
s.student_sid
HAVING
avg_s>=60
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
sql
方式一:
SELECT
s.student_sid,
(SELECT
s01.sname
FROM
student s01
WHERE
s01.sid=s.student_sid
)sname,
avg(score) avg_s
FROM
score s
GROUP BY
s.student_sid
HAVING
avg_s<60
方式二:
SELECT
s.student_sid,
s01.sname,
avg(score) avg_s
FROM
score s
RIGHT JOIN
student s01
on
s.student_sid=s01.sid
GROUP BY
s.student_sid
HAVING
avg_s<60
-- (包括有成绩的和无成绩的)
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
sql
SELECT
sc.student_sid,
s.sname,
count(sc.cours_id),
sum(sc.score)
FROM
student s
LEFT JOIN
score sc
on
s.sid= sc.student_sid
GROUP BY
sc.student_sid
8、查询没学过"张三"老师授课的同学的信息(与14重复)
sql
SELECT
*
FROM
student s
WHERE
s.sid NOT IN(
SELECT
c.student_sid
FROM
tearcher a,course b,score c
WHERE
a.tid=b.tid
AND
c.cours_id=b.cid
AND a.tname='王老师');
12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
sql
SELECT * FROM student s,
(SELECT
DISTINCT
sc.student_sid
FROM score sc
WHERE
sc.cours_id IN(
SELECT
sc.cours_id
FROM score sc
WHERE sc.student_sid = 1)
)b
WHERE s.sid = b.student_sid
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
sql
SELECT s.sid,s.sname,avg(sc.score)
from student s
INNER join score sc
on s.sid = sc.student_sid
where s.sid in(
SELECT
student_sid
from
score s1
where
s1.score<60
group by s1.student_sid
having count(1)>=2)
20、查询学生的总成绩并进行排名
sql
set @num :=0;
select (@num := @num + 1) AS '排名',t.* from (
select sco.student_sid stu,stuu.sname,sum(sco.score) sum_s from score sco,student stuu
where sco.student_sid=stuu.sid
GROUP BY sco.student_sid HAVING sum_s
) t;
sql
set @num :=0;
select sco.student_sid stu,stuu.sname,sum(sco.score) sum_s,(@num := @num + 1) AS '排名' from score sco,student stuu
where sco.student_sid=stuu.sid
GROUP BY sco.student_sid HAVING sum_s
sql
--8.0之后可以使用
SELECT student_sid, row_number() OVER(ORDER BY student_sid DESC) as row_num FROM score;
21、查询不同老师所教不同课程平均分从高到低显示
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
sql
score s
student st
course c
select s.cours_id,count(s.cours_id) from score s GROUP BY s.cours_id
--得到课程号
select course_id from (select s.cours_id course_id,count(s.cours_id) number from score s GROUP BY s.cours_id ) t where t.number >2
select * from score s where s.
select * from score s INNER JOIN (select course_id from (select s.cours_id course_id,count(s.cours_id) number from score s GROUP BY s.cours_id ) t where t.number >2) s2
where s.cours_id =s2.course_id order by s.cours_id desc limit 1,2;
SELECT co.cid 课程,(select sc.student_sid from score sc where sc.cours_id = co.cid order by sc.score desc limit 1, 1) 第二名,
(select sc.student_sid from score sc where sc.cours_id = co.cid order by sc.score desc limit 2, 1) 第三名 from course co;
--得到课程/的第2名,第3名 的学生信息
SELECT co.cid 课程,(select sc.student_sid from score sc where sc.cours_id = co.cid order by sc.score desc limit 1, 1) 第二名,
(select sc.student_sid from score sc where sc.cours_id = co.cid order by sc.score desc limit 2, 1) 第三名 from
(select cid from (select s.cours_id cid,count(s.cours_id) number from score s GROUP BY s.cours_id ) t where t.number >2) co;
(select st.*,sc.score,1 from score sc,student st where sc.student_sid=st.sid and sc.cours_id='1' order by sc.score desc limit 1,2)
union all
(select st.*,sc.score,2 from score sc,student st where sc.student_sid=st.sid and sc.cours_id='2' order by sc.score desc limit 1,2)
23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
sql
select c.cid,c.cname,
Max(sc.score) '最高分',
MIN(sc.score) '最低分',
AVG(sc.score) '平均分',
SUM(case when sc.score<60 then 1 else 0 end) "60分以下的人数",SUM(case when sc.score<60 then 1 else 0 end)/count(sc.student_sid) "60分的百分比",
SUM(case when sc.score>=60 and sc.score<70 then 1 else 0 end) "60-70分的人数",SUM(case when sc.score>=60 and sc.score<70 then 1 else 0 end)/count(sc.student_sid) "60-70分的百分比",
SUM(case when sc.score>=70 and sc.score<80 then 1 else 0 end) "70-80分的人数",SUM(case when sc.score>=70 and sc.score<80 then 1 else 0 end)/count(sc.student_sid) "70-80分的百分比",
SUM(case when sc.score>=80 and sc.score<90 then 1 else 0 end) "80-90分的人数",SUM(case when sc.score>=80 and sc.score<90 then 1 else 0 end)/count(sc.student_sid) "80-90分的百分比",
SUM(case when sc.score>=90 then 1 else 0 end) "90分以上的人数",SUM(case when sc.score>=90 then 1 else 0 end)/count(sc.student_sid) "90分以上的百分比"
from score sc
inner join course c on sc.cours_id=c.cid group by c.cid
29、查询名字中含有"li"字的学生信息
sql
SELECT
s.*
FROM
student s
WHERE
s.sname LIKE '%li%'
40、查询选修"周老师"所授课程的学生中,成绩最高的学生信息及其成绩
sql
SELECT s.* ,sc.score FROM course c,score sc,tearcher t,student s
WHERE
t.tid = c.tid
AND t.tname = '周老师'
AND c.cid = sc.cours_id
AND s.sid = sc.student_sid
ORDER BY sc.score DESC LIMIT 1
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
sql
select c1.student_sid,c1.cours_id,c1.score from score as c1 join score as c2 on c1.student_sid=c2.student_sid
where c1.cours_id !=c2.cours_id and c1.score=c2.score group by c1.student_sid,c1.cours_id;
42、查询每门功成绩最好的前两名
43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
sql
SELECT sc.cours_id,COUNT(1) FROM score sc
LEFT JOIN course c ON c.cid=sc.cours_id
GROUP BY c.cid HAVING COUNT(1)>2
ORDER BY COUNT(1) DESC,sc.cours_id ASC
45、查询选修了全部课程的学生信息
sql
自己写的错误sql
SELECT
s1.*
FROM
course c
LEFT JOIN
score s
ON
c.cid=s.cours_id
LEFT JOIN
student s1
ON
s1.sid=s.student_sid
--成功查询的sql1
select * from student stu
where stu.sid in(
select student_sid from score sco group by sco.student_sid having count(*)=(select count(*) from course)
)
--成功查询的sql2
SELECT stu.* FROM score sco,student stu where sco.student_sid = stu.sid GROUP BY
sco.student_sid HAVING count(sco.cours_id) =(select count(1) from course)
16、检索"1"课程分数小于60,按分数降序排列的学生信息
sql
错误的sql
SELECT
s.*,sc.score,sc.cours_id
FROM
score sc,student s
WHERE
sc.student_sid=s.sid
GROUP BY
sc.cours_id=1
HAVING
sc.score<60
ORDER BY
sc.score
DESC
sql
正确的sql
SELECT
s.*,sc.cours_id,sc.score
FROM
score sc,student s
WHERE
sc.cours_id=1
AND
sc.student_sid=s.sid
AND
sc.score<60
ORDER BY
sc.score
DESC
### 29、查询名字中含有"li"字的学生信息
```sql
错误的sql
SELECT
s.sname
FROM
student s
WHERE
s.sname LIKE %li
sql
正确的sql
SELECT
s.*
FROM
student s
WHERE
s.sname LIKE '%li%'
33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
sql
自己写的错误sql
SELECT
s.sname,s.sid, avg(score) avg_s
FROM
score sc,student s
WHERE
avg_s>=85
and
sc.student_sid=s.sid;
sql
正确sql
SELECT
s.sname,sc.student_sid, avg(score) avg_s
FROM
score sc,student s
WHERE
sc.student_sid=s.sid
GROUP BY
sc.student_sid
HAVING
avg_s>=85
37、查询不及格的课程
sql
自己写的sql:
select
cour_sid
from
score
where
score<60;
sql
正确的sql
SELECT
s.student_sid,c.cid,c.cname
FROM
score s,course c
WHERE
c.cid=s.cours_id
AND
score<60
50、查询下月过生日的学生
自己写的:
sql
SELECT
sname
FROM
student s
WHERE
正确答案:
sql
SELECT
*
from
student s
WHERE
month(s.sbrith)=month(CURDATE())+1;
错误分析:1.不知道下个月是用month(字段)=month(curdate())+1这样的写法
2.根据题干过生日的学生,应该是查询学生表的全部信息,而不是查名字
49、查询本月过生日的学生
自己没有写出来
正确答案:
sql
SELECT
*
from
student s
WHERE
month(s.sbrith)=month(CURDATE());
错误分析:不知道本月是用month(字段)=month(curdate());
46、查询各学生的年龄
-- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
47、查询本周过生日的学生
自己没有写出来
正确答案:
sql
SELECT
*
FROM
student s
WHERE
WEEKOFYEAR(s.sbrith)=WEEKOFYEAR(CURDATE());
错误分析:不知道本周是用weekofyear(字段)=weekyear(curdate());
48、查询下周过生日的学生
自己没有写出来
正确答案:
sql
SELECT
*
FROM
student s
WHERE
WEEKOFYEAR(s.sbrith)=WEEKOFYEAR(CURDATE())+1;
错误分析:不知道下周是用weekofyear(字段)=weekyear(curdate())+1;
44、检索至少选修两门课程的学生学号
自己写的:
sql
SELECT
s.student_sid,
count(s.cours_id) cnt
FROM
score s
GROUP BY
s.student_sid
HAVING
cnt >=2
正确答案:
sql
SELECT
s.student_sid
FROM
score s
GROUP BY
s.student_sid
HAVING
count(s.cours_id) >=2
错误分析:题干是学生学号,select后不应加count的字段
39、求每门课程的学生人数
自己写的:
自己分析到两张表:score student
sql
SELECT
s.cours_id,
count(s.student_sid)
FROM
score s,student s1
WHERE
s1.sid=s.student_sid
GROUP BY
s.cours_id
正确答案:
course score
sql
SELECT
c.cname,
s.cours_id,
COUNT(s.student_sid)
FROM
score s,course c
WHERE
s.cours_id=c.cid
GROUP BY
s.cours_id
错误分析:1.找表找错误,找到score student,而根据题干每门课程的学生人数 应该是course score
38、查询课程编号为1且课程成绩在80分以上的学生的学号和姓名;
score score
自己写的:
sql
SELECT
s01.student_sid,s.sname,s01.score
FROM
score s01,student s
WHERE
s01.cours_id=1
AND
s01.score>=80
AND
s.sid=s01.student_sid
正确答案:
sql
SELECT
s01.student_sid,s.snames
FROM
score s01,student s
WHERE
s01.cours_id=1
AND
s01.score>=80
AND
s.sid=s01.student_sid
错误分析:1.根据题干select后面应该是学号和姓名,自己多写了个课程
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
score course student
自己没写出来
正确答案:
sql
SELECT
s1.sname,
c.cname,
s.score
FROM
score s, student s1,course c
WHERE
s1.sid=s.student_sid
AND
s.cours_id=c.cid
AND
s1.sid IN(
SELECT
sc.student_sid
FROM
score sc
GROUP BY
sc.student_sid
HAVING
MIN(sc.score)>=70)
错误分析:1.根据题干查询任何一门课程成绩在70分以上,没有想到可以用学号在大于70分中的学号查找
2.没有想到到可以求出最小的成绩都大于70分,则其他也大于70分
35、查询所有学生的课程及分数情况;
student score
自己写的:
sql
SELECT
sc.cours_id,sc.score
FROM
score sc
LEFT JOIN
student s
ON
s.sid=sc.student_sid
正确答案:
sql
SELECT
s.sname,cs.cname,sc.score
FROM
student s
LEFT JOIN
score sc
ON s.sid=sc.student_sid
LEFT JOIN
course cs
ON
cs.cid=sc.cours_id
错误分析:1.题干查询所有学生的课程及分数情况;所有学生,的课程,及分数情况,包含三张表,我找了两表.
2.所有学生,应该是以学生表为准
32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
自己写的:
score course
sql
SELECT
cs.cname,
cs.cid,
avg(sc.score) avg
FROM
score sc,course cs
WHERE
sc.cours_id=cs.cid
GROUP BY
cs.cid
HAVING
avg DESC
AND
cs.cid
正确答案1:
sql
SELECT
sc.cours_id,
avg(sc.score) avg
FROM
score sc
GROUP BY 777
sc.cours_id
ORDER BY
avg DESC,
sc.cours_id
错误分析:1.根据题干结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 ,后面排序的时候不能用and,用,直接就行
2.找表错误,我找了两张表,直接在score表中,对每门课程id进行分组
30、查询同名同性学生名单,并统计同名人数
自己没写出来:
正确答案:
sql
SELECT
s.sname,s.sex,count(s.sname) cs
from
student s
GROUP BY
s.sname,s.sex
HAVING
cs>1
错误分析:1.思维受限:没有想到同名人数可以用计数方式>1表示只要大于1,就代表同名
31、查询1997年出生的学生名单
自己写的
sql
SELECT
s.*
FROM
student s
WHERE
sbrith='1997'
正确答案1
sql
SELECT
s.*
FROM
student s
WHERE
s.sbrith LIKE '1997%'
正确答案2
sql
SELECT s.sname FROM student s WHERE YEAR(s.sbrith)='1997'
错误分析:1990出生,不能写成sbrith='1997',而应该写成 LIKE '1997%'或者 YEAR(s.sbrith)='1997'
26、查询每门课程被选修的学生数
自己写的:
sql
SELECT
sc.cours_id,
COUNT(sc.student_sid)
FROM
score sc
GROUP BY
sc.cours_id
正确答案:
sql
SELECT
b.cours_id,a.cname,
COUNT(b.student_sid) cnt
FROM
course a
LEFT JOIN
score b
ON
a.cid=b.cours_id
GROUP BY
b.cours_id
错误分析:根据题干每门课程被选修的学生数 ,应该是按每门课程为准,应该是两张表,只有一张表.
27、查询出只有两门课程的全部学生的学号和姓名
自己写的:
sql
SELECT
s.sid,s.sname,count(sc.cours_id) cnt
FROM
score sc,
student s
WHERE
s.sid=sc.student_sid
AND
count(cnt)>2
正确答案:
sql
SELECT
s.sid,s.sname,count(sc.cours_id) cnt
FROM
score sc,
student s
WHERE
s.sid=sc.student_sid
GROUP BY
s.sid
HAVING
count(cnt)>2
错误分析:1.没有对学号进行分组
28、查询男生、女生人数
自己写的:
sql
SELECT
count(s.sex='男') cg,
count(s.sex='女') cw
FROM
student s
GROUP BY
cg,cs
正确答案
sql
SELECT
s.sex,count(s.sid) cnt
FROM
student s
GROUP BY
s.sex
错误分析:1.没有想到可以根据sex进行分组
2.没有想到统计人数是根据学生id统计
24、查询学生平均成绩及其名次
score
自己写的:
sql
SELECT
sc.student_sid,
avg(sc.score) avgs
FROM
score sc
GROUP BY
sc.student_sid
改:
8.0以上用rank函数,8.0以下5.7用子查询
sql
SELECT
t.*,
rank() over(ORDER BY avgs DESC) rk
FROM
(
SELECT
sc.student_sid,
avg(sc.score) avgs
FROM
score sc
GROUP BY
sc.student_sid) t
错误分析:没有想到名次可以用开窗函数random以及子查询 子查询???,子查询暂时不会,先记录一下
13、查询和"1"号的同学学习的课程完全相同的其他同学的信息
思路:1)根据1所学,构建一张临时表,所有人所学跟的课程跟1所学的课程相同。
sql
SELECT
s01.*,cours_id
FROM
student s01,
(
SELECT
sc.cours_id
FROM
score sc,
student s
WHERE
sc.student_sid=s.sid
AND
s.sid='1')t
2)用构建的跟1所需课程相同的临时表b与每个人真实所学的score表连接,真实比较,如学号跟课程号完全对应,即就是所学相同。SELECT
正确答案2:
sql
CREATE TABLE s01_s_temp AS
SELECT
b.*,s02.cours_id csid
FROM
(
SELECT
s01.*,cours_id
FROM
student s01,
(
SELECT
sc.cours_id
FROM
score sc,
student s
WHERE
sc.student_sid=s.sid
AND
s.sid='1')t )b
LEFT JOIN
score s02
ON
b.sid=s02.student_sid
AND
b.cours_id=s02.cours_id
UNION
SELECT
b.*,s02.cours_id csid
FROM
(
SELECT
s01.*,cours_id
FROM
student s01,
(
SELECT
sc.cours_id
FROM
score sc,
student s
WHERE
sc.student_sid=s.sid
AND
s.sid='1')t ) b
RIGHT JOIN
score s02
ON
b.sid=s02.student_sid
AND
b.cours_id=s02.cours_id
sql
SELECT *FROM s01_s_temp
sql
假设左右没有完全对应\有null就是和1所学不相同
SELECT *FROM s01_s_temp WHERE csid IS NULL OR cours_id IS NULL
SELECT *
FROM
student
WHERE
sid
NOT IN(
SELECT sid FROM s01_s_temp WHERE csid IS NULL OR cours_id IS NULL)
正确答案2:
sql
select
s.*
from
student s
where s.sid
in
(select
distinct
sc.cours_id
from
score sc
where
sc.student_sid <> '1'
and
sc.cours_id
in
(select
distinct
s01.cours_id
from
score s01
where
s01.cours_id = '1')
group by sc.cours_id having count(1)
=(select count(1) from score sc02 where sc02.cours_id='1'))
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
自己写的答案被覆盖了,找不到我写的答案了
正确答案:
tearcher course score student
sql
SELECT
*
FROM
student s
WHERE
s.sid NOT IN(
SELECT
c.student_sid
FROM
tearcher a,course b,score c
WHERE
a.tid=b.tid
AND
c.cours_id=b.cid
AND a.tname='王老师');
错误分析:1.没有想到没有学过张三老师即就是查找出的学号不在学过张三老师课程学号的思路
2.根据题干分析学过"张三"老师讲授的任一门课程的学生姓名 ,应该是四张表进行关联
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
score
自己写的答案被覆盖了,找不到我写的答案了
正确答案:
sql
SELECT
s.sname,sc.cours_id,c.cname,sc.score,avg(sc.score) avg_s
FROM
student s
RIGHT JOIN
score sc
ON
sc.student_sid=s.sid
RIGHT JOIN
course c
ON
c.cid=sc.cours_id
GROUP BY
s.sid
ORDER BY
avg_s
DESC
错误分析:1.自己做的时候没考虑到所有学生,错误是因为两张表找错,根据题干平均成绩从高到低显示所有学生的所有课程的成绩 学生表,分数表
11、查询没有学全所有课程的同学的信息
正确答案:
sql
SELECT
s.*
FROM
score sc
RIGHT JOIN
student s
ON
sc.student_sid=s.sid
GROUP BY
s.sid
HAVING
count(cours_id)<
(
SELECT
COUNT(sco.cours_id)
FROM
score sco
)
错误分析:没有想到没有学全即就是通过分组后通过having的课程数小于学全课程数,根据两张表student,score
10、查询学过编号为"1"但是没有学过编号为"2"的课程的同学的信息
正确答案:
sql
SELECT
s.*
FROM
score a,student s
WHERE
a.cours_id=1
AND
s.sid=a.student_sid
AND
s.sid
NOT IN
(
SELECT
c.student_sid
FROM
score c
WHERE
c.cours_id=2
);
错误分析:1.没有想到没有学过编号为"2"的课程的同学信息即就是学号不在学号为2的课程中的学生
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
自己找的表:course student两表
正确答案:
score a,score b,student s三张表
sql
SELECT
s.*
FROM
score a,score b,student s
WHERE
a.cours_id='1'
AND
b.cours_id='2'
AND
a.student_sid=b.student_sid
AND
s.sid=b.student_sid
错误分析:找错表
7、查询学过"张三"老师授课的同学的信息
sql
SELECT
s01.*
FROM
(
SELECT
t.cid tcid
FROM
tearcher t
WHERE
tname='王老师') t,
score s,
student s01
WHERE
t.tcid=s.cours_id
AND
s01.sid=s.student_sid
开始第一遍做的:
sql
SELECT
s2.*
FROM
(SELECT
s.student_sid sid
FROM
(
SELECT
t.tid,
t.cid,
t.tname
FROM
tearcher t
WHERE
tname='李老师') t
INNER JOIN
score s
ON
t.cid=s.cid) s1,student s2
WHERE
t.sid=s2.sid;
正确答案一:
1.根据张三老师在teacher表找出tid
2.根据查找出的tid在course中的找到对应的cid
3.再根据course中的cid再score表找到对应的sid
4.再根据sid在student表找到相关信息
sql
SELECT
d.*
FROM
tearcher a,course b,score c,student d
WHERE
a.tid=b.tid
AND
c.cours_id=b.cid
AND
c.student_sid=d.sid
AND a.tname='王老师'
正确答案二:
sql
SELECT * FROM
student
WHERE
sid in(
SELECT
c.student_sid
FROM
tearcher a,course b,score c
WHERE
a.tid=b.tid
AND
c.cours_id=b.cid
AND a.tname='王老师'
)
错误分析:根据题干学过"张三"老师授课的同学的信息 ,应该是四张表
6、查询"李"姓老师的数量
tearcher
自己写的:
SELECT
count(tid)
FROM
tearcher t
WHERE
sname LIKE %李%
正确答案:
sql
SELECT
COUNT(tname)
FROM
tearcher
WHERE
tname like '李%'
错误分析:没有弄清楚%的用法,%%是找中间含有的字,放在后面是找后面的,这里还需要好好在记录一下s
18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
自己写的:
score course
sql
SELECT
s.cours_id,s.cname,max(s.score),min(s.score),avg(s.sccore),
FROM
score s,course c
GROUP BY
s.cours_id
正确答案:
score course
sql
SELECT
s.cours_id,c.cname,max(s.score),min(s.score) 最低分,avg(s.score) 平均分,
SUM(CASE WHEN s.score>=60 THEN 1 ELSE 0 END)/count(1) 及格率,
SUM(CASE WHEN s.score>=70 AND s.score<80 THEN 1 ELSE 0 END)/count(1) 中等率,
SUM(CASE WHEN s.score>=80 AND s.score<90 THEN 1 ELSE 0 END)/count(1) 优良率,
SUM(CASE WHEN s.score>=90 THEN 1 ELSE 0 END)/count(1) 优秀率
FROM
course c
LEFT JOIN
score s
ON
s.cours_id=c.cid
GROUP BY
s.cours_id
错误分析:没想到可以用case when 1.及格率、中等率,优良率,优秀率是用分数的总和除以总数
19、按各科成绩进行排序,并显示排名(实现不完全)
自己写的:
sql
score course
SELECT
RAND() over()
FROM
(
SELECT
c.cname,s.score
FROM
score s ,course c
WHERE
s.cours_id=cours_id
ORDER BY
s.score desc)
正确答案一:
函数8.0以上使用
sql
SELECT
s.*,RANK() over(partition order by s_cour_id DESC)
FROM
score s
正确答案二:
8.0以下使用子查询方式
sql
SELECT
s.*,(SELECT count( sc.score) from score sc where sc.cours_id=s.cours_id AND sc.score<s.score) rk
FROM
score s
错误分析:1.排名应该是写rank() over(partion order by 字段 desc)
2.子查询的方式条件是