-- 一、创建教学系统的数据库,表,以及数据
--student(sno,sname,sage,ssex) 学生表
--course(cno,cname,tno) 课程表
--sc(sno,cno,score) 成绩表
--teacher(tno,tname) 教师表
--1.创建数据库test1
use master
GO
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = 'test1')
DROP DATABASE test1
GO
CREATE DATABASE test1
GO
use test1
GO
--2.创建教师表
CREATE TABLE [dbo].[teacher](
[tno] [int] NOT NULL PRIMARY KEY,
[tname] [nvarchar](20) NOT NULL
)
--插入数据
INSERT INTO teacher(tno,tname)VALUES(1,'张老师')
INSERT INTO teacher(tno,tname)VALUES(2,'王老师')
INSERT INTO teacher(tno,tname)VALUES(3,'李老师')
INSERT INTO teacher(tno,tname)VALUES(4,'赵老师')
INSERT INTO teacher(tno,tname)VALUES(5,'刘老师')
INSERT INTO teacher(tno,tname)VALUES(6,'向老师')
INSERT INTO teacher(tno,tname)VALUES(7,'李文静')
INSERT INTO teacher(tno,tname)VALUES(8,'叶平')
--3.创建学员表
CREATE TABLE [dbo].[student](
[sno] [int] NOT NULL PRIMARY KEY,
[sname] [nvarchar](20) NOT NULL,
[sage] [datetime] NOT NULL,
[ssex] [char](2) NOT NULL
)
--插入数据
INSERT INTO student(sno,sname,sage,ssex) VALUES(1,'张三','1980-1-23','男')
INSERT INTO student(sno,sname,sage,ssex) VALUES(2,'李四','1982-12-12','男')
INSERT INTO student(sno,sname,sage,ssex) VALUES(3,'张飒','1981-9-9','男')
INSERT INTO student(sno,sname,sage,ssex) VALUES(4,'莉莉','1983-3-23','女')
INSERT INTO student(sno,sname,sage,ssex) VALUES(5,'王弼','1982-6-21','男')
INSERT INTO student(sno,sname,sage,ssex) VALUES(6,'王丽','1984-10-10','女')
INSERT INTO student(sno,sname,sage,ssex) VALUES(7,'刘香','1980-12-22','女')
--4.创建课程表
CREATE TABLE [dbo].[course](
[cno] [int] NOT NULL PRIMARY KEY,
[cname] [nvarchar](20) NOT NULL,
[tno] [int] NOT NULL
)
--创建外键
ALTER TABLE [dbo].[course] WITH CHECK ADD
CONSTRAINT [FK_course_teacher] FOREIGN KEY([tno])
REFERENCES [dbo].[teacher] ([tno])
--插入数据
insert into course(cno,cname,tno) values(1,'企业管理',3)
insert into course(cno,cname,tno) values(2,'马思',1)
insert into course(cno,cname,tno) values(3,'UML',2)
insert into course(cno,cname,tno) values(4,'数据库',5)
insert into course(cno,cname,tno) values(5,'物理',8)
--5.创建成绩表
CREATE TABLE [dbo].[sc](
[sno] [int] NOT NULL,
[cno] [int] NOT NULL,
[score] [int] NOT NULL
)
--创建外键
ALTER TABLE [dbo].[sc] WITH CHECK ADD CONSTRAINT [FK_sc_course] FOREIGN KEY([cno])
REFERENCES [dbo].[course] ([cno])
ALTER TABLE [dbo].[sc] WITH CHECK ADD CONSTRAINT [FK_sc_student] FOREIGN KEY([sno])
REFERENCES [dbo].[student] ([sno])
--插入数据
INSERT INTO sc(sno,cno,score)VALUES(1,1,80)
INSERT INTO sc(sno,cno,score)VALUES(1,2,86)
INSERT INTO sc(sno,cno,score)VALUES(1,3,83)
INSERT INTO sc(sno,cno,score)VALUES(1,4,89)
INSERT INTO sc(sno,cno,score)VALUES(2,1,50)
INSERT INTO sc(sno,cno,score)VALUES(2,2,36)
--INSERT INTO sc(sno,cno,score)VALUES(2,3,43)
INSERT INTO sc(sno,cno,score)VALUES(2,4,59)
INSERT INTO sc(sno,cno,score)VALUES(3,1,50)
INSERT INTO sc(sno,cno,score)VALUES(3,2,96)
--INSERT INTO sc(sno,cno,score)VALUES(3,3,73)
INSERT INTO sc(sno,cno,score)VALUES(3,4,69)
INSERT INTO sc(sno,cno,score)VALUES(4,1,90)
INSERT INTO sc(sno,cno,score)VALUES(4,2,36)
INSERT INTO sc(sno,cno,score)VALUES(4,3,88)
--INSERT INTO sc(sno,cno,score)VALUES(4,4,99)
INSERT INTO sc(sno,cno,score)VALUES(5,1,90)
INSERT INTO sc(sno,cno,score)VALUES(5,2,96)
INSERT INTO sc(sno,cno,score)VALUES(5,3,98)
INSERT INTO sc(sno,cno,score)VALUES(5,4,99)
INSERT INTO sc(sno,cno,score)VALUES(6,1,70)
INSERT INTO sc(sno,cno,score)VALUES(6,2,66)
INSERT INTO sc(sno,cno,score)VALUES(6,3,58)
INSERT INTO sc(sno,cno,score)VALUES(6,4,79)
INSERT INTO sc(sno,cno,score)VALUES(7,1,80)
INSERT INTO sc(sno,cno,score)VALUES(7,2,76)
INSERT INTO sc(sno,cno,score)VALUES(7,3,68)
INSERT INTO sc(sno,cno,score)VALUES(7,4,59)
INSERT INTO sc(sno,cno,score)VALUES(7,5,89)
--1、查询课程1的成绩 比 课程2的成绩 高 的所有学生的学号.
select a.sno from
(select sno,score from sc where cno=1) a,
(select sno,score from sc where cno=2) b
where a.score>b.score and a.sno=b.sno
--2、查询平均成绩大于60分的同学的学号和平均成绩;
select sno,avg(score) as sscore from sc group by sno having avg(score) >60
--select a.sno as "学号", avg(a.score) as "平均成绩"
--from
--(select sno,score from sc) a
--group by sno having avg(a.score)>60
但是写成这样就会出错:语法没错
--select a.sno as "学号", avg(a.score) as "平均成绩"
--from
--(select sno,score from sc) a
--where avg(a.score)>60
--3、查询所有同学的学号、姓名、选课数、总成绩
select a.sno as 学号, b.sname as 姓名,
count(a.cno) as 选课数, sum(a.score) as 总成绩
from sc a, student b
where a.sno = b.sno
group by a.sno, b.sname
go
--3、查询所有同学的学号、姓名、选课数、总成绩
select student.sno as 学号, student.sname as 姓名,
count(sc.cno) as 选课数, sum(score) as 总成绩
from student left Outer join sc on student.sno = sc.sno
group by student.sno, sname
--4、查询姓“李”的老师的个数;
select count(distinct(tname)) from teacher where tname like '李%‘
select tname as "姓名", count(distinct(tname)) as "人数"
from teacher
where tname like'李%'
group by tname
go
--5、查询没学过“叶平”老师课的同学的学号、姓名;
select student.sno,student.sname from student
where sno not in (select distinct(sc.sno) from sc,course,teacher
where sc.cno=course.cno and teacher.tno=course.tno and teacher.tname='叶平')
--6、查询同时学过课程1和课程2的同学的学号、姓名
select sno, sname from student
where sno in (select sno from sc where sc.cno = 1)
and sno in (select sno from sc where sc.cno = 2)
go
select c.sno, c.sname from
(select sno from sc where sc.cno = 1) a,
(select sno from sc where sc.cno = 2) b,
student c
where a.sno = b.sno and a.sno = c.sno
go
select student.sno,student.sname from student,sc where student.sno=sc.sno and sc.cno=1
and exists( Select * from sc as sc_2 where sc_2.sno=sc.sno and sc_2.cno=2)
go
--7、查询学过“叶平”老师所教所有课程的所有同学的学号、姓名
select a.sno, a.sname from student a, sc b
where a.sno = b.sno and b.cno in
(select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '叶平')
select a.sno, a.sname from student a, sc b,
(select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '叶平') e
where a.sno = b.sno and b.cno = e.cno
--8、查询 课程编号1的成绩 比 课程编号2的成绩 高的所有同学的学号、姓名
select a.sno, a.sname from student a,
(select sno, score from sc where cno = 1) b,
(select sno, score from sc where cno = 2) c
where b.score > c.score and b.sno = c.sno and a.sno = b.sno
--9、查询所有课程成绩小于60分的同学的学号、姓名
select sno,sname from student
where sno not in (select distinct sno from sc where score > 60)
--10、查询所有课程成绩大于60分的同学的学号、姓名
select sno,sname from student
where sno not in (select distinct sno from sc where score < 60)
--11、查询没有学全所有课的同学的学号、姓名
select student.sno, student.sname
from student, sc
where student.sno = sc.sno
group by student.sno, student.sname
having count(sc.cno) < (select count(cno) from course)
方法二:
select r2.rsno
from
(select COUNT(course.cno)as num1 from course) r1,
(select COUNT(sc.sno)as num2, sc.sno as rsno from sc group by sc.sno ) r2
where r1.num1>r2.num2
--12、查询至少有一门课程 与 学号为1的同学所学课程 相同的同学的学号和姓名
select distinct a.sno, a.sname
from student a, sc b
where a.sno <> 1 and a.sno=b.sno and
b.cno in (select cno from sc where sno = 1)
方法二:
--select s.sno,s.sname
--from student s,
--(select sc.sno
--from sc
--where sc.cno in (select sc1.cno from sc sc1 where sc1.sno=1)and sc.sno<>1
--group by sc.sno)r1
--where r1.sno=s.sno
--13、把“sc”表中“刘老师”所教课的成绩都更改为此课程的平均成绩
update sc set score = (select avg(sc_2.score) from sc sc_2 where sc_2.cno=sc.cno)
from course,teacher where course.cno=sc.cno and course.tno=teacher.tno and teacher.tname='叶平'
--14、查询和2号同学学习的课程完全相同的其他同学学号和姓名
/* --Do first :
select sno
from sc
where sno <> 2
group by sno
having sum(cno) = (select sum(cno) from sc where sno = 2)
*/
select b.sno, b.sname
from sc a, student b
where b.sno <> 2 and a.sno = b.sno
group by b.sno, b.sname
having sum(cno) = (select sum(cno) from sc where sno = 2)
--15、删除学习“叶平”老师课的sc表记录
delete sc from course, teacher
where course.cno = sc.cno and course.tno = teacher.tno and tname = '叶平'
--16、向sc表中插入一些记录,这些记录要求符合以下条件:
--将没有课程3成绩同学的该成绩补齐, 其成绩取所有学生的课程2的平均成绩
INSERT sc select sno, 3, (select avg(score) from sc where cno = 2)
from student
where sno not in (select sno from sc where cno = 3)
--17、按平平均分从高到低显示所有学生的如下统计报表:
-- 学号,企业管理,马思,UML,数据库,物理,课程数,平均分
SELECT sno as 学号
,max(case when cno = 1 then score end) AS 企业管理
,max(case when cno = 2 then score end) AS 马思
,max(case when cno = 3 then score end) AS UML
,max(case when cno = 4 then score end) AS 数据库
,max(case when cno = 5 then score end) AS 物理
,count(cno) AS 课程数
,avg(score) AS 平均分
FROM sc
GROUP by sno
ORDER by avg(score) DESC
--18、查询各科成绩最高分和最低分:以如下形式显示:课程号,最高分,最低分
select cno as 课程号, max(score) as 最高分, min(score) 最低分
from sc group by cno
select course.cno as '课程号'
,MAX(score) as '最高分'
,MIN(score) as '最低分'
from sc,course
where sc.cno=course.cno
group by course.cno
--19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
SELECT t.cno AS 课程号,
max(course.cname)AS 课程名,
isnull(AVG(score),0) AS 平均成绩,
100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/count(1) AS 及格率
FROM sc t, course
where t.cno = course.cno
GROUP BY t.cno
ORDER BY 及格率 desc
--20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马思(002),UML (003),数据库(004)
select
avg(case when cno = 1 then score end) as 平均分1,
avg(case when cno = 2 then score end) as 平均分2,
avg(case when cno = 3 then score end) as 平均分3,
avg(case when cno = 4 then score end) as 平均分4,
100 * sum(case when cno = 1 and score > 60 then 1 else 0 end) / sum(case when cno = 1 then 1 else 0 end) as 及格率1,
100 * sum(case when cno = 2 and score > 60 then 1 else 0 end) / sum(case when cno = 2 then 1 else 0 end) as 及格率2,
100 * sum(case when cno = 3 and score > 60 then 1 else 0 end) / sum(case when cno = 3 then 1 else 0 end) as 及格率3,
100 * sum(case when cno = 4 and score > 60 then 1 else 0 end) / sum(case when cno = 4 then 1 else 0 end) as 及格率4
from sc
--21、查询不同老师所教不同课程平均分, 从高到低显示
-- 张老师 数据库 88
select max(c.tname) as 教师, max(b.cname) 课程, avg(a.score) 平均分
from sc a, course b, teacher c
where a.cno = b.cno and b.tno = c.tno
group by a.cno
order by 平均分 desc
方法二:
select r.tname as '教师',r.rname as '课程' , AVG(score) as '平均分'
from sc,
(select
t.tname,c.cno as rcso,c.cname as rname
from teacher t ,course c
where t.tno=c.tno
)r
where sc.cno=r.rcso
group by sc.cno,r.tname,r.rname
order by AVG(score) desc
--22、查询如下课程成绩均在第3名到第6名之间的学生的成绩:
-- [学生ID],[学生姓名],企业管理,马思,UML,数据库,平均成绩
select top 6 max(a.sno) 学号, max(b.sname) 姓名,
max(case when cno = 1 then score end) as 企业管理,
max(case when cno = 2 then score end) as 马思,
max(case when cno = 3 then score end) as UML,
max(case when cno = 4 then score end) as 数据库,
avg(score) as 平均分
from sc a, student b
where a.sno not in (select top 2 sno from sc where cno = 1 order by score desc)
and a.sno not in (select top 2 sno from sc where cno = 2 order by score desc)
and a.sno not in (select top 2 sno from sc where cno = 3 order by score desc)
and a.sno not in (select top 2 sno from sc where cno = 4 order by score desc)
and a.sno = b.sno
group by a.sno
--23、统计打印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
select sc.cno as 课程ID, cname as 课程名称,
sum(case when score >= 85 then 1 else 0 end) as [100-85],
sum(case when score < 85 and score >= 70 then 1 else 0 end) as [85-70],
sum(case when score < 70 and score >= 60 then 1 else 0 end) as [70-60],
sum(case when score < 60 then 1 else 0 end) as [ <60]
from sc, course
where sc.cno = course.cno
group by sc.cno, cname
--24、查询学生平均分及其名次
--drop table t1
--select sno, avg(score) as pjf into t1 from sc group by sno
--go
--
--drop table t2
--select distinct avg(score) as pjf into t2 from sc group by sno
--go
--
--select
-- (select count(1) from t2 where pjf >= t1.pjf) as 名次,
-- sno as 学号,
-- pjf as 平均分
--from t1
--order by pjf desc
--go
select
(select count(1)
from (select distinct avg(score) as pjf from sc group by sno) as t2
where pjf >= t1.pjf) as 名次,
sno as 学号,
pjf as 平均分
from (select sno, avg(score) as pjf from sc group by sno) as t1
order by pjf desc
go
--25、查询各科成绩前三名的记录:(不考虑成绩并列情况)
--drop table aa
--select sno, cno, score into aa from sc order by cno, score desc
--
--drop table bb
--select distinct cno, score into bb from sc order by cno, score desc
--
--select aa.* from aa
--where aa.score in (select top 3 score from bb where aa.cno = bb.cno)
select *
from (select top 9999 sno, cno, score from sc order by cno, score desc) as aa
where aa.score in
(select top 3 score
from (select distinct top 9999 cno, score from sc order by cno, score desc) as bb
where aa.cno = bb.cno)
--26、查询每门课程被选修的学生数
select cno,count(sno) from sc group by cno
select
sc.cno
,COUNT(sc.sno)' 学生数 '
from sc,course c
where sc.cno=c.cno
group by sc.cno
--27、查询出只选修了一门课程的全部学生的学号和姓名
SELECT sc.sno, student.sname, count(cno) AS 选课数
FROM sc, student
WHERE sc.sno = student.sno
GROUP BY sc.sno, student.sname
HAVING count(cno) = 3
--28、查询男生、女生人数
select
(select count(1) from student where ssex = '男') 男生人数,
(select count(1) from student where ssex = '女') 女生人数
select
(select COUNT(sno) where ssex='男') as '男生人数'
,(select COUNT(sno) where ssex='女') as '女生人数'
from student
group by student.ssex
--29、查询姓“张”的学生名单
SELECT sname FROM student WHERE sname like '张%'
--30、查询同名同性学生名单,并统计同名人数
select
s1.sname
,COUNT(s1.sname) as '人数'
from student s1,
(select
s.sname ,s.ssex
from student s
)r
where s1.sname=r.sname and s1.ssex=r.ssex
group by s1.sname
having COUNT(s1.sname)>1
go
select sname, count(1) from student group by sname having count(1) > 1
--31、1981年出生的学生名单(注:student表中sage列的类型是datetime)
select sname, CONVERT(char(4), DATEPART(year,sage)) as age
from student
where DATEPART(year,sage)=1981
--32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select cno 课程号, avg(score) 平均分
from sc group by cno order by 平均分 asc, cno desc
--33、查询平均成绩大于80的所有学生的学号、姓名和平均成绩
select
s1.sno,
s.sname,
AVG(s1.score) as '平均成绩'
from student s,sc s1
where s.sno=s1.sno
group by s1.sno,s.sname
having AVG(s1.score)>80
go
select sno, avg(score)
from sc
group by sno
having avg(score) > 80
--34、查询 数据库 分数 低于60的学生姓名和分数
select c.sname, a.score
from sc a, course b, student c
where a.cno = b.cno and a.sno = c.sno
and b.cname = '数据库' and score < 60
--35、查询所有学生的选课情况
SELECT sc.sno 学号,sname 姓名,cname 课程, sc.cno 课号
FROM sc,student,course
WHERE sc.sno=student.sno and sc.cno=course.cno
ORDER BY sc.sno
--36、查询成绩在70分以上的学生姓名、课程名称和分数
select
s.sname,
c.cname,
(s1.score) as '分数'
from student s,sc s1,course c
where s.sno=s1.sno and c.cno=s1.cno and s1.score>=70
--group by s1.sno,s.sname
go
SELECT student.sno,student.sname,sc.cno,sc.score
FROM student,Sc
WHERE sc.score>=70 AND sc.sno=student.sno;
--37、查询不及格的课程,并按课程号从大到小排列
select cno, score from sc where score < 60 order by cno
go
select
sc.cno
,c.cname
,sc.score
from sc ,course c
where sc.score<60 and c.cno=sc.cno
order by sc.cno desc
--38、查询课程编号为3且课程成绩在80分以上的学生的学号和姓名
select sc.sno,student.sname from sc,student where sc.sno=student.sno and score>80 and cno=3
go
select
s.sno,s.sname,s1.score
from student s,sc s1
where s1.sno=s.sno and s1.cno=3 and s1.score>=80
--39、求选了课程的学生人数
select count(distinct sno) from sc
--40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select student.sname,cname, score
from student,sc,course C,teacher
where student.sno=sc.sno and sc.cno=C.cno and C.tno=teacher.tno
and teacher.tname ='叶平'
and sc.score=(select max(score)from sc where cno = C.cno)
--41、查询各个课程及相应的选修人数
select cno 课程号, count(1) 选修人数 from sc group by cno
go
select
c.cname
,COUNT(sc.sno) '选修人数'
from course c,sc
where sc.cno=c.cno
group by c.cname
--42、查询不同课程成绩相同的学生的学号、课程号、学生成绩
select distinct A.sno, A.cno,B.score
from sc A ,sc B
where A.Score=B.Score and A.cno <>B.cno
order by B.score
go
select
sc.sno
,sc.cno
,sc.score
from sc,
(select
sc.sno
,sc.cno
,sc.score
from sc)r
where r.score=sc.score and r.cno<>sc.cno
--43、查询每门课程成绩最好的前两名的学生ID
--先按照 课程, 成绩 高低 对 sc表 排序
--select * from sc order by cno, score desc
select * from sc a
where score in (select top 2 score from sc where a.cno = sc.cno order by sc.score desc)
order by a.cno, a.score desc
--查询各单科状元
select * from sc a
where score = (select top 1 score from sc where a.cno = sc.cno order by sc.score desc)
order by a.cno, a.score desc
最终结果:
select top 2--这是最后的一行编码目的是只取用课程成绩降序排名后的前2行
r.sname as '前两名'
, MAX(r.grade) as '课程成绩'
from
(
select
s.sname
,s.sno
,max(score) as grade
from student s,sc
where sc.sno=s.sno
group by s.sname
,s.sno
)r
group by r.sname
order by 课程成绩 desc
--44、统计每门课程的学生选修人数(至少有2人选修的课程才统计)。要求输出课程号和选修人数,
--查询结果按人数降序排列,若人数相同,按课程号升序排列
select cno as 课程号,count(1) as 人数
from sc
group by cno having count(1) > 1
order by count(1) desc,cno
go
select
r.cno as 课程号
,r.num as '选修人数'
from
(select
sc.cno
,COUNT(sc.cno)as num
from sc
group by sc.cno
)r
where r.num>=2
--45、检索至少选修了5门课程的学生学号
select sno from sc group by sno having count(1) >= 5
go
select
r.sno
,r.sname
,COUNT(r.sno) as 至少选修了5门
from
(select
s.sno
,s.sname
,sc.cno
from sc,student s
where sc.sno=s.sno
)r
group by r.sno,r.sname
having COUNT(r.sno)>=5
--46、查询全部学生都选修的课程的课程号和课程名
--(思路:查询最受欢迎的课程是啥)
--select cno 课程ID, count(1) 选修人数 from sc group by cno
select course.cno, cname
from sc, course
where sc.cno = course.cno
group by course.cno, cname
having count(sc.cno) = (select count(1) from student)
go
select
c1.cno
,c1.cname
from course c1,
(
select
r.cno as cno
from
(select
sc.cno
,count(sc.cno) as num
from sc,course c
where sc.cno=c.cno
group by sc.cno
)r
where r.num=(select count(1) from student)
)rr
where rr.cno=c1.cno
--查询最受欢迎的课程
select cno 课程ID, count(cno) 选修人数
from sc group by cno
having count(cno) in (select top 1 count(cno) from sc group by cno order by count(cno) desc)
order by 选修人数 desc
--47、查询没学过“叶平”老师讲授的任一门课程的学生姓名
--思路: 先得到学过“叶平”老师讲授的所有课程清单
--select a.cno from course a, teacher b where a.tno = b.tno and b.tname = '叶平')
--然后: 从 sc表中 得到 学过上述课程的 所有学生清单
--select sno from sc where cno in
--(select a.cno from course a, teacher b where a.tno = b.tno and b.tname = '叶平'))
--最后: 从student表中 得到不在上数学生清单中的其他学生,即为最后的查询结果
select sno, sname from student
where sno not in(
select sno from sc where cno in
(select a.cno from course a, teacher b where a.tno = b.tno and b.tname = '叶平'))
select sno, sname from student
where sno not in
(select sno from course,teacher,sc
where course.tno=teacher.tno and sc.cno=course.cno and tname='叶平')
--48、查询两门以上不及格课程的同学的学号及其平均成绩
--思路: 先查询出所有不及格的sc中的记录
--select sno, score from sc where score < 60
select sno 学号, avg(score) 平均分, count(1) 不及格课程数
from sc where score < 60 group by sno having count(1) > 2
select
sno,avg(score) 平均分,COUNT(sno) as 不及格课程
from sc
where sc.score<60
group by sno
having COUNT(sno)>2
--49、检索4号课程分数大于60的同学学号,按分数降序排列
select sno, score from sc where cno = 4 and score > 60 order by score desc
--50、删除2号同学的课程1的成绩
--delete sc where sno = 2 and cno = 1
--select * from sc where sno = 2 and cno = 1
delete from sc where sno = 2 and cno = 1
作业:
--43.查询各单科状元
--46.查询最受欢迎的课程(选修学生最多的课程)
--xx.查询成绩最好的课程
--xx.查询最受欢迎的老师(选修学生最多的老师)
--xx.查询教学质量最好的老师
--xx.查询需要补考的各科学生清单
--student(sno,sname,sage,ssex) 学生表
--course(cno,cname,tno) 课程表
--sc(sno,cno,score) 成绩表
--teacher(tno,tname) 教师表
--1.创建数据库test1
use master
GO
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = 'test1')
DROP DATABASE test1
GO
CREATE DATABASE test1
GO
use test1
GO
--2.创建教师表
CREATE TABLE [dbo].[teacher](
[tno] [int] NOT NULL PRIMARY KEY,
[tname] [nvarchar](20) NOT NULL
)
--插入数据
INSERT INTO teacher(tno,tname)VALUES(1,'张老师')
INSERT INTO teacher(tno,tname)VALUES(2,'王老师')
INSERT INTO teacher(tno,tname)VALUES(3,'李老师')
INSERT INTO teacher(tno,tname)VALUES(4,'赵老师')
INSERT INTO teacher(tno,tname)VALUES(5,'刘老师')
INSERT INTO teacher(tno,tname)VALUES(6,'向老师')
INSERT INTO teacher(tno,tname)VALUES(7,'李文静')
INSERT INTO teacher(tno,tname)VALUES(8,'叶平')
--3.创建学员表
CREATE TABLE [dbo].[student](
[sno] [int] NOT NULL PRIMARY KEY,
[sname] [nvarchar](20) NOT NULL,
[sage] [datetime] NOT NULL,
[ssex] [char](2) NOT NULL
)
--插入数据
INSERT INTO student(sno,sname,sage,ssex) VALUES(1,'张三','1980-1-23','男')
INSERT INTO student(sno,sname,sage,ssex) VALUES(2,'李四','1982-12-12','男')
INSERT INTO student(sno,sname,sage,ssex) VALUES(3,'张飒','1981-9-9','男')
INSERT INTO student(sno,sname,sage,ssex) VALUES(4,'莉莉','1983-3-23','女')
INSERT INTO student(sno,sname,sage,ssex) VALUES(5,'王弼','1982-6-21','男')
INSERT INTO student(sno,sname,sage,ssex) VALUES(6,'王丽','1984-10-10','女')
INSERT INTO student(sno,sname,sage,ssex) VALUES(7,'刘香','1980-12-22','女')
--4.创建课程表
CREATE TABLE [dbo].[course](
[cno] [int] NOT NULL PRIMARY KEY,
[cname] [nvarchar](20) NOT NULL,
[tno] [int] NOT NULL
)
--创建外键
ALTER TABLE [dbo].[course] WITH CHECK ADD
CONSTRAINT [FK_course_teacher] FOREIGN KEY([tno])
REFERENCES [dbo].[teacher] ([tno])
--插入数据
insert into course(cno,cname,tno) values(1,'企业管理',3)
insert into course(cno,cname,tno) values(2,'马思',1)
insert into course(cno,cname,tno) values(3,'UML',2)
insert into course(cno,cname,tno) values(4,'数据库',5)
insert into course(cno,cname,tno) values(5,'物理',8)
--5.创建成绩表
CREATE TABLE [dbo].[sc](
[sno] [int] NOT NULL,
[cno] [int] NOT NULL,
[score] [int] NOT NULL
)
--创建外键
ALTER TABLE [dbo].[sc] WITH CHECK ADD CONSTRAINT [FK_sc_course] FOREIGN KEY([cno])
REFERENCES [dbo].[course] ([cno])
ALTER TABLE [dbo].[sc] WITH CHECK ADD CONSTRAINT [FK_sc_student] FOREIGN KEY([sno])
REFERENCES [dbo].[student] ([sno])
--插入数据
INSERT INTO sc(sno,cno,score)VALUES(1,1,80)
INSERT INTO sc(sno,cno,score)VALUES(1,2,86)
INSERT INTO sc(sno,cno,score)VALUES(1,3,83)
INSERT INTO sc(sno,cno,score)VALUES(1,4,89)
INSERT INTO sc(sno,cno,score)VALUES(2,1,50)
INSERT INTO sc(sno,cno,score)VALUES(2,2,36)
--INSERT INTO sc(sno,cno,score)VALUES(2,3,43)
INSERT INTO sc(sno,cno,score)VALUES(2,4,59)
INSERT INTO sc(sno,cno,score)VALUES(3,1,50)
INSERT INTO sc(sno,cno,score)VALUES(3,2,96)
--INSERT INTO sc(sno,cno,score)VALUES(3,3,73)
INSERT INTO sc(sno,cno,score)VALUES(3,4,69)
INSERT INTO sc(sno,cno,score)VALUES(4,1,90)
INSERT INTO sc(sno,cno,score)VALUES(4,2,36)
INSERT INTO sc(sno,cno,score)VALUES(4,3,88)
--INSERT INTO sc(sno,cno,score)VALUES(4,4,99)
INSERT INTO sc(sno,cno,score)VALUES(5,1,90)
INSERT INTO sc(sno,cno,score)VALUES(5,2,96)
INSERT INTO sc(sno,cno,score)VALUES(5,3,98)
INSERT INTO sc(sno,cno,score)VALUES(5,4,99)
INSERT INTO sc(sno,cno,score)VALUES(6,1,70)
INSERT INTO sc(sno,cno,score)VALUES(6,2,66)
INSERT INTO sc(sno,cno,score)VALUES(6,3,58)
INSERT INTO sc(sno,cno,score)VALUES(6,4,79)
INSERT INTO sc(sno,cno,score)VALUES(7,1,80)
INSERT INTO sc(sno,cno,score)VALUES(7,2,76)
INSERT INTO sc(sno,cno,score)VALUES(7,3,68)
INSERT INTO sc(sno,cno,score)VALUES(7,4,59)
INSERT INTO sc(sno,cno,score)VALUES(7,5,89)
下面进入第二阶段、、、、、!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
--教学系统SQL案例
注意:我觉得具有挑战性的也是检验你学的SQL语句学的好不好的几道题:9,13,14,16,19,24,25
--1、查询课程1的成绩 比 课程2的成绩 高 的所有学生的学号.
select a.sno from
(select sno,score from sc where cno=1) a,
(select sno,score from sc where cno=2) b
where a.score>b.score and a.sno=b.sno
--2、查询平均成绩大于60分的同学的学号和平均成绩;
select sno,avg(score) as sscore from sc group by sno having avg(score) >60
--select a.sno as "学号", avg(a.score) as "平均成绩"
--from
--(select sno,score from sc) a
--group by sno having avg(a.score)>60
但是写成这样就会出错:语法没错
--select a.sno as "学号", avg(a.score) as "平均成绩"
--from
--(select sno,score from sc) a
--where avg(a.score)>60
--3、查询所有同学的学号、姓名、选课数、总成绩
select a.sno as 学号, b.sname as 姓名,
count(a.cno) as 选课数, sum(a.score) as 总成绩
from sc a, student b
where a.sno = b.sno
group by a.sno, b.sname
go
--3、查询所有同学的学号、姓名、选课数、总成绩
select student.sno as 学号, student.sname as 姓名,
count(sc.cno) as 选课数, sum(score) as 总成绩
from student left Outer join sc on student.sno = sc.sno
group by student.sno, sname
--4、查询姓“李”的老师的个数;
select count(distinct(tname)) from teacher where tname like '李%‘
select tname as "姓名", count(distinct(tname)) as "人数"
from teacher
where tname like'李%'
group by tname
go
--5、查询没学过“叶平”老师课的同学的学号、姓名;
select student.sno,student.sname from student
where sno not in (select distinct(sc.sno) from sc,course,teacher
where sc.cno=course.cno and teacher.tno=course.tno and teacher.tname='叶平')
--6、查询同时学过课程1和课程2的同学的学号、姓名
select sno, sname from student
where sno in (select sno from sc where sc.cno = 1)
and sno in (select sno from sc where sc.cno = 2)
go
select c.sno, c.sname from
(select sno from sc where sc.cno = 1) a,
(select sno from sc where sc.cno = 2) b,
student c
where a.sno = b.sno and a.sno = c.sno
go
select student.sno,student.sname from student,sc where student.sno=sc.sno and sc.cno=1
and exists( Select * from sc as sc_2 where sc_2.sno=sc.sno and sc_2.cno=2)
go
--7、查询学过“叶平”老师所教所有课程的所有同学的学号、姓名
select a.sno, a.sname from student a, sc b
where a.sno = b.sno and b.cno in
(select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '叶平')
select a.sno, a.sname from student a, sc b,
(select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '叶平') e
where a.sno = b.sno and b.cno = e.cno
--8、查询 课程编号1的成绩 比 课程编号2的成绩 高的所有同学的学号、姓名
select a.sno, a.sname from student a,
(select sno, score from sc where cno = 1) b,
(select sno, score from sc where cno = 2) c
where b.score > c.score and b.sno = c.sno and a.sno = b.sno
--9、查询所有课程成绩小于60分的同学的学号、姓名
select sno,sname from student
where sno not in (select distinct sno from sc where score > 60)
--10、查询所有课程成绩大于60分的同学的学号、姓名
select sno,sname from student
where sno not in (select distinct sno from sc where score < 60)
--11、查询没有学全所有课的同学的学号、姓名
select student.sno, student.sname
from student, sc
where student.sno = sc.sno
group by student.sno, student.sname
having count(sc.cno) < (select count(cno) from course)
方法二:
select r2.rsno
from
(select COUNT(course.cno)as num1 from course) r1,
(select COUNT(sc.sno)as num2, sc.sno as rsno from sc group by sc.sno ) r2
where r1.num1>r2.num2
--12、查询至少有一门课程 与 学号为1的同学所学课程 相同的同学的学号和姓名
select distinct a.sno, a.sname
from student a, sc b
where a.sno <> 1 and a.sno=b.sno and
b.cno in (select cno from sc where sno = 1)
方法二:
--select s.sno,s.sname
--from student s,
--(select sc.sno
--from sc
--where sc.cno in (select sc1.cno from sc sc1 where sc1.sno=1)and sc.sno<>1
--group by sc.sno)r1
--where r1.sno=s.sno
--13、把“sc”表中“刘老师”所教课的成绩都更改为此课程的平均成绩
update sc set score = (select avg(sc_2.score) from sc sc_2 where sc_2.cno=sc.cno)
from course,teacher where course.cno=sc.cno and course.tno=teacher.tno and teacher.tname='叶平'
--14、查询和2号同学学习的课程完全相同的其他同学学号和姓名
/* --Do first :
select sno
from sc
where sno <> 2
group by sno
having sum(cno) = (select sum(cno) from sc where sno = 2)
*/
select b.sno, b.sname
from sc a, student b
where b.sno <> 2 and a.sno = b.sno
group by b.sno, b.sname
having sum(cno) = (select sum(cno) from sc where sno = 2)
--15、删除学习“叶平”老师课的sc表记录
delete sc from course, teacher
where course.cno = sc.cno and course.tno = teacher.tno and tname = '叶平'
--16、向sc表中插入一些记录,这些记录要求符合以下条件:
--将没有课程3成绩同学的该成绩补齐, 其成绩取所有学生的课程2的平均成绩
INSERT sc select sno, 3, (select avg(score) from sc where cno = 2)
from student
where sno not in (select sno from sc where cno = 3)
--17、按平平均分从高到低显示所有学生的如下统计报表:
-- 学号,企业管理,马思,UML,数据库,物理,课程数,平均分
SELECT sno as 学号
,max(case when cno = 1 then score end) AS 企业管理
,max(case when cno = 2 then score end) AS 马思
,max(case when cno = 3 then score end) AS UML
,max(case when cno = 4 then score end) AS 数据库
,max(case when cno = 5 then score end) AS 物理
,count(cno) AS 课程数
,avg(score) AS 平均分
FROM sc
GROUP by sno
ORDER by avg(score) DESC
--18、查询各科成绩最高分和最低分:以如下形式显示:课程号,最高分,最低分
select cno as 课程号, max(score) as 最高分, min(score) 最低分
from sc group by cno
select course.cno as '课程号'
,MAX(score) as '最高分'
,MIN(score) as '最低分'
from sc,course
where sc.cno=course.cno
group by course.cno
--19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
SELECT t.cno AS 课程号,
max(course.cname)AS 课程名,
isnull(AVG(score),0) AS 平均成绩,
100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/count(1) AS 及格率
FROM sc t, course
where t.cno = course.cno
GROUP BY t.cno
ORDER BY 及格率 desc
--20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马思(002),UML (003),数据库(004)
select
avg(case when cno = 1 then score end) as 平均分1,
avg(case when cno = 2 then score end) as 平均分2,
avg(case when cno = 3 then score end) as 平均分3,
avg(case when cno = 4 then score end) as 平均分4,
100 * sum(case when cno = 1 and score > 60 then 1 else 0 end) / sum(case when cno = 1 then 1 else 0 end) as 及格率1,
100 * sum(case when cno = 2 and score > 60 then 1 else 0 end) / sum(case when cno = 2 then 1 else 0 end) as 及格率2,
100 * sum(case when cno = 3 and score > 60 then 1 else 0 end) / sum(case when cno = 3 then 1 else 0 end) as 及格率3,
100 * sum(case when cno = 4 and score > 60 then 1 else 0 end) / sum(case when cno = 4 then 1 else 0 end) as 及格率4
from sc
--21、查询不同老师所教不同课程平均分, 从高到低显示
-- 张老师 数据库 88
select max(c.tname) as 教师, max(b.cname) 课程, avg(a.score) 平均分
from sc a, course b, teacher c
where a.cno = b.cno and b.tno = c.tno
group by a.cno
order by 平均分 desc
方法二:
select r.tname as '教师',r.rname as '课程' , AVG(score) as '平均分'
from sc,
(select
t.tname,c.cno as rcso,c.cname as rname
from teacher t ,course c
where t.tno=c.tno
)r
where sc.cno=r.rcso
group by sc.cno,r.tname,r.rname
order by AVG(score) desc
--22、查询如下课程成绩均在第3名到第6名之间的学生的成绩:
-- [学生ID],[学生姓名],企业管理,马思,UML,数据库,平均成绩
select top 6 max(a.sno) 学号, max(b.sname) 姓名,
max(case when cno = 1 then score end) as 企业管理,
max(case when cno = 2 then score end) as 马思,
max(case when cno = 3 then score end) as UML,
max(case when cno = 4 then score end) as 数据库,
avg(score) as 平均分
from sc a, student b
where a.sno not in (select top 2 sno from sc where cno = 1 order by score desc)
and a.sno not in (select top 2 sno from sc where cno = 2 order by score desc)
and a.sno not in (select top 2 sno from sc where cno = 3 order by score desc)
and a.sno not in (select top 2 sno from sc where cno = 4 order by score desc)
and a.sno = b.sno
group by a.sno
--23、统计打印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
select sc.cno as 课程ID, cname as 课程名称,
sum(case when score >= 85 then 1 else 0 end) as [100-85],
sum(case when score < 85 and score >= 70 then 1 else 0 end) as [85-70],
sum(case when score < 70 and score >= 60 then 1 else 0 end) as [70-60],
sum(case when score < 60 then 1 else 0 end) as [ <60]
from sc, course
where sc.cno = course.cno
group by sc.cno, cname
--24、查询学生平均分及其名次
--drop table t1
--select sno, avg(score) as pjf into t1 from sc group by sno
--go
--
--drop table t2
--select distinct avg(score) as pjf into t2 from sc group by sno
--go
--
--select
-- (select count(1) from t2 where pjf >= t1.pjf) as 名次,
-- sno as 学号,
-- pjf as 平均分
--from t1
--order by pjf desc
--go
select
(select count(1)
from (select distinct avg(score) as pjf from sc group by sno) as t2
where pjf >= t1.pjf) as 名次,
sno as 学号,
pjf as 平均分
from (select sno, avg(score) as pjf from sc group by sno) as t1
order by pjf desc
go
--25、查询各科成绩前三名的记录:(不考虑成绩并列情况)
--drop table aa
--select sno, cno, score into aa from sc order by cno, score desc
--
--drop table bb
--select distinct cno, score into bb from sc order by cno, score desc
--
--select aa.* from aa
--where aa.score in (select top 3 score from bb where aa.cno = bb.cno)
select *
from (select top 9999 sno, cno, score from sc order by cno, score desc) as aa
where aa.score in
(select top 3 score
from (select distinct top 9999 cno, score from sc order by cno, score desc) as bb
where aa.cno = bb.cno)
--26、查询每门课程被选修的学生数
select cno,count(sno) from sc group by cno
select
sc.cno
,COUNT(sc.sno)' 学生数 '
from sc,course c
where sc.cno=c.cno
group by sc.cno
--27、查询出只选修了一门课程的全部学生的学号和姓名
SELECT sc.sno, student.sname, count(cno) AS 选课数
FROM sc, student
WHERE sc.sno = student.sno
GROUP BY sc.sno, student.sname
HAVING count(cno) = 3
--28、查询男生、女生人数
select
(select count(1) from student where ssex = '男') 男生人数,
(select count(1) from student where ssex = '女') 女生人数
select
(select COUNT(sno) where ssex='男') as '男生人数'
,(select COUNT(sno) where ssex='女') as '女生人数'
from student
group by student.ssex
--29、查询姓“张”的学生名单
SELECT sname FROM student WHERE sname like '张%'
--30、查询同名同性学生名单,并统计同名人数
select
s1.sname
,COUNT(s1.sname) as '人数'
from student s1,
(select
s.sname ,s.ssex
from student s
)r
where s1.sname=r.sname and s1.ssex=r.ssex
group by s1.sname
having COUNT(s1.sname)>1
go
select sname, count(1) from student group by sname having count(1) > 1
--31、1981年出生的学生名单(注:student表中sage列的类型是datetime)
select sname, CONVERT(char(4), DATEPART(year,sage)) as age
from student
where DATEPART(year,sage)=1981
--32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select cno 课程号, avg(score) 平均分
from sc group by cno order by 平均分 asc, cno desc
--33、查询平均成绩大于80的所有学生的学号、姓名和平均成绩
select
s1.sno,
s.sname,
AVG(s1.score) as '平均成绩'
from student s,sc s1
where s.sno=s1.sno
group by s1.sno,s.sname
having AVG(s1.score)>80
go
select sno, avg(score)
from sc
group by sno
having avg(score) > 80
--34、查询 数据库 分数 低于60的学生姓名和分数
select c.sname, a.score
from sc a, course b, student c
where a.cno = b.cno and a.sno = c.sno
and b.cname = '数据库' and score < 60
--35、查询所有学生的选课情况
SELECT sc.sno 学号,sname 姓名,cname 课程, sc.cno 课号
FROM sc,student,course
WHERE sc.sno=student.sno and sc.cno=course.cno
ORDER BY sc.sno
--36、查询成绩在70分以上的学生姓名、课程名称和分数
select
s.sname,
c.cname,
(s1.score) as '分数'
from student s,sc s1,course c
where s.sno=s1.sno and c.cno=s1.cno and s1.score>=70
--group by s1.sno,s.sname
go
SELECT student.sno,student.sname,sc.cno,sc.score
FROM student,Sc
WHERE sc.score>=70 AND sc.sno=student.sno;
--37、查询不及格的课程,并按课程号从大到小排列
select cno, score from sc where score < 60 order by cno
go
select
sc.cno
,c.cname
,sc.score
from sc ,course c
where sc.score<60 and c.cno=sc.cno
order by sc.cno desc
--38、查询课程编号为3且课程成绩在80分以上的学生的学号和姓名
select sc.sno,student.sname from sc,student where sc.sno=student.sno and score>80 and cno=3
go
select
s.sno,s.sname,s1.score
from student s,sc s1
where s1.sno=s.sno and s1.cno=3 and s1.score>=80
--39、求选了课程的学生人数
select count(distinct sno) from sc
--40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select student.sname,cname, score
from student,sc,course C,teacher
where student.sno=sc.sno and sc.cno=C.cno and C.tno=teacher.tno
and teacher.tname ='叶平'
and sc.score=(select max(score)from sc where cno = C.cno)
--41、查询各个课程及相应的选修人数
select cno 课程号, count(1) 选修人数 from sc group by cno
go
select
c.cname
,COUNT(sc.sno) '选修人数'
from course c,sc
where sc.cno=c.cno
group by c.cname
--42、查询不同课程成绩相同的学生的学号、课程号、学生成绩
select distinct A.sno, A.cno,B.score
from sc A ,sc B
where A.Score=B.Score and A.cno <>B.cno
order by B.score
go
select
sc.sno
,sc.cno
,sc.score
from sc,
(select
sc.sno
,sc.cno
,sc.score
from sc)r
where r.score=sc.score and r.cno<>sc.cno
--43、查询每门课程成绩最好的前两名的学生ID
--先按照 课程, 成绩 高低 对 sc表 排序
--select * from sc order by cno, score desc
select * from sc a
where score in (select top 2 score from sc where a.cno = sc.cno order by sc.score desc)
order by a.cno, a.score desc
--查询各单科状元
select * from sc a
where score = (select top 1 score from sc where a.cno = sc.cno order by sc.score desc)
order by a.cno, a.score desc
最终结果:
select top 2--这是最后的一行编码目的是只取用课程成绩降序排名后的前2行
r.sname as '前两名'
, MAX(r.grade) as '课程成绩'
from
(
select
s.sname
,s.sno
,max(score) as grade
from student s,sc
where sc.sno=s.sno
group by s.sname
,s.sno
)r
group by r.sname
order by 课程成绩 desc
--44、统计每门课程的学生选修人数(至少有2人选修的课程才统计)。要求输出课程号和选修人数,
--查询结果按人数降序排列,若人数相同,按课程号升序排列
select cno as 课程号,count(1) as 人数
from sc
group by cno having count(1) > 1
order by count(1) desc,cno
go
select
r.cno as 课程号
,r.num as '选修人数'
from
(select
sc.cno
,COUNT(sc.cno)as num
from sc
group by sc.cno
)r
where r.num>=2
--45、检索至少选修了5门课程的学生学号
select sno from sc group by sno having count(1) >= 5
go
select
r.sno
,r.sname
,COUNT(r.sno) as 至少选修了5门
from
(select
s.sno
,s.sname
,sc.cno
from sc,student s
where sc.sno=s.sno
)r
group by r.sno,r.sname
having COUNT(r.sno)>=5
--46、查询全部学生都选修的课程的课程号和课程名
--(思路:查询最受欢迎的课程是啥)
--select cno 课程ID, count(1) 选修人数 from sc group by cno
select course.cno, cname
from sc, course
where sc.cno = course.cno
group by course.cno, cname
having count(sc.cno) = (select count(1) from student)
go
select
c1.cno
,c1.cname
from course c1,
(
select
r.cno as cno
from
(select
sc.cno
,count(sc.cno) as num
from sc,course c
where sc.cno=c.cno
group by sc.cno
)r
where r.num=(select count(1) from student)
)rr
where rr.cno=c1.cno
--查询最受欢迎的课程
select cno 课程ID, count(cno) 选修人数
from sc group by cno
having count(cno) in (select top 1 count(cno) from sc group by cno order by count(cno) desc)
order by 选修人数 desc
--47、查询没学过“叶平”老师讲授的任一门课程的学生姓名
--思路: 先得到学过“叶平”老师讲授的所有课程清单
--select a.cno from course a, teacher b where a.tno = b.tno and b.tname = '叶平')
--然后: 从 sc表中 得到 学过上述课程的 所有学生清单
--select sno from sc where cno in
--(select a.cno from course a, teacher b where a.tno = b.tno and b.tname = '叶平'))
--最后: 从student表中 得到不在上数学生清单中的其他学生,即为最后的查询结果
select sno, sname from student
where sno not in(
select sno from sc where cno in
(select a.cno from course a, teacher b where a.tno = b.tno and b.tname = '叶平'))
select sno, sname from student
where sno not in
(select sno from course,teacher,sc
where course.tno=teacher.tno and sc.cno=course.cno and tname='叶平')
--48、查询两门以上不及格课程的同学的学号及其平均成绩
--思路: 先查询出所有不及格的sc中的记录
--select sno, score from sc where score < 60
select sno 学号, avg(score) 平均分, count(1) 不及格课程数
from sc where score < 60 group by sno having count(1) > 2
select
sno,avg(score) 平均分,COUNT(sno) as 不及格课程
from sc
where sc.score<60
group by sno
having COUNT(sno)>2
--49、检索4号课程分数大于60的同学学号,按分数降序排列
select sno, score from sc where cno = 4 and score > 60 order by score desc
--50、删除2号同学的课程1的成绩
--delete sc where sno = 2 and cno = 1
--select * from sc where sno = 2 and cno = 1
delete from sc where sno = 2 and cno = 1
作业:
--43.查询各单科状元
--46.查询最受欢迎的课程(选修学生最多的课程)
--xx.查询成绩最好的课程
--xx.查询最受欢迎的老师(选修学生最多的老师)
--xx.查询教学质量最好的老师
--xx.查询需要补考的各科学生清单
相关推荐
掌握JDBC的基本操作,如连接数据库、执行SQL语句以及处理结果集等,是进行数据库操作的关键技能。 #### JavaMail:邮件发送 除了数据库交互外,JavaMail API也是JavaEE开发中常用的工具之一。JavaMail提供了一套...
要学好Java并找到一份相关工作,需要对整个学习过程有深入的理解和坚持不懈的努力。Java是一种广泛应用的编程语言,尤其在企业级应用开发中占据重要地位。以下将详细阐述学习Java的不同阶段及其关键知识点。 首先,...
4.2 学习数据库开发技术 这里我想重点说一下数据库开发技术,数据库技术是做业务系统必备技能,JavaWeb开发人员最低程度都应该掌握SQL语句的使用!数据库技术大体可分为DBA技术和数据库开发技术,对于开发而言,应...
这部分会教授如何连接数据库,执行SQL语句,以及使用数据绑定技术将数据库数据实时显示在界面上。 第五章至后续章节可能会深入到更多实际应用,如网络编程、图形用户界面设计、多线程编程、内存管理、性能优化等。...
哈希表源码
sun_3ck_03_0119
内容概要:本文档详细介绍了基于 MATLAB 实现的 LSTM-AdaBoost 时间序列预测模型,涵盖项目背景、目标、挑战、特点、应用领域以及模型架构和代码示例。随着大数据和AI的发展,时间序列预测变得至关重要。传统方法如 ARIMA 在复杂非线性序列中表现欠佳,因此引入了 LSTM 来捕捉长期依赖性。但 LSTM 存在易陷局部最优、对噪声鲁棒性差的问题,故加入 AdaBoost 提高模型准确性和鲁棒性。两者结合能更好应对非线性和长期依赖的数据,提供更稳定的预测。项目还展示了如何在 MATLAB 中具体实现模型的各个环节。 适用人群:对时间序列预测感兴趣的开发者、研究人员及学生,特别是有一定 MATLAB 编程经验和熟悉深度学习或机器学习基础知识的人群。 使用场景及目标:①适用于金融市场价格预测、气象预报、工业生产故障检测等多种需要时间序列分析的场合;②帮助使用者理解并掌握将LSTM与AdaBoost结合的实现细节及其在提高预测精度和抗噪方面的优势。 其他说明:尽管该模型有诸多优点,但仍存在训练时间长、计算成本高等挑战。文中提及通过优化数据预处理、调整超参数等方式改进性能。同时给出了完整的MATLAB代码实现,便于学习与复现。
1996-2019年各地级市平均工资数据 1、时间:1996-2019年 2、来源:城市nj、各地级市统计j 3、指标:平均工资(在岗职工) 4、范围:295个地级市
AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
内容概要:本文介绍了一种新颖的变压器模型C2Former(Calibrated and Complementary Transformer),专门用于解决RGB图像和红外图像之间的物体检测难题。传统方法在进行多模态融合时面临两个主要问题——模态错位(Modality miscalibration)和融合不准确(fusion imprecision)。作者针对这两个问题提出采用互模交叉注意力模块(Inter-modality Cross-Attention, ICA)以及自适应特征采样模块(Adaptive Feature Sampling, AFS)来改善。具体来说,ICA可以获取对齐并且互补的特性,在特征层面进行更好的整合;而AFS则减少了计算成本。通过实验验证了基于C2Former的一阶段和二阶段检测器均能在现有公开数据集上达到最先进的表现。 适合人群:计算机视觉领域的研究人员和技术人员,特别是从事跨模态目标检测的研究人员,对Transformer架构有一定了解的开发者。 使用场景及目标:适用于需要将可见光和热成像传感器相结合的应用场合,例如全天候的视频监控系统、无人驾驶汽车、无人
上海人工智能实验室:金融大模型应用评测报告-摘要版2024.pdf
malpass_02_0907
C++-自制学习辅助工具
内容概要:本文提供了有关微信生态系统的综合开发指导,具体涵盖了微信机器人的Java与Python开发、全套及特定应用的小程序源码(PHP后台、DeepSeek集成),以及微信公众号的基础开发与智能集成方法。文中不仅给出了各种应用的具体案例和技术要点如图灵API对接、DeepSeek大模型接入等的简述,还指出了相关资源链接以便深度探究或直接获取源码进行开发。 适合人群:有意开发微信应用程序或提升相应技能的技术爱好者和专业人士。不论是初涉者寻求基本理解和操作流程,还是进阶者期望利用提供的资源进行项目构建或是研究。 使用场景及目标:开发者能够根据自身兴趣选择不同方向深入学习微信平台的应用创建,如社交自动化(机器人)、移动互联网服务交付(小程序),或者公众信息服务(公众号)。特别是想要尝试引入AI能力到应用中的人士,文中介绍的内容非常有价值。 其他说明:文中提及的多个项目都涉及到了最新技术栈(如DeepSeek大模型),并且为不同层次的学习者提供从零开始的详细资料。对于那些想要迅速获得成果同时深入了解背后原理的人来说是个很好的起点。
pimpinella_3cd_01_0916
mellitz_3cd_01_0516
schube_3cd_01_0118
AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!