select 训练

Posted Cosplay-Berlin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了select 训练相关的知识,希望对你有一定的参考价值。

--1、查询“c001”课程比“c002”课程成绩高的所有学生的学号;
SELECT b.sno FROM (SELECT * FROM sc WHERE cno=‘c001‘) a,
(SELECT * FROM sc WHERE cno=‘c002‘) b
WHERE a.sno=b.sno AND a.score>b.score;

--2、查询平均成绩大于60 分的同学的学号和平均成绩;
SELECT AVG(score),sno FROM sc GROUP BY sno
HAVING AVG(score)>60;

--3、查询所有同学的学号、姓名、选课数、总成绩;
SELECT a.sno,a.sname,b.cno,b.score
FROM student a,sc b WHERE a.sno=b.sno;

--4、查询姓“刘”的老师的个数;
SELECT COUNT(1) FROM teacher WHERE tname LIKE‘刘%‘;

--5、查询没学过“谌燕”老师课的同学的学号、姓名;
SELECT sno,sname FROM student
WHERE sno NOT IN(SELECT sno FROM sc
WHERE cno IN (SELECT cno FROM course
WHERE tno IN(SELECT tno FROM teacher WHERE tname=‘谌燕‘)));

--6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
SELECT sno,sname FROM student
WHERE sno IN(SELECT sno FROM sc WHERE cno=‘c001‘ OR cno=‘c002‘);

--7、查询学过“谌燕”老师所教的所有课的同学的学号、姓名;
SELECT sno,sname FROM student
WHERE sno IN(SELECT sno FROM sc
WHERE cno IN (SELECT cno FROM course
WHERE tno IN(SELECT tno FROM teacher WHERE tname=‘谌燕‘)));

--8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;
SELECT sno,sname FROM student
WHERE sno IN
(
SELECT b.sno FROM (SELECT * FROM sc WHERE cno=‘c001‘) a,
(SELECT * FROM sc WHERE cno=‘c002‘) b
WHERE a.sno=b.sno AND a.score>b.score
);
--9、查询所有课程成绩小于60 分的同学的学号、姓名;
SELECT sno,sname FROM student
WHERE sno IN
(
SELECT sno FROM sc WHERE score <60
);

--10、查询没有学全所有课的同学的学号、姓名;
SELECT sc.sno,student.sname FROM sc, student  
WHERE sc.sno=student.sno
GROUP BY sc.sno,student.sname
having count(cno)<(SELECT count (cno) FROM course);

--11、查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名;
SELECT sno,sname FROM student
WHERE sno IN(SELECT sno FROM sc WHERE cno
IN(SELECT course.cno FROM course ,sc
WHERE sc.cno=course.cno AND sc.sno=‘s001‘));

--12、查询至少学过学号为“s001”同学所有一门课的其他同学学号和姓名;
SELECT sno,sname FROM student
WHERE sno IN(SELECT sno FROM sc WHERE cno
IN(SELECT course.cno FROM course ,sc
WHERE sc.cno=course.cno AND sc.sno=‘s001‘) AND sno!=‘s001‘);

--13、把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩;
UPDATE sc SET score=AVG(score)
WHERE cno IN(SELECT cno FROM course
WHERE tno=(SELECT tno FROM teacher
WHERE tname=‘谌燕‘));

--14、查询和“s001”号的同学学习的课程完全相同的其他同学学号和姓名;
SELECT sno,sname FROM student
WHERE sno IN(SELECT sno FROM sc
WHERE cno IN(SELECT cno FROM sc
WHERE sno=‘s001‘) AND sno!=‘s001‘);

--15、删除学习“谌燕”老师课的SC 表记录;
DELETE FROM sc
WHERE cno IN(SELECT cno FROM sc
WHERE cno IN(SELECT cno FROM course
WHERE tno IN(SELECT tno FROM teacher
WHERE tname=‘谌燕‘)));
分,最低分

--16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“c002”课程的同学学号、“c002”号课的平均成绩;
INSERT INTO sc
VALUES((SELECT sno FROM sc WHERE cno!=‘c002‘),‘c002‘,
(SELECT AVG(score) FROM sc WHERE cno=‘c002‘));

--17、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
SELECT sno 课程ID,MAX(s.score) 最高分,MIN(s.score) 最低分
FROM SC s GROUP BY sno;
--18、按各科平均成绩从低到高和及格率的百分数从高到低顺序
SELECT cno,avg(score),sum(case WHEN score>=60 THEN 1 ELSE 0 END)/count(*)
as 及格率
FROM sc GROUP BY cno
ORDER BY avg(score) desc
--19、查询不同老师所教不同课程平均分从高到低显示
SELECT MAX(t.tno),MAX(t.tname),MAX(c.cno),MAX(c.cname),c.cno,avg(score) FROM sc , course c,teacher t
WHERE sc.cno=c.cno AND c.tno=t.tno
GROUP BY c.cno
ORDER BY avg(score) desc

