分享整理的12条sql语句连同数据(2)
来源:asp之家 发布时间:2012-07-11 16:14:59
标签:sql语句,连同数据
整理的sql在下面:
Student(S#,Sname,Sage,Ssex) 学生表
Course(C#,Cname,T#) 课程表
SC(S#,C#,score) 成绩表
Teacher(T#,Tname) 教师表
1. 选出每门功课都及格的学号
select distinct `s#` from sc where `s#` not in (select `s#` from sc where score <60)
2. 查询“1”课程比“2”课程成绩高的所有学生的学号;
SELECT c01.`s#` from (select `s#`, `score` from sc where `c#`=1) c01,
(select `s#`, `score` from sc where `c#`=2) c02
where c01.`s#` = c02.`s#` and c01.score > c02.score
3. 查询平均成绩大于60分的同学的学号和平均成绩;
select `s#`, avg(score) from sc group by `s#` having avg(score) > 60
4. 查询所有同学的学号、姓名、选课数、总成绩;
select student.`s#`, student.`Sname`, count(`c#`), sum(score) from student left outer join sc on student.`s#` = sc.`s#` group by student.`s#`, sc.`s#`
5.查询没学过“叶平”老师课的同学的学号、姓名;
select student.`s#`, student.`Sname` from student where student.`s#` not in (select distinct(sc.`s#`) from teacher, course, sc where Tname='叶平' and teacher.`t#` = course.`t#` and sc.`c#`= course.`c#` )
6. 查询学过“001”并且也学过编号“002”课程的同学的学号、姓名
select student.`s#`, student.sname from student, sc where student.`s#` = sc.`s#` and sc.`c#` = 1 and exists (select * from sc sc_2 where sc_2.`c#`=2 and sc.`s#`=sc_2.`s#`)
7. 查询学过“叶平”老师所教的所有课的同学的学号、姓名 (巧妙)
select `s#`, sname from student where `s#` in
(select `s#` from sc, teacher, course where tname='叶平' and teacher.`t#`=course.`t#` and course.`c#`= sc.`c#` group by `s#` having count(sc.`c#`)=
(select count(`c#`) from teacher, course where tname='叶 平' and teacher.`t#`=course.`t#`) )
8. 查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名 (有代表性)
select `s#`, sname from (select student.`s#`, student.sname, score, (select score from sc sc_2 where student.`s#`=sc_2.`s#` and sc_2.`c#`=2) score2 from student , sc where
sc.`s#`=student.`s#` and sc.`c#`=1) s_2 where score2 < score
9.查询没有学全所有课的同学的学号、姓名
select student.`S#`, Sname from student, sc where student.`s#` = sc.`s#` group by `s#`, sname having count(`c#`) < (select count(`c#`) from course)
10. 查询至少有一门课与学号为“002”的同学所学相同的同学的学号和姓名;
select distinct(sc.`s#`), sname from student, sc where student.`s#`=sc.`s#` and `c#` in (select `c#` from sc where `s#`=002)
11. 把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
update sc inner join
(select sc2.`c#`, avg(sc2.score) score from sc sc2, teacher, course where
sc2.`c#`=course.`c#` and tname='叶平' and teacher.`t#` = course.`t#` and course.`c#`=sc2.`c#` group by course.`c#`) sc3 on sc.`c#`=sc3.`c#` set sc.score=sc3.score
12. 查询2号的同学学习的课程他都学了的同学的学号;(注意理解:where语句的 第一个条件过滤掉不满足c#的记录,再group by,就比较清晰)
select `S#` from SC where `C#` in (select `C#` from SC where `S#`=2)
group by `S#` having count(*)=(select count(*) from SC where `S#`=2);
作者 人在江湖
0
投稿
猜你喜欢
- 在《JavaScript语言精粹》的第72页有这样一段:用正则表达式字面量创建的RegExp对象来共享同一个单实例:function mak
- 1、简单的按钮js事件 用于判断和显示提示 <script type="text/javascript&
- 数据库连接字符串的常见问题和解决方法:基本知识1:SQL Server数据库的身份验证方式,分windows验证,SQL Server验证两
- 1. 鼠标的哪个按键被点击?<html><head><script type="text/javas
- 在浏览器 IE6 、IE7、Firefox2+、Firefpx3+、Opera9.6+、Safari3.1+中测试以下代码:<!DOC
- 随着手机用户的不断增加,WAP站点如雨后春笋迅速的滋长开来,手机邮箱也不断的出现在人的眼前,笔者也曾经开发了一套手机邮箱的系统,但由于时间仓
- 测试sql: 代码如下:SET STATISTICS IO ON SET STATISTICS TIME ON SELECT COUNT(1
- a {}{ background:url(images/normal.gif); } a:ho
- 今天闲逛在网上时,看到一个11px大小的字体,显示却很清晰,赶紧查看站点的CSS,这字体称叫做:PMingLiu。效果相当不错,相比于我们使
- 在asp里通过以下两个函数实现javascript里的escape函数和unescape函数
- 出差到了中国雅虎,这里的风格和淘宝很不一样。和雅虎一比,淘宝的办公环境就是个菜市场,闹哄哄,到处是人,在走道里狂奔乱窜,在每个会议室争得面红
- session请求过程当第一次访问网站时,Seesion_start()函数就会创建一个唯一的Session ID,并自动通过HTTP的响应
- MySQL安全性指南(3) 作 者: 晏子2.4 不用GRANT设置用户如果你有一个早于3.22.11的MySQL版本,你不能使用GRANT
- SQL Server 的扩展存储过程,其实就是一个普通的 Windows DLL,只不过按照某种规则实现了某些函数而已。近日在写一个扩展存储
- HTML 5基本思维概念形成于2003年,之后W3C对页面超文本应用技术工作小组(WHATWG)开发的HTML草图颇感兴趣,这个小组的开发人
- 刚才运行了一段代码,来查看Request.ServerVariables里面有多少值,看了一下,共50个!代码<%=Request.S
- 在使用Ajax过程中,有时候总会遇到一些难题,浏览器兼容、编码、IE下的特殊处理等等,偶尔会搞的人头昏脑胀哭笑不得,这里列一些小贴士,或许有
- 我插入Mysql5的中文一直是乱码。但是直接使用mysqlAdmin,EMS等工具插入DB就不是乱码。而且我还可以使用程序正常地读出来。原因
- 下面代码即是VBScript代码在服务器端编译后的显示内容,如果我们把这段代码保存成静态文件(HTML)或JS文件,那么上一篇提出的问题就迎
- 代码如下: Function closeHTML(strContent) Dim arrTags, i, OpenPos, ClosePos