数据库查询 查询“001”课程比“002”课程成绩高的所有学生的学号

作者&投稿:尾临 (若有异议请与网页底部的电邮联系)
~

分析如下:

--1select SC1.S#  from SC SC1 JOIN SC SC2 ON SC1.S#=SC2.S#

WHERE SC1.C#='001' AND SC2.C#='002' AND SC1.score>SC2.score

--2select S#,AVG(score)

平均成绩  from SC    group by S#    

having AVG(score)>60 --3select Student.S#,

Sname,COUNT(*) 选课数,SUM(score) 总成绩 

from Student JOIN SC on Student.S#=SC.S#  

group by Student.S#,Sname

扩展资料:

数据库操作的注意事项

1、对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。

2、应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:select id from t where num is null

最好不要给数据库留NULL,尽可能的使用 NOT NULL填充数据库.

备注、描述、评论之类的可以设置为 NULL,其他的,最好不要使用NULL。

不要以为 NULL 不需要空间,比如:char(100) 型,在字段建立时,空间就固定了, 不管是否插入值(NULL也包含在内),都是占用 100个字符的空间的,如果是varchar这样的变长字段, null 不占用空间。

可以在num上设置默认值0,确保表中num列没有null值,然后这样查询:select id from t where num = 0

3、应尽量避免在 where 子句中使用 != 或 <> 操作符,否则将引擎放弃使用索引而进行全表扫描。

4、应尽量避免在 where 子句中使用 or 来连接条件,如果一个字段有索引,一个字段没有索引,将导致引擎放弃使用索引而进行全表扫描,如:select id from t where num=10 or Name = 'admin'

可以这样查询:
select id from t where num = 10
union all
select id from t where Name = 'admin'

5、in 和 not in 也要慎用,否则会导致全表扫描,如:select id from t where num in(1,2,3)

对于连续的数值,能用 between 就不要用 in 了:select id from t where num between 1 and 3

很多时候用 exists 代替 in 是一个好的选择:select num from a where num in(select num from b)

用下面的语句替换:select num from a where exists(select 1 from b where num=a.num)

6、下面的查询也将导致全表扫描:select id from t where name like ‘%abc%’

若要提高效率,可以考虑全文检索。

7、如果在 where 子句中使用参数,也会导致全表扫描。因为SQL只有在运行时才会解析局部变量,但优化程序不能将访问计划的选择推迟到运行时;它必须在编译时进行选择。然 而,如果在编译时建立访问计划,变量的值还是未知的,因而无法作为索引选择的输入项。

参考资料来源:百度百科:数据库




木垒蒙古自治州13531153953: sql数据库查询
兴胀因力: select a.student_id from course as a,course as b where a.course_id = '001' and b.course_id='002' and a.score>b.score and a.student_id = b.student_id

木垒蒙古自治州13531153953: SQL查询语句,高手进! -
兴胀因力:--1、查询“001”课程比“002”课程成绩高的所有学生的学号; select Student.Sid from (select sid,score from SC where Cid='1') Student, (select Sid,score from SC where Cid='2') Course --2、查询平均成绩大于60分的同学的学号和平均成绩; ...

木垒蒙古自治州13531153953: sql中怎么实现如下查询: 学生01学了001,002俩课程,怎么查询出和学生01所学课程完全一样的同学 -
兴胀因力: SELECT * FROM STUDENT WHERE SID IN (SELECT a.SID FROM (SELECT SID,GROUP_CONCAT(CID ORDER BY CID ) AS gc FROM SC GROUP BY SID )a WHERE a.gc = (SELECT GROUP_CONCAT(CID ORDER BY CID) FROM SC ...

木垒蒙古自治州13531153953: 一条关于sql语句的查询问题 -
兴胀因力: select a.S# from SC a ,SC b where a.S#=b.S# and a.C#='001' and b.C#='002' and a.score>b.score

木垒蒙古自治州13531153953: 数据库查询语句 -
兴胀因力: 1) 查询学生总人数 SELECT COUNT(SNo) FROM Student 2) 查询选修了课程的学生人数 SELECT COUNT(DISTINCT SNo) FROM SC 3) 计算1号课程的学生平均成绩 SELECT AVG(Score) FROM SC WHERE CNo = 1 4) 查询选修了3门以...

木垒蒙古自治州13531153953: sql server查询问题. -
兴胀因力: 1、select stu.姓名,stu.院系 from stu join scor on stu.学号=scor.学号 join cou on cou.课程号=scor.课程号 where scor.课程号='001'2、select stu.姓名,stu.院系 --想看什么字段自己填吧 你没有说 from stu join scor on stu.学号=scor.学号 join cou on ...

木垒蒙古自治州13531153953: 有关系表SC(S# , C#, Score),求“001”号课成绩比“002”号课成绩高的所...
兴胀因力: 1、select studentnub,age from student where sex ="男" 2、select coursenum,teachername from course where coursename ="数据库系统概论" 3、select studentnum,studentname from student where studentnum=(select studentnum from SC where score>80)4.update student set grade=grade+10 where coursename='计算机技术系'

木垒蒙古自治州13531153953: 怎么用SQL查询语句? -
兴胀因力: select * from table1 where 平均成绩>60 select 姓名 from table1 where 性别='0' and 学号 select * from table1 order by 编号dssc//将学好按照降序排列 select * from table1 order by 学号asc //将学号按照升序排列 use 数据e79fa5e98193e4b893e5b19...

木垒蒙古自治州13531153953: 有一张学生表 字段有:学生编号 ,学生成绩,科目 sql怎么查询科目一成绩比科目二高的 学生编号 -
兴胀因力: SELECT a.编号 FROM( SELECT 编号,成绩 FROM student WHERE 科目='科目一')AS a LEFT JOIN ( SELECT 编号,成绩 FROM student WHERE 科目='科目二')AS b ON a.编号=b.编号 WHERE a.成绩>b.成绩

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网