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
作業:
--43.查詢各單科狀元
--46.查詢最受歡迎的課程(選修學生最多的課程)
--xx.查詢成績最好的課程
--xx.查詢最受歡迎的老師(選修學生最多的老師)
--xx.查詢教學質量最好的老師
--xx.查詢需要補考的各科學生清單
---------------------
作者:zhangyulin54321
來源:CSDN
轉載:https://blog.csdn.net/zhangyulin54321/article/details/7956699
版權宣告:本文為博主原創文章,轉載請附上博文連結!