sql 经典练习 ()

--1. 查询01 课程比02 课程 成绩高的学生以及课程分数.

 

--查询课程1,和分数,

select * from SC sc
where sc.CId='01'
---查询课程2 和分数 ,
select * from SC sc2
where sc2.CId='02'


select * from Student RIGHT JOIN (

select t1.SId,t1.class1,t2.class2 from

(select sc.SId,sc.score as class1 from SC sc where sc.CId='01' )as t1,
(select sc2.SId,sc2.score as class2 from SC sc2 where sc2.CId='02') as t2


where t1.SId=t2.SId and t1.class1>t2.class2
) r

on Student.SId = r.SId;


---2.平均分 高于60分的同学的情况

select s.Sname,s.SId ,k.平均分 from Student s
inner join

(select sc.SId,AVG(sc.score) as 平均分
from SC sc
group by sc.SId
)k

on s.SId=k.SId

--3. 成绩表中,存在成绩的学生的信息

select s.* from student s inner join
(select distinct sc.SId from SC sc
where sc.score is not null ) k
on s.SId=k.SId

 

---4. 所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩


select s.SId,s.Sname,k.countCorse,k.scoreSum from Student s
inner join
(
select sc.SId,COUNT(sc.CId) as countCorse,SUM(sc.score)scoreSum from SC sc
group by sc.SId
) k
on s.SId=k.SId


---4-1. 有成绩的学生信息
select * from Student s
where s.SId in(select SC.SId from SC )


select * from Student s
where exists (select * from SC sc where sc.SId=s.SId )


---5.查询姓李的老师的数目
select count(1) from Teacher t
where t.Tname like '李%'
group by t.TId


--6. 听过张三老师课的学生的信息
--(1)张三老师教过的课
select c.CId from Course c,Teacher t
where c.TId=t.TId and t.Tname='张三'

--(2)听过这门课的学生的信息
select * from Student s, SC sc
where sc.CId in(
select c.CId from Course c,Teacher t
where c.TId=t.TId and t.Tname='张三'

)
and s.SId=sc.SId


---连接查询方法
select s.* from Student s,SC sc,Course c,Teacher t
where c.TId=t.TId and
c.CId=sc.CId and
sc.SId=s.SId
and t.Tname='张三'


--7 没有学完 全部课程的人的信息 :

--先找出 学完全部课程的人的姓名。然后not in


select * from Student s
where s.SId not in(

select sc.SId from SC sc
group by sc.SId
having count(sc.CId)=( select count(1) from Course)


--8. 求出和01同学 至少有一门课 都是一起学过的人
---先求 01 同学的课程
--然后求 修改这些课程的学生的id

--然后连表查询


--求s1的同学学过的课.


select s.* from Student s

inner join
(
select distinct sc2.SId from SC sc2 where sc2.CId in(
select sc.CId from SC sc where sc.SId='01')
) k
on s.SId=k.SId


---11. 查询两门课低于 60分的学生的信息,和平均分


---12 .01课程 小于60分的人, 降序排列


--先查出 01 小于60分的 sid

select s.* from Student s ,SC sc
where sc.CId='01'and
sc.score<60
and s.SId=sc.SId
order by sc.score desc


---13.按照平均成绩的高到低,求出 学生信息,平均成绩,总成绩

select s.Sname,k.theAvg,k.theSum from Student s
inner join
(select sc.SId,AVG(sc.score) as theAvg ,SUM(sc.score) theSum from SC sc
group by sc.SId)k

on s.SId=k.SId
order by k.theAvg desc


---19,查询每门课程 选修的学生数
select cid, count(sc.SId)
from SC sc
group by sc.CId


---20.只修2门课的学生名字,和学生id
select s.* from Student s
inner join

(select sc.SId theid from SC sc
group by sc.SId
having count(sc.CId) =2 ) k

on s.SId=k.theid


--21查询男生人数,女生人数
select COUNT(s.Ssex),s.Ssex from Student s
group by s.Ssex


---22.查询含有风字的学生信息
select * from Student s
where s.Sname like '%风%'

 

--23 查询同名的学生 的所有信息

select * from Student s
where s.Sname in

(
select s.Sname from Student s
group by s.Sname
having count( s.Sname)>1
)


---24 1900年出生的人
select * from Student s
where YEAR( s.Sage)=1990;

 

---26 平局分大于85的 人的姓名 ,平均分
select s.Sname, s.SId,k.avgScore from Student s
inner join
(
select sc.SId theid,AVG(sc.score) avgScore from SC sc
group by sc.SId
) k
on s.SId=k.theid


---27 数学低于60的名字,和分数

--方法1:
select * from Student s
where s.SId in
(
select sc.SId from Course c,SC sc
where c.Cname='数学' and sc.CId=c.CId and sc.score<60
)


--方法2:
select s.* from Student s,Course c,SC sc
where s.SId=sc.SId and
sc.CId=c.CId and
c.Cname='数学' and
sc.score<60


--28 所有学生,所有课程的 分数。 允许没成绩
select s.Sname,sc.CId,sc.score from Student s
inner join SC sc on s.SId=sc.SId

--29 有一门课 是70分以上的学生的 姓名、课程名、分数

--(1)先找出70分以上的,sid,课程名,分数
select sc.SId, c.Cname,sc.score
from SC sc,Course c
where sc.CId=c.CId and sc.score>70

---综合
select s.Sname,c.Cname,sc.score from Student s ,SC sc,Course c
where sc.CId=c.CId and sc.score>70 and s.SId=sc.SId


--30 存在不合格成绩的课程
select sc.CId,count(*)
from SC sc
where sc.score<60
group by sc.CId


--31. 01课程在 80分以上的 学生名字,学号


select s.* from SC sc,Student s
where sc.score>78 and sc.CId='01'and
s.SId=sc.SId

 

---32 每门课的学生数
select sc.CId, count(sc.SId)
from SC sc
group by sc.CId


---37 每门课的选修人数,大于5才统计
select sc.CId,count(sc.SId)
from SC sc
group by sc.CId


---38.选修了2门课的学生
select sc.SId ,count(sc.CId) from SC sc
group by sc.SId
having count(sc.CId)>2


---39 选修了全部课程的 学生名称

select * from Student s
where s.SId in (

select sc.SId
from SC sc
group by sc.SId

having count(sc.CId)=(select count(*) from Course)
)

 

 

参考https://www.jianshu.com/p/476b52ee4f1b

 

转载于:https://www.cnblogs.com/bingyizhihun/p/11520829.html

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值