--20、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
SELECT sc.cno,c.cname,
SUM(CASE  WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS "[100-85]",
SUM(CASE  WHEN score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS "[85-70]",
SUM(CASE  WHEN score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS "[70-60]",
SUM(CASE  WHEN score <60 THEN 1 ELSE 0 END) AS "[<60]"
FROM sc, course c
WHERE  sc.cno=c.cno
GROUP BY sc.cno ,c.cname;
--21、查询各科成绩前三名的记录:(不考虑成绩并列情况)
SELECT c.cno 课程编号,c.cname 课程名称, d.sname 学生名称,s.r1 名次,s.score 成绩
FROM (SELECT sno,cno,score,row_number() OVER(PARTITION BY cno ORDER BY score DESC) r1
FROM sc) s,course c,student d
WHERE s.cno=c.cno AND s.sno=d.sno AND r1<4
ORDER BY c.cno,r1;
--22、查询每门课程被选修的学生数
SELECT s.sno,COUNT(DISTINCT s.sno) 人数
FROM sc s
GROUP BY s.sno;
--23、查询出只选修了一门课程的全部学生的学号和姓名
SELECT d.sno,MAX(d.sname)
FROM sc s,student d
WHERE d.sno=s.sno
GROUP BY d.sno
HAVING COUNT(DISTINCT s.cno)=1;
--24、查询男生、女生人数
SELECT SUM(CASE d.ssex WHEN ‘男‘ THEN 1 ELSE 0 END) 男生,
SUM(CASE d.ssex WHEN ‘女‘ THEN 1 ELSE 0 END) 女
FROM student d;
--25、查询姓“张”的学生名单
SELECT * FROM student WHERE sname LIKE ‘张%‘;
--26、查询同名同性学生名单,并统计同名人数
SELECT sname 姓名,ssex 性别,COUNT(1) 人数
FROM student
GROUP BY sname,ssex
HAVING COUNT(1)>1;
--27、1981 年出生的学生名单(注:Student 表中Sage 列的类型是number)
SELECT * FROM student
WHERE to_char(sage,‘yyyy‘)=‘1981‘;
--28、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
SELECT cno,ROUND(AVG(score),2) 平均分
FROM sc
GROUP BY cno
ORDER BY AVG(score),cno DESC;
--29、查询平均成绩大于85 的所有学生的学号、姓名和平均成绩
SELECT s.sno,MAX(d.sname),AVG(s.score)
FROM sc s,student d
WHERE s.sno=d.sno
GROUP BY s.sno
HAVING AVG(s.score)>85;
--30、查询课程名称为“数据库”,且分数低于60 的学生姓名和分数
SELECT d.sname,s.score
FROM student d,sc s,course c
WHERE d.sno=s.sno AND s.cno=c.cno
AND c.cname=‘数据库‘ AND s.score<60;
--31、查询所有学生的选课情况;
SELECT d.sno 学号,MAX(d.sname) 姓名,COUNT(DISTINCT s.cno) 选课数
FROM student d,sc s
WHERE d.sno=s.sno
GROUP BY d.sno;
--32、查询任何一门课程成绩在70 分以上的姓名、课程名称和分数;
SELECT MAX(d.sname) 姓名,MAX(c.cname) 课程名称,s.score 分数
FROM student d,sc s,course c
WHERE d.sno=s.sno AND s.cno=c.cno
GROUP BY s.sno,s.score
HAVING COUNT(1)=(
SELECT COUNT(cno) FROM sc WHERE sno=s.sno);
--33、查询不及格的课程,并按课程号从大到小排列
SELECT s.sno,c.cname,s.score
FROM sc s,course c
WHERE s.cno=c.cno AND s.score<60
ORDER BY s.cno DESC;
--34、查询课程编号为c001 且课程成绩在80 分以上的学生的学号和姓名;
SELECT d.sno,d.sname
FROM student d,sc s
WHERE d.sno=s.sno
AND s.cno=‘c003‘ AND s.score>80;
--35、求选了课程的学生人数
SELECT COUNT(DISTINCT sno) 人数
FROM sc;
--36、查询选修“谌燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩
SELECT sname,score
FROM (SELECT d.sname,s.score,row_number()
OVER(ORDER BY s.score DESC) r1
FROM student d,sc s,course c,teacher t
WHERE d.sno=s.sno AND s.cno=c.cno AND c.tno=t.tno AND t.tname=‘谌燕‘)
WHERE r1=1;
--37、查询各个课程及相应的选修人数
SELECT cno 课程编号,COUNT(DISTINCT sno) 选修人数
FROM sc
GROUP BY cno;
--38、查询不同课程成绩相同的学生的学号、课程号、学生成绩
SELECT s.sno,s.cno,s.score FROM sc s,sc s1
WHERE s.cno<>s1.cno
AND s.score=s1.score AND s.sno=s1.sno;
--39、查询每门功课成绩最好的前两名
SELECT d.sno,d.sname,s.cno,s.score,s.r1
FROM(SELECT s1.sno,s1.cno,s1.score,row_number()
OVER(PARTITION BY s1.cno ORDER BY s1.score DESC) r1 FROM sc s1) s,student d
WHERE s.sno=d.sno AND r1<3
ORDER BY s.cno,s.r1;
--40、统计每门课程的学生选修人数(超过10 人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT s.cno,COUNT(DISTINCT s.sno) 选修人数
FROM course c,sc s
WHERE c.cno=s.cno
GROUP BY s.cno
HAVING COUNT(DISTINCT s.sno)>10
ORDER BY COUNT(DISTINCT s.sno) DESC,s.cno;

以上是关于select 训练的主要内容,如果未能解决你的问题,请参考以下文章

vue学习笔记の实现select组件

oracle训练

云原生训练营模块二 Go语言进阶

云原生训练营模块二 Go语言进阶

select * 和select 1,select count(*)和select count的区别

怎么清除select已选择的内容