首页 > 其他分享 >习题练习

习题练习

时间:2023-02-28 17:12:41浏览次数:31  
标签:01 cid 练习 score sid sc 习题 select

是网上早的题目,答案大部分是我自己写的,部分是别人的答案

insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
1.学生表
Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
2.课程表
Course(CID,Cname,TID) --CID --课程编号,Cname 课程名称,TID 教师编号
3.教师表
Teacher(TID,Tname) --TID 教师编号,Tname 教师姓名
4.成绩表
SC(SID,CID,score) --SID 学生编号,CID 课程编号,score 分数

 

查询选修了全部课程的学生信息
(某学生不存在一门课没有选修)

select a.* 
from student a,sc b where a.sid=b.sid group by a.sid,a.sname,a.sage,a.ssex having count(*)=(select count(*) from course)

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

select sid from sc 
group by sid
having count(*)>=2

 

统计每门课程的学生选修人数(超过5人的课程才统计)。\
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 

select cid,count(*)
from sc 
group by cid
having count(*)>5
order by count(*) desc,cid asc

查询每门功成绩最好的前两名

select * from (select sid,cid,rank() over(partition by cid order by score desc) r from sc 
)where r<3

查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

select distinct a.* from sc a, sc b
where a.sid=b.sid
and a.cid<>b.cid
and a.score=b.score

 

查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩

select a.*,b.score from student a,sc b,course c,teacher d
where a.sid=b.sid
and b.cid=c.cid
and c.tid=d.tid
and d.tname='张三'
and score=(select max(e.score) from sc e where e.cid=b.cid)

 


求每门课程的学生人数

select b.cname,count(*) from sc a,course b
where a.cid=b.cid
group by b.cname

 


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

select *
--a.sid,a.sname
from student a,sc b
where a.sid=b.sid 
and b.cid='01' 
and b.score>80

 

 

查询不及格的课程

select a.sname,c.cname,b.score from student a,sc b,course c
where a.sid=b.sid 
and b.cid = c.cid
and b.score<60

 

查询任何一门课程成绩在70分以上的姓名、课程名称和分数

select a.sname,c.cname,b.score from student a,sc b,course c
where a.sid=b.sid 
and b.cid = c.cid
and b.score>70

 

查询所有学生的课程及分数情况

select a.sname,c.cname,b.score from student a,sc b,course c
where a.sid=b.sid
and b.cid=c.cid

 

查询课程名称为"数学",且分数低于60的学生姓名和分数

select a.sname,b.score from student a,sc b,course c
where a.sid=b.sid
and b.cid=c.cid
and c.cname='数学'
and b.score<60

 

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

SELECT A.SID,A.SNAME,ROUND(AVG(SCORE),2) R FROM STUDENT A,SC B
WHERE A.SID=B.SID
GROUP BY A.SID,A.SNAME
HAVING AVG(SCORE)>=85
ORDER BY R DESC

 

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

select cid,round(avg(score),2) R from sc
group by cid
ORDER BY R DESC,CID ASC

 


查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)

select * from student 
where substr(sage,1,4)='1990'

 

查询同名同性学生名单,并统计同名人数

select sname,ssex,count(*) from Student
group by sname,ssex
having count(*)>1

 

 

查询名字中含有"风"字的学生信息

select * from student 
where sname like '%风%'

 

查询男生、女生人数

select sum(case when ssex='男' then 1 else 0 end),sum(case when ssex='女' then 1 else 0 end)
from Student

 

查询出只有两门课程的全部学生的学号和姓名

select a.sid,a.sname from student a,sc b
where a.sid=b.sid
group by a.sid,a.sname
having count(*)=2

 

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

select cid,count(*) from sc 
group by cid

 

查询各科成绩前三名的记录

select * from (
select cid,score,
rank() 
over(partition by cid order by score desc) r 
from sc)
where r<4

 

 

查询学生平均成绩及其名次

select sid,round(avg(score),2),dense_rank() over( order by round(avg(score),2) desc)from SC
group by sid

 


