转,sql 50道练习题

时间:2023-02-22 14:49:17

SQL语句50题

 

-- 一、创建教学系统的数据库,表,以及数据

--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