50个常用的SQL语句(答案).doc

上传人:ni****g 文档编号:551332642 上传时间:2023-04-20 格式:DOC 页数:5 大小:52.01KB
返回 下载 相关 举报
50个常用的SQL语句(答案).doc_第1页
第1页 / 共5页
50个常用的SQL语句(答案).doc_第2页
第2页 / 共5页
50个常用的SQL语句(答案).doc_第3页
第3页 / 共5页
50个常用的SQL语句(答案).doc_第4页
第4页 / 共5页
50个常用的SQL语句(答案).doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《50个常用的SQL语句(答案).doc》由会员分享,可在线阅读,更多相关《50个常用的SQL语句(答案).doc(5页珍藏版)》请在金锄头文库上搜索。

1、基本信息Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC where C#=001) a,(select s#,score from SC where C#=002) b where a.scoreb.score and a.s#=b.s#; 2、查询平均成绩大于60分的同学的学号和平均成绩; se

2、lect S#,avg(score) from sc group by S# having avg(score) 60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname 4、查询姓“李”的老师的个数; select count(distinct(Tname) from Teacher where Tname like 李%; 5、

3、查询没学过“叶平”老师课的同学的学号、姓名; select Student.S#,Student.Sname from Student 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=叶平); 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; select Student.S#,Student.Sname from Student,SC where Student.

4、S#=SC.S# and SC.C#=001and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#=002); 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; select S#,Sname from Student where S# in (select S# from SC ,Course ,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname=叶平 group by S# having count(SC.C

5、#)=(select count(C#) from Course,Teacher where Teacher.T#=Course.T# and Tname=叶平); 8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名; 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 from Student,SC where Student.S#=SC.S#

6、 and C#=001) S_2 where score2 60); 10、查询没有学全所有课的同学的学号、姓名; select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# group by Student.S#,Student.Sname having count(C#) =60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分数 FROM SC T,Course where t.C#=course.C# GROUP BY t.C# ORDER BY 100 * SUM(CASE WHE

7、N isnull(score,0)=60 THEN 1 ELSE 0 END)/COUNT(*) DESC 20、查询如下课程平均成绩和及格率的百分数(用1行显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004) SELECT SUM(CASE WHEN C# =001 THEN score ELSE 0 END)/SUM(CASE C# WHEN 001 THEN 1 ELSE 0 END) AS 企业管理平均分 ,100 * SUM(CASE WHEN C# = 001 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASE

8、 WHEN C# = 001 THEN 1 ELSE 0 END) AS 企业管理及格百分数 ,SUM(CASE WHEN C# = 002 THEN score ELSE 0 END)/SUM(CASE C# WHEN 002 THEN 1 ELSE 0 END) AS 马克思平均分,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 马克思及格百分数 ,SUM(CASE WHEN C# = 003 THEN score ELSE

9、 0 END)/SUM(CASE C# WHEN 003 THEN 1 ELSE 0 END) AS UML平均分 ,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及格百分数 ,SUM(CASE WHEN C# = 004 THEN score ELSE 0 END)/SUM(CASE C# WHEN 004 THEN 1 ELSE 0 END) AS 数据库平均分 ,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 数据库及格百分数 FROM SC 21、查询不

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号