SQL查询强化训练(1)

自作多情 提交于 2020-02-08 01:45:58
 1  SELECT T1.C#,
 2 
 3          ROUND(T1.AVG_SCORE, 2) AVG_SCORE,
 4 
 5          ROUND(T2.PASS_COUNT / T1.ALL_COUNT, 2) PASS_POINT
 6 
 7     FROM (SELECT DISTINCT C#,
 8 
 9                           COUNT(1) OVER(PARTITION BY C# ORDER BY C#) ALL_COUNT,
10 
11                           AVG(SCORE) OVER(PARTITION BY C# ORDER BY C#) AVG_SCORE
12 
13             FROM SC) T1,
14 
15          (SELECT DISTINCT C#,
16 
17                           COUNT(1) OVER(PARTITION BY C# ORDER BY C#) PASS_COUNT
18 
19             FROM SC
20 
21            WHERE SCORE >= 60) T2
22 
23    WHERE T1.C# = T2.C#
24 
25    ORDER BY AVG_SCORE, PASS_POINT DESC
Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
 
  Union All,对两个结果集进行并集操作,包括重复行,不进行排序;
 
  Intersect,对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;
 
  Minus,对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。

 

SQL查询强化训练 

摘自 http://blog.163.com/ky_199/blog/static/143176020125711452318/

http://yabar.iteye.com/blog/583203 更全

http://www.iteye.com/wiki/blog/1143788更多练习的sql

 

SQL查询强化训练

以下案例用到以下表

Student(S#,Sname,Sage,Ssex) 学生表

S#:学生编号,字符串型

Sname: 学生姓名,字符串型

Sage:年龄 ,数值型    

Ssex:性别,字符串型

Course(C#,Cname,T#) 课程表

C#:课程编号 ,字符串型 (企业管理001,马列002,UML 003,数据库004,英语006) 

Cname:课程名称 ,字符串型

T#:教师编号,字符串型

SC(S#,C#,score) 成绩表

S#:学生编号 ,字符串型  

C#:课程编号 ,字符串型    

score:成绩,数值型

Teacher(T#,Tname) 教师表

T#:教师编号 ,字符串型  

Tname:教师姓名,字符串型

 

注:合格的标准为课程的成绩大于等于60分,带#号的字段表示主键或有外键关联

 

**********************************************************************************************************

View Code
  1 SQL脚本预准备如下:
  2 
  3 create table COURSE
  4 
  5 (
  6 
  7   C#    VARCHAR2(20),
  8 
  9   CNAME VARCHAR2(20),
 10 
 11   T#    VARCHAR2(20)
 12 
 13 )
 14 
 15 create table SC
 16 
 17 (
 18 
 19   S#    VARCHAR2(20),
 20 
 21   C#    VARCHAR2(20),
 22 
 23   SCORE NUMBER(5,2)
 24 
 25 )
 26 
 27 create table STUDENT
 28 
 29 (
 30 
 31   S#    VARCHAR2(20),
 32 
 33   SNAME VARCHAR2(20),
 34 
 35   SAGE  NUMBER,
 36 
 37   SSEX  VARCHAR2(20)
 38 
 39 )
 40 
 41 create table TEACHER
 42 
 43 (
 44 
 45   T#    VARCHAR2(20),
 46 
 47   TNAME VARCHAR2(20)
 48 
 49 )
 50 
 51 insert into course (C#, CNAME, T#)
 52 
 53 values ('001', '企业管理', '9001');
 54 
 55  
 56 
 57 insert into course (C#, CNAME, T#)
 58 
 59 values ('002', '马列', '9002');
 60 
 61  
 62 
 63 insert into course (C#, CNAME, T#)
 64 
 65 values ('003', 'UML', '9006');
 66 
 67  
 68 
 69 insert into course (C#, CNAME, T#)
 70 
 71 values ('004', '数据库', '9004');
 72 
 73  
 74 
 75 insert into course (C#, CNAME, T#)
 76 
 77 values ('006', '英语', '9006');
 78 
 79  
 80 
 81 insert into sc (S#, C#, SCORE)
 82 
 83 values ('1008', '003', 85.00);
 84 
 85  
 86 
 87 insert into sc (S#, C#, SCORE)
 88 
 89 values ('1008', '004', 21.00);
 90 
 91  
 92 
 93 insert into sc (S#, C#, SCORE)
 94 
 95 values ('1008', '001', 47.00);
 96 
 97  
 98 
 99 insert into sc (S#, C#, SCORE)
100 
101 values ('1005', '006', 99.00);
102 
103  
104 
105 insert into sc (S#, C#, SCORE)
106 
107 values ('1012', '002', 22.00);
108 
109  
110 
111 insert into sc (S#, C#, SCORE)
112 
113 values ('1001', '001', 60.00);
114 
115  
116 
117 insert into sc (S#, C#, SCORE)
118 
119 values ('1001', '002', 88.00);
120 
121  
122 
123 insert into sc (S#, C#, SCORE)
124 
125 values ('1001', '004', 20.00);
126 
127  
128 
129 insert into sc (S#, C#, SCORE)
130 
131 values ('1002', '006', 20.00);
132 
133  
134 
135 insert into sc (S#, C#, SCORE)
136 
137 values ('1003', '002', 78.00);
138 
139  
140 
141 insert into sc (S#, C#, SCORE)
142 
143 values ('1003', '006', 90.00);
144 
145  
146 
147 insert into sc (S#, C#, SCORE)
148 
149 values ('1003', '001', 97.00);
150 
151  
152 
153 insert into sc (S#, C#, SCORE)
154 
155 values ('1003', '003', 72.00);
156 
157  
158 
159 insert into sc (S#, C#, SCORE)
160 
161 values ('1004', '002', 88.00);
162 
163  
164 
165 insert into sc (S#, C#, SCORE)
166 
167 values ('1004', '006', 62.00);
168 
169  
170 
171 insert into sc (S#, C#, SCORE)
172 
173 values ('1006', '001', 32.00);
174 
175  
176 
177 insert into sc (S#, C#, SCORE)
178 
179 values ('1006', '003', 99.00);
180 
181  
182 
183 insert into sc (S#, C#, SCORE)
184 
185 values ('1006', '004', 69.00);
186 
187  
188 
189 insert into sc (S#, C#, SCORE)
190 
191 values ('1006', '006', 73.00);
192 
193  
194 
195 insert into sc (S#, C#, SCORE)
196 
197 values ('1007', '001', 77.00);
198 
199  
200 
201 insert into sc (S#, C#, SCORE)
202 
203 values ('1007', '002', 54.00);
204 
205  
206 
207 insert into sc (S#, C#, SCORE)
208 
209 values ('1007', '003', 83.00);
210 
211  
212 
213 insert into sc (S#, C#, SCORE)
214 
215 values ('1008', '006', 12.00);
216 
217  
218 
219 insert into sc (S#, C#, SCORE)
220 
221 values ('1008', '002', 55.00);
222 
223  
224 
225 insert into sc (S#, C#, SCORE)
226 
227 values ('1009', '002', 34.00);
228 
229  
230 
231 insert into sc (S#, C#, SCORE)
232 
233 values ('1010', '002', 84.00);
234 
235  
236 
237 insert into sc (S#, C#, SCORE)
238 
239 values ('1010', '006', 87.00);
240 
241  
242 
243 insert into sc (S#, C#, SCORE)
244 
245 values ('1011', '006', 99.00);
246 
247  
248 
249 insert into sc (S#, C#, SCORE)
250 
251 values ('1012', '004', 87.00);
252 
253  
254 
255 insert into sc (S#, C#, SCORE)
256 
257 values ('1002', '002', 75.00);
258 
259  
260 
261 insert into sc (S#, C#, SCORE)
262 
263 values ('1011', '001', 12.00);
264 
265  
266 
267 insert into sc (S#, C#, SCORE)
268 
269 values ('1011', '002', 76.00);
270 
271  
272 
273 insert into sc (S#, C#, SCORE)
274 
275 values ('1011', '004', 44.00);
276 
277  
278 
279 insert into sc (S#, C#, SCORE)
280 
281 values ('1012', '001', 89.00);
282 
283  
284 
285 insert into student (S#, SNAME, SAGE, SSEX)
286 
287 values ('1001', 'kangyue', 29, 'male');
288 
289  
290 
291 insert into student (S#, SNAME, SAGE, SSEX)
292 
293 values ('1002', 'wangyu', 29, 'male');
294 
295  
296 
297 insert into student (S#, SNAME, SAGE, SSEX)
298 
299 values ('1003', '王新刚', 30, '');
300 
301  
302 
303 insert into student (S#, SNAME, SAGE, SSEX)
304 
305 values ('1004', '鲁豫', 27, 'famale');
306 
307  
308 
309 insert into student (S#, SNAME, SAGE, SSEX)
310 
311 values ('1005', 'sanhui', 29, 'male');
312 
313  
314 
315 insert into student (S#, SNAME, SAGE, SSEX)
316 
317 values ('1006', '梦涵', 28, 'famale');
318 
319  
320 
321 insert into student (S#, SNAME, SAGE, SSEX)
322 
323 values ('1007', 'yangdayong', 30, 'male');
324 
325  
326 
327 insert into student (S#, SNAME, SAGE, SSEX)
328 
329 values ('1008', 'sunguoqiang', 30, 'male');
330 
331  
332 
333 insert into student (S#, SNAME, SAGE, SSEX)
334 
335 values ('1009', '李宏利', 49, 'male');
336 
337  
338 
339 insert into student (S#, SNAME, SAGE, SSEX)
340 
341 values ('1010', '小心', 20, 'famale');
342 
343  
344 
345 insert into student (S#, SNAME, SAGE, SSEX)
346 
347 values ('1011', '小泉', 99, 'male');
348 
349  
350 
351 insert into student (S#, SNAME, SAGE, SSEX)
352 
353 values ('1012', '特里', 39, 'male');
354 
355  
356 
357 insert into teacher (T#, TNAME)
358 
359 values ('9001', '崔老师');
360 
361  
362 
363 insert into teacher (T#, TNAME)
364 
365 values ('9002', '颜老师');
366 
367  
368 
369 insert into teacher (T#, TNAME)
370 
371 values ('9003', '岳老师');
372 
373  
374 
375 insert into teacher (T#, TNAME)
376 
377 values ('9004', '夏老师');
378 
379  
380 
381 insert into teacher (T#, TNAME)
382 
383 values ('9005', '杨老师');
384 
385  
386 
387 insert into teacher (T#, TNAME)
388 
389 values ('9006', '叶平');
390 
391  
392 
393 insert into teacher (T#, TNAME)
394 
395 values ('9007', '梦老师');

 

 

*************************************************************************************************************************

完成以下语句:

1、查询001课程比002课程成绩高的所有学生的学号;

View Code
 1 SELECT T2.S#--, T2.COURSE_001, T2.COURSE_002
 2 
 3 FROM
 4 
 5 (
 6 
 7   SELECT T1.S#, SUM(T1.COURSE_001) COURSE_001, SUM(T1.COURSE_002) COURSE_002
 8 
 9   FROM
10 
11   (
12 
13     SELECT S#,
14 
15            CASE WHEN C# = '001'THEN SCORE
16 
17                 ELSE 0
18 
19                 END COURSE_001,
20 
21            CASE WHEN C# = '002'THEN SCORE
22 
23                 ELSE 0
24 
25                 END COURSE_002
26 
27     FROM SC
28 
29   ) T1
30 
31   GROUP BY S#
32 
33 ) T2
34 
35 WHERE T2.COURSE_001 > T2.COURSE_002

 

2、查询平均成绩大于60分的同学的学号和平均成绩;

View Code
1 SELECT S#, AVG(SCORE) SCORE
2 
3 FROM SC
4 
5 GROUP BY S#
6 
7 HAVING AVG(SCORE) >60

 

3、查询所有同学的学号、姓名、选课数、总成绩;

View Code
 1 SELECT T1.S#, T1.SNAME, T2.CNT, T2.TOTAL_SCORE
 2 
 3 FROM STUDENT T1,
 4 
 5 (
 6 
 7 SELECT S#, COUNT(1) CNT, SUM(SCORE) TOTAL_SCORE
 8 
 9 FROM SC
10 
11 GROUP BY S#
12 
13 ) T2
14 
15 WHERE T1.S# = T2.S#

 

4、查询姓“李”的老师的个数;

View Code
1 SELECT COUNT(1) FROM TEACHER WHERE TNAME LIKE '李%';

 

5、查询没学过“叶平”老师课的同学的学号、姓名;

View Code
 1 SELECT T.S#, T.SNAME
 2 
 3   FROM STUDENT T
 4 
 5 WHERE T.S# NOT IN (SELECT S#
 6 
 7                   FROM SC
 8 
 9                  WHERE C# IN (SELECT C#
10 
11                                 FROM TEACHER T1, COURSE T2
12 
13                                WHERE T1.T# = T2.T#
14 
15                                  AND TNAME = '叶平'));

 

6、查询学过“001并且也学过编号002”课程的同学的学号、姓名;

View Code
 1 SELECT T1.S#, T1.SNAME
 2 
 3   FROM STUDENT T1
 4 
 5 WHERE EXISTS (SELECT T2.S#
 6 
 7           FROM SC T2
 8 
 9          WHERE T2.C# IN ('001', '002')
10 
11            AND T2.S# = T1.S#
12 
13          GROUP BY T2.S#
14 
15         HAVING COUNT(1) > 1);

 

32.--法一:  
33.  
34.SELECT c.s#,c.sname FROM t_wolf_student c WHERE c.s# IN(  
35.  
36.SELECT a.s# FROM t_wolf_sc a WHERE a.c#='001'  
37.  
38.INTERSECT  
39.  
40.SELECT b.s# FROM t_wolf_sc b WHERE b.c#='002');  
41.  
42.--法二:  
43.  
44.SELECT a.s#,a.sname FROM t_wolf_student a,t_wolf_sc b WHERE a.s#=b.s# AND b.c#='001' AND EXISTS   
45.  
46.(SELECT * FROM t_wolf_sc c WHERE c.c#='002' AND c.s#=b.s#);  
47.  

   

7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

View Code
 1 SELECT T.S#, T.SNAME
 2 
 3   FROM STUDENT T
 4 
 5 WHERE T.S# IN (SELECT S#
 6 
 7                   FROM SC
 8 
 9                  WHERE C# IN (SELECT C#
10 
11                                 FROM TEACHER T1, COURSE T2
12 
13                                WHERE T1.T# = T2.T#
14 
15                                  AND TNAME = '叶平'));

 

 

8、查询课程编号001的成绩比课程编号002课程低的所有同学的学号、姓名;

View Code
 1 SELECT T3.S#, T3.SNAME--, T2.COURSE_001, T2.COURSE_002
 2 
 3 FROM
 4 
 5 (
 6 
 7   SELECT T1.S#, SUM(T1.COURSE_001) COURSE_001, SUM(T1.COURSE_002) COURSE_002
 8 
 9   FROM
10 
11   (
12 
13     SELECT S#,
14 
15            CASE WHEN C# = '001'THEN SCORE
16 
17                 ELSE 0
18 
19                 END COURSE_001,
20 
21            CASE WHEN C# = '002'THEN SCORE
22 
23                 ELSE 0
24 
25                 END COURSE_002
26 
27     FROM SC
28 
29   ) T1
30 
31   GROUP BY S#
32 
33 ) T2,
34 
35 STUDENT T3
36 
37 WHERE T2.S# = T3.S#
38 
39 AND   T2.COURSE_001 < T2.COURSE_002

 

9、查询所有课程成绩小于60分的同学的学号、姓名;

View Code
 1 SELECT T.S#, T.SNAME FROM STUDENT T
 2 
 3 WHERE EXISTS (SELECT DISTINCT T1.S#
 4 
 5           FROM SC T1
 6 
 7          WHERE NOT EXISTS (SELECT 1
 8 
 9                   FROM SC T2
10 
11                  WHERE T2.SCORE > 60
12 
13                    AND T2.S# = T1.S#)
14 
15            AND T1.S# = T.S#)

 

 

这里需要注意的:

View Code
 1 有两个简单例子,以说明 “exists”和“in”的效率问题
 2 
 3 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ;
 4 
 5     T1数据量小而T2数据量非常大时,T1<<T2 时,1) 的查询效率高。
 6 
 7 2) select * from T1 where T1.a in (select T2.a from T2) ;
 8 
 9      T1数据量非常大而T2数据量小时,T1>>T2 时,2) 的查询效率高。
10 
11 exists 用法:
12 
13 请注意 1)句中的有颜色字体的部分 ,理解其含义;
14 
15 其中 “select 1 from T2 where T1.a=T2.a” 相当于一个关联表查询,相当于
16 
17 “select 1 from T1,T2     where T1.a=T2.a”
18 
19 但是,如果你当当执行 1) 句括号里的语句,是会报语法错误的,这也是使用exists需要注意的地方。
20 
21 “exists(xxx)”就表示括号里的语句能不能查出记录,它要查的记录是否存在。
22 
23 因此“select 1”这里的 “1”其实是无关紧要的,换成“*”也没问题,它只在乎括号里的数据能不能查找出来,是否存在这样的记录,如果存在,这 1) 句的where 条件成立。
24 
25  
26 
27 in 的用法:
28 
29 继续引用上面的例子
30 
31 “2) select * from T1 where T1.a in (select T2.a from T2) ”
32 
33 这里的“in”后面括号里的语句搜索出来的字段的内容一定要相对应,一般来说,T1和T2这两个表的a字段表达的意义应该是一样的,否则这样查没什么意义。
34 
35 打个比方:T1,T2表都有一个字段,表示工单号,但是T1表示工单号的字段名叫“ticketid”,T2则为“id”,但是其表达的意义是一样的,而且数据格式也是一样的。这时,用 2)的写法就可以这样:
36 
37 “select * from T1 where T1.ticketid in (select T2.id from T2) ”
38 
39 Select name from employee where name not in (select name from student);
40 
41 Select name from employee where not exists (select name from student);
42 
43 第一句SQL语句的执行效率不如第二句。
44 
45 通过使用EXISTS,Oracle会首先检查主查询,然后运行子查询直到它找到第一个匹配项,这就节省了时间。Oracle在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用EXISTS比使用IN通常查询速度快的原因

 

 

10、          查询没有学全所有课的同学的学号、姓名;

 

View Code
 1 SELECT T1.S#, T1.SNAME FROM STUDENT T1
 2 
 3 WHERE NOT EXISTS (SELECT S#
 4 
 5           FROM (SELECT S#, COUNT(1) CNT FROM SC GROUP BY S#) T
 6 
 7          WHERE CNT = (SELECT COUNT(1) FROM COURSE)
 8 
 9            AND T.S# = T1.S#)
10 
11 ORDER BY T1.S#

 

11、          查询至少有一门课与学号为1001的同学所学相同的同学的学号和姓名;

View Code
 1 SELECT T1.S#, T1.SNAME
 2 
 3   FROM STUDENT T1,
 4 
 5        (SELECT DISTINCT S#
 6 
 7           FROM SC
 8 
 9          WHERE C# IN (SELECT C# FROM SC WHERE S# = 1001)
10 
11            AND S# != 1001) T2
12 
13 WHERE T1.S# = T2.S#

 

12、          查询至少学过学号为1001同学所有课的其他同学学号和姓名;

View Code
 1 SELECT DISTINCT T4.S#, T4.SNAME
 2 
 3   FROM (SELECT T1.S#, T1.CNT1, T2.CNT2, COUNT(1) OVER(PARTITION BY S#) CNT
 4 
 5           FROM (SELECT S#, C#, COUNT(1) OVER(PARTITION BY S#) CNT1
 6 
 7                   FROM SC
 8 
 9                  WHERE S# != '1001'
10 
11                  GROUP BY S#, C#) T1,
12 
13                (SELECT C#, COUNT(1) OVER(PARTITION BY S#) CNT2
14 
15                   FROM SC
16 
17                  WHERE S# = '1001') T2
18 
19          WHERE T1.C# = T2.C#
20 
21            AND T1.CNT1 >= T2.CNT2) T3,
22 
23        STUDENT T4
24 
25 WHERE T4.S# = T3.S#
26 
27    AND T3.CNT >= T3.CNT2

 

13、          把SC表中叶平老师教的课的成绩都更改为此课程的平均成绩;(暂时未发现其他好办法)

 

View Code
 1 UPDATE SC SET SC.SCORE=(
 2 
 3   WITH TT AS(
 4 
 5     SELECT T.C#, AVG(T.SCORE) SCO
 6 
 7       FROM SC T
 8 
 9      WHERE T.C# IN (SELECT T1.C#
10 
11                       FROM COURSE T1, TEACHER T2
12 
13                      WHERE T1.T# = T2.T#
14 
15                        AND T2.TNAME = '叶平')
16 
17      GROUP BY T.C#

18 
19   )
20 
21   SELECT SCO FROM TT WHERE TT.C# = SC.C#
22 
23 )
24 
25 WHERE EXISTS
26 
27           (SELECT T1.C#
28 
29             FROM COURSE T1, TEACHER T2
30 
31            WHERE T1.T# = T2.T#
32 
33              AND T2.TNAME = '叶平'
34 
35              AND T1.C# = SC.C#
36 
37           )

 

14、          查询和1002号的同学学习的课程完全相同的其他同学学号和姓名;

View Code
 1 SELECT DISTINCT T4.S#, T4.SNAME
 2 
 3   FROM (SELECT T1.S#, T2.CNT2, COUNT(1) OVER(PARTITION BY S#) CNT
 4 
 5           FROM (SELECT S#, C#, COUNT(1) OVER(PARTITION BY S#) CNT1
 6 
 7                   FROM SC
 8 
 9                  WHERE S# != '1002'
10 
11                  GROUP BY S#, C#) T1,
12 
13                (SELECT C#, COUNT(1) OVER(PARTITION BY S#) CNT2
14 
15                   FROM SC
16 
17                  WHERE S# = '1002') T2
18 
19          WHERE T1.C# = T2.C#
20 
21            AND T1.CNT1 = T2.CNT2) T3,
22 
23        STUDENT T4
24 
25 WHERE T4.S# = T3.S#
26 
27    AND T3.CNT = T3.CNT2

 

15、          删除学习“叶平”老师课的SC表记录;

 

View Code
1 DELETE FROM SC T1
2 
3 WHERE EXISTS (SELECT 1
4 
5           FROM COURSE T2
6 
7          WHERE T2.T# IN (SELECT T# FROM TEACHER WHERE TNAME = '叶平')
8 
9            AND T2.C# = T1.C#)

 

16、          向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号002课程的同学学号、002、002号课的平均成绩

View Code
 1 INSERT INTO SC WITH TT AS
 2 
 3   (SELECT ROUND(AVG(SCORE), 2) AVG_SCORE FROM SC WHERE C# = '002')
 4 
 5   SELECT DISTINCT T1.S#, '002', TT.AVG_SCORE
 6 
 7     FROM SC T1, TT
 8 
 9    WHERE NOT EXISTS (SELECT T2.S#
10 
11             FROM SC T2
12 
13            WHERE T2.C# = '002'
14 
15              AND T2.S# = T1.S#)

 

17、          按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示:学生ID,,数据库,企业管理,英语,有效课程数,有效平均分;

View Code
 1 SELECT S#, ENTERPRISE_, DATABASE_, ENGLISH_, SCORE_CNT,
 2 
 3        TRUNC((NVL(ENTERPRISE_, 0) + NVL(DATABASE_, 0) + NVL(ENGLISH_, 0))/SCORE_CNT, 2) AVG_SCORE
 4 
 5 FROM
 6 
 7 (
 8 
 9   SELECT S#, ENTERPRISE_, DATABASE_, ENGLISH_,
10 
11          DECODE(ENTERPRISE_, '', DECODE(DATABASE_, '', DECODE(ENGLISH_, '', 0, 1), DECODE(ENGLISH_, '', 1, 2)), DECODE(DATABASE_, '',DECODE(ENGLISH_, '', 1, 2), DECODE(ENGLISH_,'',2, 3))) SCORE_CNT
12 
13   FROM(
14 
15     SELECT S#, SUM(ENTERPRISE_) ENTERPRISE_, SUM(DATABASE_) DATABASE_, SUM(ENGLISH_) ENGLISH_
16 
17     FROM
18 
19     (
20 
21     SELECT S#,
22 
23            CASE WHEN C# = '001' THEN SCORE
24 
25            ELSE NULL
26 
27            END ENTERPRISE_,
28 
29            CASE WHEN C# = '004' THEN SCORE
30 
31            ELSE NULL
32 
33            END DATABASE_,
34 
35            CASE WHEN C# = '006' THEN SCORE
36 
37            ELSE NULL
38 
39            END ENGLISH_
40 
41       FROM SC
42 
43      WHERE C# IN(SELECT C# FROM COURSE WHERE CNAME IN ('数据库', '企业管理', '英语'))
44 
45      )
46 
47      GROUP BY S#
48 
49    )
50 
51 );

 

18、          查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

View Code
1 SELECT C#, MAX(SCORE), MIN(SCORE) FROM SC GROUP BY C#;

 

19、          按各科平均成绩从低到高和及格率的百分数从高到低顺序;

 

View Code
 1 SELECT T1.C#,
 2 
 3          ROUND(T1.AVG_SCORE, 2) AVG_SCORE,
 4 
 5          ROUND(T2.PASS_COUNT / T1.ALL_COUNT, 2) PASS_POINT
 6 
 7     FROM (SELECT DISTINCT C#,
 8 
 9                           COUNT(1) OVER(PARTITION BY C# ORDER BY C#) ALL_COUNT,
10 
11                           AVG(SCORE) OVER(PARTITION BY C# ORDER BY C#) AVG_SCORE
12 
13             FROM SC) T1,
14 
15          (SELECT DISTINCT C#,
16 
17                           COUNT(1) OVER(PARTITION BY C# ORDER BY C#) PASS_COUNT
18 
19             FROM SC
20 
21            WHERE SCORE >= 60) T2
22 
23    WHERE T1.C# = T2.C#
24 
25    ORDER BY AVG_SCORE, PASS_POINT DESC

 

20、          查询如下课程平均成绩和及格率的百分数: 企业管理(C#为001)平均分,企业管理及格百分数,马克思平(C#为002)均分,马克思及格百分数,UML(C#为003)平均分,UML及格百分数,数据库平均分(C#为004),数据库及格百分数

View Code
 1 WITH TT AS( 
 2 
 3        SELECT C#, SCORE
 4 
 5        FROM SC
 6 
 7        WHERE C# IN (SELECT C#
 8 
 9                     FROM COURSE
10 
11                     WHERE CNAME IN ('企业管理', '马列', 'UML', '数据库'))
12 
13    )
14 
15    SELECT T1.C#,
16 
17          ROUND(T1.AVG_SCORE, 2) AVG_SCORE,
18 
19          ROUND(T2.PASS_COUNT / T1.ALL_COUNT*100, 2) || '%' PASS_POINT
20 
21     FROM (SELECT DISTINCT C#,
22 
23                           COUNT(1) OVER(PARTITION BY C# ORDER BY C#) ALL_COUNT,
24 
25                           AVG(SCORE) OVER(PARTITION BY C# ORDER BY C#) AVG_SCORE
26 
27             FROM TT) T1,
28 
29          (SELECT DISTINCT C#,
30 
31                           COUNT(1) OVER(PARTITION BY C# ORDER BY C#) PASS_COUNT
32 
33             FROM TT
34 
35            WHERE SCORE >= 60) T2
36 
37    WHERE T1.C# = T2.C#

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!