统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60 及所占百分比 to_char(,'FM9999999999999999.00') as amount

SELECT a.cid,b.cname,
sum(case when a.score>=85 and a.score<100 then 1 else 0 end ) as "100-85",to_char(sum(case when a.score>=85 and a.score<100 then 1 else 0 end )/count(*),'FM9999999999999999.00'),
sum(case when a.score>=70 and a.score<85 then 1 else 0 end ) as "85-70",to_char(sum(case when a.score>=70 and a.score<85 then 1 else 0 end )/count(*),'FM9999999999999999.00'),
sum(case when a.score>=60 and a.score<70 then 1 else 0 end ) as "70-60",to_char(sum(case when a.score>=60 and a.score<70 then 1 else 0 end )/count(*),'FM9999999999999999.00'),
sum(case when a.score>=0 and a.score<60 then 1 else 0 end ) as "0-60",to_char(sum(case when a.score>=0 and a.score<60 then 1 else 0 end )/count(*),'FM9999999999999999.00') from sc a,course b
where a.cid=b.cid
group by a.cid,b.cname

 


查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

select a.*,b.cid,b.score 
from student a,(select cid,sid,score,dense_rank() over(partition by cid order by score DESC) R
from SC) b
where a.sid=b.sid
and R>=2 
AND R<=3

 

查询不同老师所教不同课程平均分从高到低显示

select tname,a.cid,avg(score) avge
from teacher c,course a,sc b
where a.cid=b.cid
and c.tid=a.tid
group by tname,a.cid
order by avge desc

 

 

查询学生的总成绩并进行排名

select sid,sum(score) as r from sc 
group by sid 
order by r desc

 

按各科成绩进行排序,并显示排名

select cid,sid,score,
dense_rank() over(partition by cid order by score) 
from sc
order by cid,score

 


查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,
最高分,最低分,平均分,及格率,中等率,优良率,优秀率
--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

select A.cid,A.cname,max(b.score),min(b.score),avg(b.score),
sum(case when b.score>=60 then 1 else 0 end )/count(*) ,
sum(case when b.score>=70 and b.score<80 then 1 else 0 end)/count(*),
sum(case when b.score>=80 and b.score<90 then 1 else 0 end)/count(*),
sum(case when b.score>=90 then 1 else 0 end)/count(*)
from course a,sc b 
where a.cid=b.cid
GROUP BY a.cid,a.cname;

按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

select a.SID  学生编号,a.Sname 学生姓名,
max(case c.Cname when '语文' then b.score else null end) 语文,
max(case c.Cname when '数学' then b.score else null end) 数学,
max(case c.Cname when '英语' then b.score else null end) 英语,
cast(avg(b.score) as decimal(18,2)) 平均分
from Student a
left join SC b on a.SID = b.SID
left join Course c on b.CID = c.CID
group by a.SID , a.Sname
order by 平均分

 

检索"01"课程分数小于60,按分数降序排列的学生信息

select a.*,b.score from student a,sc b
where b.cid='01'
and score<60
order by score desc

 

查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

select a.sid,a.sname,avg(b.score) from student a,sc b
where exists 
(select c.sid,count(*) from sc c where a.sid=c.sid
group by c.sid) 
group by a.sid,a.sname

 


查询至少有一门课与学号为"01"的同学所学相同的同学的信息
(没有一门课与学号01同学所学相同的同学信息)

select distinct a.* from student a,sc b 
where a.sid=b.sid
and a.sid<>'01' 
and exists (
select * from SC c 
where c.sid='01' 
and b.cid=c.cid )

 

查询没有学全所有课程的同学的信息

select sid,count(cid) from sc
group by sid
having count(cid)<(select count(cid) from course)

 

查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

select distinct a.* from student a,sc b,sc c
where a.sid=b.sid
and b.cid='01'
and not exists (select * from sc c where a.sid=c.sid and c.cid='02')

 


查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

select distinct a.* from student a,sc b,sc c
where a.sid=b.sid
and a.sid=c.sid
and b.cid='01'
and c.cid='02'

 


