分享整理的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);
作者 人在江湖


猜你喜欢
- 如果你是个赛车手,并且按一下按钮就能够立即更换引擎而不需要把车开到车库里去换,那会是什么感觉呢?MySQL数据库为开发人员所做的就好像是按按
- 一、安装redis:1.下载:wget http://download.redis.io/releases/redis-3.2.8.tar.
- 建表CREATE TABLE `map` ( `id` int(11) NOT NULL, `address` varchar(255) N
- 一、Less/Scss变量换肤具体实现:1、初始化vue项目2、安装插件:npm install style-resources-loade
- 本文实例讲述了PHPExcel冻结(锁定)表头的简单实现方法。分享给大家供大家参考,具体如下:PHPExcel是一款功能比较强大的操作微软e
- 第七步: 在自定义分页的Repeater 里添加排序功能现在已经完成了自定义分页,我们再来添加排序功能。ProductsBLL类的GetPr
- 整数的阶乘(英语:factorial)是所有小于及等于该数的正整数的积,0的阶乘为1。即:n!=1×2×3×...×n。首先导入math模块
- 删除字段从Model中删除一个字段要比添加容易得多。 删除字段,仅仅只要以下几个步骤: 删除字段,然后重
- TensorFlow是一款优秀的深度学习框架,支持多种常见的操作系统,例如Windows10,Mac Os等等,同时也支持运行在NVIDIA
- 本文实例讲述了Go语言使用sort包对任意类型元素的集合进行排序的方法。分享给大家供大家参考。具体如下:使用sort包的函数进行排序时,集合
- 由于python内部的变量其实都是reference,而Tensorflow实现的时候也没有意义去判断输出是否是同一变量名,从而判定是否要新
- 目录1. 配置Python环境变量2. 安装Python编辑器,并在其中配置Python3. 安装控制包uiautomator2,和其它辅助
- Python矩阵的基本用法mat()函数将目标数据的类型转化成矩阵(matrix)1,mat()函数和array()函数的区别Numpy函数
- 环境:1 .Windows Server 2016 Datacenter 64位2 .SQL Server 2016 Enterprise
- 随机漫步生成是无规则的,是系统自行选择的结果。根据设定的规则自定生成,上下左右的方位,每次所经过的方向路径。首先,创建一个RandomWal
- DataFrame是一个组织成命名列的数据集。它在概念上等同于关系数据库中的表或R/Python中的数据框架,但其经过了优化。DataFra
- 出于工作需要,学习了GAN,原理这块就不多讲了,主要讲怎么训练自己的数据生成新的图片,因为博客上大多是生成MNIST数据集,生成自己的图片时
- 有时候难免会要用到只读的文本框,可今天发现只读文本框有一个缺陷,当鼠标焦点在文本框里面的时候按回退键(backSpace), 会退回到前一个
- 前言SQLSERVER 2005中不知因何去掉了很重要的DEBUGGER功能,要调试,必须要安装VS2005专业版或者更高版本。非常不方便。
- 代码是这样的:var reg = /^1[345678][0-9]{9}$/g;console.log(reg.test(153280446