sql语句练习
<p>
</p>
<p class="MsoNormal"><span style='font-size: 9.0pt; font-family: "Microsoft JhengHei";' lang="EN-US">Student(S#,Sname,Sage,Ssex) </span><span style='font-size: 9.0pt; font-family: "Microsoft JhengHei";'>学生表<span lang="EN-US"><br>
Course(C#,Cname,T#) </span>课程表<span lang="EN-US"><br>
SC(S#,C#,score) </span>成绩表<span lang="EN-US"><br>
Teacher(T#,Tname) </span>教师表<span lang="EN-US"><br><br></span>问题:<span lang="EN-US"><br>
1</span>、查询“<span lang="EN-US">001</span>”课程比“<span lang="EN-US">002</span>”课程成绩高的所有学生的学号;<span lang="EN-US"><br>
select a.S# from (select s#,score from
SC where C#='001') a,(select s#,score<br>
from SC where C#='002') b<br>
where a.score>b.score and a.s#=b.s#;<br>
2</span>、查询平均成绩大于<span lang="EN-US">60</span>分的同学的学号和平均成绩;<span lang="EN-US"><br>
select S#,avg(score)<br>
from sc<br>
group by S# having avg(score) >60;<br>
3</span>、查询所有同学的学号、姓名、选课数、总成绩;<span lang="EN-US"><br>
select
Student.S#,Student.Sname,count(SC.C#),sum(score)<br>
from Student left Outer join SC on
Student.S#=SC.S#<br>
group by Student.S#,Sname<br>
4</span>、查询姓“李”的老师的个数;<span lang="EN-US"><br>
select count(distinct(Tname))<br>
from Teacher<br>
where Tname like '</span>李<span lang="EN-US">%';<br>
5</span>、查询没学过“叶平”老师课的同学的学号、姓名;<span lang="EN-US"><br>
select Student.S#,Student.Sname<br>
from Student <br>
where S# not in (select distinct(
SC.S#) from SC,Course,Teacher where
SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='</span>叶平<span lang="EN-US">');<br>
6</span>、查询学过“<span lang="EN-US">001</span>”并且也学过编号“<span lang="EN-US">002</span>”课程的同学的学号、姓名;<span lang="EN-US"><br>
select Student.S#,Student.Sname from
Student,SC where Student.S#=SC.S# and SC.C#='001'and exists( Select * from SC
as SC_2 where SC_2.S#=SC.S# and SC_2.C#='002');<br>
7</span>、查询学过“叶平”老师所教的所有课的同学的学号、姓名;<span lang="EN-US"><br>
select S#,Sname<br>
from Student<br>
where S# in (select S# from SC ,Course
,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='</span>叶平<span lang="EN-US">' group by S# having count(SC.C#)=(select count(C#) from
Course,Teacher where
Teacher.T#=Course.T# and Tname='</span>叶平<span lang="EN-US">'));<br>
8</span>、查询课程编号“<span lang="EN-US">002</span>”的成绩比课程编号“<span lang="EN-US">001</span>”课程低的所有同学的学号、姓名;<span lang="EN-US"><br>
Select S#,Sname from (select
Student.S#,Student.Sname,score ,(select score from SC SC_2 where
SC_2.S#=Student.S# and SC_2.C#='002') score2<br>
from Student,SC where Student.S#=SC.S#
and C#='001') S_2 where score2 <score;<br>
9</span>、查询所有课程成绩小于<span lang="EN-US">60</span>分的同学的学号、姓名;<span lang="EN-US"><br>
select S#,Sname<br>
from Student<br>
where S# not in (select Student.S# from
Student,SC where S.S#=SC.S# and score>60);<br>
10</span>、查询没有学全所有课的同学的学号、姓名;<span lang="EN-US"><br>
select Student.S#,Student.Sname<br>
from Student,SC<br>
where Student.S#=SC.S# group by Student.S#,Student.Sname having count(C#)
<(select count(C#) from Course);<br>
11</span>、查询至少有一门课与学号为“<span lang="EN-US">1001</span>”的同学所学相同的同学的学号和姓名;<span lang="EN-US"><br>
select S#,Sname from Student,SC where
Student.S#=SC.S# and C# in select C# from SC where S#='1001';<br>
12</span>、查询至少学过学号为“<span lang="EN-US">001</span>”同学所有一门课的其他同学学号和姓名;<span lang="EN-US"><br>
select distinct SC.S#,Sname<br>
from Student,SC<br>
where Student.S#=SC.S# and C# in
(select C# from SC where S#='001');<br>
13</span>、把“<span lang="EN-US">SC</span>”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;<span lang="EN-US"><br>
update SC set score=(select
avg(SC_2.score)<br>
from SC SC_2<br>
where SC_2.C#=SC.C# ) from
Course,Teacher where Course.C#=SC.C# and Course.T#=Teacher.T# and
Teacher.Tname='</span>叶平<span lang="EN-US">');<br>
14</span>、查询和“<span lang="EN-US">1002</span>”号的同学学习的课程完全相同的其他同学学号和姓名;<span lang="EN-US"><br>
select S# from SC where C# in (select
C# from SC where S#='1002')<br>
group by S# having count(*)=(select
count(*) from SC where S#='1002');<br>
15</span>、删除学习“叶平”老师课的<span lang="EN-US">SC</span>表记录;<span lang="EN-US"><br>
Delect SC<br>
from course ,Teacher <br>
where Course.C#=SC.C# and Course.T#=
Teacher.T# and Tname='</span>叶平<span lang="EN-US">';<br>
16</span>、向<span lang="EN-US">SC</span>表中插入一些记录,这些记录要求符合以下条件:没有上过编号“<span lang="EN-US">003</span>”课程的同学学号、<span lang="EN-US">2</span>、<span lang="EN-US"><br>
</span>号课的平均成绩;<span lang="EN-US"><br>
Insert SC select S#,'002',(Select
avg(score)<br>
from SC where C#='002') from Student
where S# not in (Select S# from SC where C#='002');<br>
17</span>、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示: 学生<span lang="EN-US">ID,,</span>数据库<span lang="EN-US">,</span>企业管理<span lang="EN-US">,</span>英语<span lang="EN-US">,</span>有效课程数<span lang="EN-US">,</span>有效平均分<span lang="EN-US"><br>
SELECT S# as </span>学生<span lang="EN-US">ID<br>
,(SELECT score FROM SC WHERE
SC.S#=t.S# AND C#='004') AS </span>数据库<span lang="EN-US"><br>
,(SELECT score FROM SC WHERE
SC.S#=t.S# AND C#='001') AS </span>企业管理<span lang="EN-US"><br>
,(SELECT score FROM SC WHERE
SC.S#=t.S# AND C#='006') AS </span>英语<span lang="EN-US"><br>
,COUNT(*) AS </span>有效课程数<span lang="EN-US">, AVG(t.score) AS </span>平均成绩<span lang="EN-US"><br>
FROM SC AS t<br>
GROUP BY S#<br>
ORDER BY avg(t.score) <br>
18</span>、查询各科成绩最高和最低的分:以如下形式显示:课程<span lang="EN-US">ID</span>,最高分,最低分<span lang="EN-US"><br>
SELECT L.C# As </span>课程<span lang="EN-US">ID,L.score AS </span>最高分<span lang="EN-US">,R.score AS </span>最低分<span lang="EN-US"><br>
FROM SC L ,SC AS R<br>
WHERE L.C# = R.C# and<br>
L.score = (SELECT MAX(IL.score)<br>
FROM SC AS
IL,Student AS IM<br>
WHERE L.C# = IL.C#
and IM.S#=IL.S#<br>
GROUP BY IL.C#)<br>
AND<br>
R.Score = (SELECT MIN(IR.score)<br>
FROM SC AS IR<br>
WHERE R.C# = IR.C#<br>
GROUP BY IR.C#<br>
);<br>
19</span>、按各科平均成绩从低到高和及格率的百分数从高到低顺序<span lang="EN-US"><br>
SELECT t.C# AS </span>课程号<span lang="EN-US">,max(course.Cname)AS </span>课程名<span lang="EN-US">,isnull(AVG(score),0)
AS </span>平均成绩<span lang="EN-US"><br>
,100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*)
AS </span>及格百分数<span lang="EN-US"><br>
FROM SC T,Course<br>
where t.C#=course.C#<br>
GROUP BY t.C#<br>
ORDER BY 100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0
END)/COUNT(*) DESC<br>
20</span>、查询如下课程平均成绩和及格率的百分数<span lang="EN-US">(</span>用<span lang="EN-US">"1</span>行<span lang="EN-US">"</span>显示<span lang="EN-US">): </span>企业管理(<span lang="EN-US">001</span>),马克2思(<span lang="EN-US">002</span>),<span lang="EN-US">OO&UML </span>(<span lang="EN-US">003</span>),数据库(<span lang="EN-US">004</span>)<span lang="EN-US"><br>
SELECT SUM(CASE WHEN C# ='001' THEN
score ELSE 0 END)/SUM(CASE C# WHEN '001' THEN 1 ELSE 0 END) AS </span>企业管理平均分<span lang="EN-US"><br>
,100 * SUM(CASE WHEN C# = '001'
AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '001' THEN 1 ELSE 0
END) AS </span>企业管理及格百分数<span lang="EN-US"><br>
,SUM(CASE WHEN C# = '002' THEN
score ELSE 0 END)/SUM(CASE C# WHEN '002' THEN 1 ELSE 0 END) AS </span>马克2思平均分<span lang="EN-US"><br>
,100 * SUM(CASE WHEN C# = '002'
AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '002' THEN 1 ELSE 0
END) AS </span>马克2思及格百分数<span lang="EN-US"><br>
,SUM(CASE WHEN C# = '003' THEN
score ELSE 0 END)/SUM(CASE C# WHEN '003' THEN 1 ELSE 0 END) AS UML</span>平均分<span lang="EN-US"><br>
,100 * SUM(CASE WHEN C# = '003'
AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '003' THEN 1 ELSE 0
END) AS UML</span>及格百分数<span lang="EN-US"><br>
,SUM(CASE WHEN C# = '004' THEN
score ELSE 0 END)/SUM(CASE C# WHEN '004' THEN 1 ELSE 0 END) AS </span>数据库平均分<span lang="EN-US"><br>
,100 * SUM(CASE WHEN C# = '004'
AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '004' THEN 1 ELSE 0
END) AS </span>数据库及格百分数<span lang="EN-US"><br>
FROM SC<br>
21</span>、查询不同老师所教不同课程平均分从高到低显示<span lang="EN-US"><br>
SELECT max(Z.T#) AS </span>教师<span lang="EN-US">ID,MAX(Z.Tname) AS </span>教师姓名<span lang="EN-US">,C.C# AS </span>课程ID<span lang="EN-US">,MAX(C.Cname) AS </span>课程名称<span lang="EN-US">,AVG(Score) AS </span>平均成绩<span lang="EN-US"><br>
FROM SC AS T,Course AS C ,Teacher AS
Z<br>
where T.C#=C.C# and C.T#=Z.T#<br>
GROUP BY C.C#<br>
ORDER BY AVG(Score) DESC<br>
22</span>、查询如下课程成绩第<span lang="EN-US"> 3 </span>名到第<span lang="EN-US"> 6 </span>名的学生成绩单:企业管理(<span lang="EN-US">001</span>),马克2思(<span lang="EN-US">002</span>),<span lang="EN-US">UML </span>(<span lang="EN-US">003</span>),数据库(<span lang="EN-US">004</span>)<span lang="EN-US"><br>
[</span>学生<span lang="EN-US">ID],[</span>学生姓名<span lang="EN-US">],</span>企业管理<span lang="EN-US">,</span>马克2思<span lang="EN-US">,UML,</span>数据库<span lang="EN-US">,</span>平均成绩<span lang="EN-US"><br>
SELECT DISTINCT top 3<br>
SC.S# As </span>学生学号<span lang="EN-US">,<br>
Student.Sname AS </span>学生姓名<span lang="EN-US"> ,<br>
T1.score AS </span>企业管理<span lang="EN-US">,<br>
T2.score AS </span>马克2思<span lang="EN-US">,<br>
T3.score AS UML,<br>
T4.score AS </span>数据库<span lang="EN-US">,<br>
ISNULL(T1.score,0) +
ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) as </span>总分<span lang="EN-US"><br>
FROM Student,SC LEFT JOIN SC AS T1<br>
ON SC.S# = T1.S#
AND T1.C# = '001'<br>
LEFT JOIN SC AS T2<br>
ON SC.S# = T2.S#
AND T2.C# = '002'<br>
LEFT JOIN SC AS T3<br>
ON SC.S# = T3.S#
AND T3.C# = '003'<br>
LEFT JOIN SC AS T4<br>
ON SC.S# = T4.S#
AND T4.C# = '004'<br>
WHERE student.S#=SC.S# and<br>
ISNULL(T1.score,0) +
ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)<br>
NOT IN<br>
(SELECT<br>
DISTINCT<br>
TOP 15 WITH TIES<br>
ISNULL(T1.score,0) +
ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)<br>
FROM sc<br>
LEFT JOIN sc AS T1<br>
ON sc.S# = T1.S# AND T1.C# = 'k1'<br>
LEFT JOIN sc AS T2<br>
ON sc.S# = T2.S#
AND T2.C# = 'k2'<br>
LEFT JOIN sc AS T3<br>
ON sc.S# = T3.S#
AND T3.C# = 'k3'<br>
LEFT JOIN sc AS T4<br>
ON sc.S# = T4.S# AND T4.C# = 'k4'<br>
ORDER BY ISNULL(T1.score,0) +
ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) DESC);<br><br>
23</span>、统计列印各科成绩<span lang="EN-US">,</span>各分数段人数<span lang="EN-US">:</span>课程<span lang="EN-US">ID,</span>课程名称<span lang="EN-US">,[100-85],[85-70],[70-60],[ <60]<br>
SELECT SC.C# as </span>课程<span lang="EN-US">ID, Cname as </span>课程名称<span lang="EN-US"><br>
,SUM(CASE WHEN score BETWEEN 85
AND 100 THEN 1 ELSE 0 END) AS [100 - 85]<br>
,SUM(CASE WHEN score BETWEEN 70
AND 85 THEN 1 ELSE 0 END) AS [85 - 70]<br>
,SUM(CASE WHEN score BETWEEN 60
AND 70 THEN 1 ELSE 0 END) AS [70 - 60]<br>
,SUM(CASE WHEN score < 60 THEN
1 ELSE 0 END) AS [60 -]<br>
FROM SC,Course<br>
where SC.C#=Course.C#<br>
GROUP BY SC.C#,Cname;<br><br>
24</span>、查询学生平均成绩及其名次<span lang="EN-US"><br>
SELECT 1+(SELECT COUNT( distinct </span>平均成绩<span lang="EN-US">)<br>
FROM (SELECT S#,AVG(score)
AS </span>平均成绩<span lang="EN-US"><br>
FROM SC<br>
GROUP BY S#<br>
) AS T1<br>
WHERE </span>平均成绩<span lang="EN-US"> > T2.</span>平均成绩<span lang="EN-US">) as </span>名次<span lang="EN-US">,<br>
S# as </span>学生学号<span lang="EN-US">,</span>平均成绩<span lang="EN-US"><br>
FROM (SELECT S#,AVG(score) </span>平均成绩<span lang="EN-US"><br>
FROM SC<br>
GROUP BY S#<br>
) AS T2<br>
ORDER BY </span>平均成绩<span lang="EN-US">
desc;<br>
<br>
25</span>、查询各科成绩前三名的记录<span lang="EN-US">:(</span>不考虑成绩并列情况<span lang="EN-US">)<br>
SELECT t1.S# as </span>学生<span lang="EN-US">ID,t1.C# as </span>课程<span lang="EN-US">ID,Score as </span>分数<span lang="EN-US"><br>
FROM SC t1<br>
WHERE score IN (SELECT TOP 3 score<br>
FROM SC<br>
WHERE t1.C#= C#<br>
ORDER BY score DESC<br>
)<br>
ORDER BY t1.C#;<br>
26</span>、查询每门课程被选修的学生数<span lang="EN-US"><br>
select c#,count(S#) from sc group by
C#;<br>
27</span>、查询出只选修了一门课程的全部学生的学号和姓名<span lang="EN-US"><br>
select SC.S#,Student.Sname,count(C#) AS
</span>选课数<span lang="EN-US"><br>
from SC ,Student<br>
where SC.S#=Student.S# group by SC.S#
,Student.Sname having count(C#)=1;<br>
28</span>、查询男生、女生人数<span lang="EN-US"><br>
Select count(Ssex) as </span>男生人数<span lang="EN-US"> from Student group by Ssex having Ssex='</span>男<span lang="EN-US">';<br>
Select count(Ssex) as </span>女生人数<span lang="EN-US"> from Student group by Ssex having Ssex='</span>女<span lang="EN-US">'</span>;<span lang="EN-US"><br>
29</span>、查询姓“张”的学生名单<span lang="EN-US"><br>
SELECT Sname FROM Student WHERE Sname
like '</span>张<span lang="EN-US">%';<br>
30</span>、查询同名同性学生名单,并统计同名人数<span lang="EN-US"><br>
select Sname,count(*) from Student
group by Sname having count(*)>1;;<br>
31</span>、<span lang="EN-US">1981</span>年出生的学生名单<span lang="EN-US">(</span>注:<span lang="EN-US">Student</span>表中<span lang="EN-US">Sage</span>列的类型是<span lang="EN-US">datetime)<br>
select Sname, CONVERT(char (11),DATEPART(year,Sage)) as age<br>
from student<br>
where
CONVERT(char(11),DATEPART(year,Sage))='1981';<br>
32</span>、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列<span lang="EN-US"><br>
Select C#,Avg(score) from SC group by
C# order by Avg(score),C# DESC ;<br>
33</span>、查询平均成绩大于<span lang="EN-US">85</span>的所有学生的学号、姓名和平均成绩<span lang="EN-US"><br>
select Sname,SC.S# ,avg(score)<br>
from Student,SC<br>
where Student.S#=SC.S# group by
SC.S#,Sname having avg(score)>85;<br>
34</span>、查询课程名称为“数据库”,且分数低于<span lang="EN-US">60</span>的学生姓名和分数<span lang="EN-US"><br>
Select Sname,isnull(score,0)<br>
from Student,SC,Course<br>
where SC.S#=Student.S# and
SC.C#=Course.C# and Course.Cname='</span>数据库<span lang="EN-US">'and score <60;<br>
35</span>、查询所有学生的选课情况;<span lang="EN-US"><br>
SELECT SC.S#,SC.C#,Sname,Cname<br>
FROM SC,Student,Course<br>
where SC.S#=Student.S# and SC.C#=Course.C#
;<br>
36</span>、查询任何一门课程成绩在<span lang="EN-US">70</span>分以上的姓名、课程名称和分数;<span lang="EN-US"><br>
SELECT distinct
student.S#,student.Sname,SC.C#,SC.score<br>
FROM student,Sc<br>
WHERE SC.score>=70 AND
SC.S#=student.S#;<br>
37</span>、查询不及格的课程,并按课程号从大到小排列<span lang="EN-US"><br>
select c# from sc where scor e <60
order by C# ;<br>
38</span>、查询课程编号为<span lang="EN-US">003</span>且课程成绩在<span lang="EN-US">80</span>分以上的学生的学号和姓名;<span lang="EN-US"><br>
select SC.S#,Student.Sname from
SC,Student where SC.S#=Student.S# and Score>80 and C#='003';<br>
39</span>、求选了课程的学生人数<span lang="EN-US"><br>
select count(*) from sc;<br>
40</span>、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩<span lang="EN-US"><br>
select Student.Sname,score<br>
from Student,SC,Course C,Teacher<br>
where Student.S#=SC.S# and SC.C#=C.C#
and C.T#=Teacher.T# and Teacher.Tname='</span>叶平<span lang="EN-US">' and
SC.score=(select max(score)from SC where C#=C.C# );<br>
41</span>、查询各个课程及相应的选修人数<span lang="EN-US"><br>
select count(*) from sc group by C#;<br>
42</span>、查询不同课程成绩相同的学生的学号、课程号、学生成绩<span lang="EN-US"><br>
select distinct A.S#,B.score from SC A ,SC B where A.Score=B.Score and A.C#
<>B.C# ;<br>
43</span>、查询每门功成绩最好的前两名<span lang="EN-US"><br>
SELECT t1.S# as </span>学生<span lang="EN-US">ID,t1.C# as </span>课程<span lang="EN-US">ID,Score as </span>分数<span lang="EN-US"><br>
FROM SC t1<br>
WHERE score IN (SELECT TOP 2 score<br>
FROM SC<br>
WHERE t1.C#= C#<br>
ORDER BY score DESC<br>
)<br>
ORDER BY t1.C#;<br>
44</span>、统计每门课程的学生选修人数(超过<span lang="EN-US">10</span>人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列<span lang="EN-US"> <br>
select C# as </span>课程号<span lang="EN-US">,count(*) as
</span>人数<span lang="EN-US"><br>
from
sc <br>
group
by C#<br>
order
by count(*) desc,c# <br>
45</span>、检索至少选修两门课程的学生学号<span lang="EN-US"><br>
select S# <br>
from
sc <br>
group
by s#<br>
having count(*)
> = 2<br>
46</span>、查询全部学生都选修的课程的课程号和课程名<span lang="EN-US"><br>
select C#,Cname
<br>
from
Course <br>
where
C# in (select
c# from sc group
by c#) <br>
47</span>、查询没学过“叶平”老师讲授的任一门课程的学生姓名<span lang="EN-US"><br>
select Sname from Student where S#
not in (select S# from Course,Teacher,SC where Course.T#=Teacher.T# and
SC.C#=course.C# and Tname='</span>叶平<span lang="EN-US">');<br>
48</span>、查询两门以上不及格课程的同学的学号及其平均成绩<span lang="EN-US"><br>
select S#,avg(isnull(score,0)) from
SC where S# in (select S# from SC where score <60 group by S# having
count(*)>2)group by S#;<br>
49</span>、检索“<span lang="EN-US">004</span>”课程分数小于<span lang="EN-US">60</span>,按分数降序排列的同学学号<span lang="EN-US"><br>
select S# from SC where C#='004'and
score <60 order by score desc;<br>
50</span>、删除“<span lang="EN-US">002</span>”同学的“<span lang="EN-US">001</span>”课程的成绩<span lang="EN-US"><br>
delete from Sc where S#='001'and C#='001';<br><!--[if !supportLineBreakNewLine]--><br><!--[endif]--></span></span></p>