***查询没学过"张三"老师授课的同学的信息***可以尝试使用exist去写

select e.*
from student e
where e.sid not in (select distinct a.sid
from student a,sc b,course c,teacher d
where a.sid=b.sid
and b.cid=c.cid
and c.tid=d.tid
and tname='张三' )

 


查询学过"张三"老师授课的同学的信息

select distinct a.sid,a.sname,a.sage,a.ssex
from student a,sc b,course c,teacher d
where a.sid=b.sid
and b.cid=c.cid
and c.tid=d.tid
and tname='张三'

查询"01"课程比"02"课程成绩高的学生的信息及课程分数

 

select a.sid,a.sname,a.sage,a.ssex,b.score "01分数",c.score "02分数"
from student a,sc b,sc c
where b.cid='01'
and c.cid='02'
and a.sid=b.sid
and a.sid=c.sid
and b.score>c.score

 

 

查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

select a.sid,a.sname,avg(b.score) "平均成绩"
from student a,sc b
where a.sid=b.sid
group by a.sid,a.sname
having avg(b.score)>=60

 


查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

select a.sid,a.sname,count(a.sid),sum(b.score)
from student a,sc b
where a.sid=b.sid
group by a.sid,a.sname

 

查询"李"姓老师的数量

select count(*) from Teacher
where Tname like '李%'

 

标签:01,cid,练习,score,sid,sc,习题,select
From: https://www.cnblogs.com/chenbao1012/p/17165149.html

相关文章

  • 江南信息学2023年第一周练习20230223 题解
    比赛链接1001:鸡尾酒疗法1#include<bits/stdc++.h>2usingnamespacestd;3intmain()4{5intn;6cin>>n;7doublea,b;8cin>>a......
  • L1-011 A-B【团体程序设计天梯赛-练习集】
    L1-011A-B题要求你计算A−B。不过麻烦的是,A和B都是字符串——即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A−B。输入格式:输入在2行中先后......
  • L1-002 打印沙漏【团体程序设计天梯赛-练习集】
    L1-002打印沙漏本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印\[*****\]\[***\]\[*\]\[***\]\[*****\]所谓“沙漏形......
  • 课堂练习01:计算最长英语链
    课堂练习01题目:计算最长英语单词链。一、题目内容:大家经常玩成语接龙游戏,我们试一试英语的接龙吧:一个文本文件中有N个不同的英语单词,我们能否写一个程序,快速找出最长......
  • JDBC练习,查询所有
    查询所有数据获取Connection定义SQL:selection*fromtb_brand;获取PerparedStatement对象设置参数:不需要执行SQL处理结果:List<Brand>释放资源没有系统学过这些,只......
  • 英文单词首尾相连的练习
    题目描述题目思路我主要是将本次练习的思路分成了三块:1、先将各个单词从文件中读取出来,存储在数组中;2、分别将各个单词的首尾字母各自存储在一个数组中,供于后续的首尾......
  • 课堂练习01题目:计算最长英语单词链。
     一、题目内容:大家经常玩成语接龙游戏,我们试一试英语的接龙吧:一个文本文件中有N个不同的英语单词,我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最......
  • Java基础——(综合练习)普通加密
    packagecom.zhao.test;publicclassTest18{/*需求:​某系统的数字密码(大于0),比如1983,采用加密方式进行传输。规则如下:......
  • Java基础——(综合练习)选手打分
    packagecom.zhao.test;importjava.util.Scanner;publicclassTest17{/*需求:在唱歌比赛中,有6名评委给选手打分,分数范围是[0-100]之间的整数。选......
  • 02_18_Java语音进阶||day18_Java基础小节练习(17-20部分)数据类型转换&运算符&方法入门
    第一部分数据类型转换&运算符&方法入门第一题编写步骤:定义类Test1定义main方法定义两个byte类型变量b1,b2,并分别赋值为10和20.定义变量b3,保存b1和b2的和,并输出.定义两......