sql查询语句练习

发布 2021-05-08 20:02:28 阅读 4679

解析:(select s#,score from sc where c#='001') a//从sc中查询c#=001的学生学号和分数,并定义为a表。

select s#,score from sc where c#='002') b //从sc中查询c#=002的学生学号和分数,并定义为b表。

where > and 条件,a表与b表中学号相同且分数比b中的高。

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

select s#,**g(score) rom sc

group by s# h**ing **g(score) >60;

解析: group by s# h**ing **g(score) >60 这里是指平均分数大于60分的学号。使用h**ing 子句原因是,where 关键字无法与合计函数一起使用。

合计函数 (比如 sum) 常常需要添加 group by 语句,根据一个或多个列对结果集进行分组。

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

select

from student left outer join sc on

group by

解析: student left outer join sc on 左外连接,从student表返回所有的行放于左表中,从sc中返回的与左表匹配的行放于右表。student表中有但sc表中没有的在右表中对应行留空。

group by 以 分组。

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

select count(distinct(tname))

from teacher

where tname like '李%';

解析:distinct(tname) 在表中,可能会包含重复值。distinct 用于返回唯一不同的值,这里的作用就是防止返回的tname出现重复值。

where tname like '李%' 模糊查询,"%代表一或多个字符 。

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

select

from student

where s# not in (select distinct( from sc,course,teacher where and and '叶平');

解析:not in 题目是查询没学过“叶平”老师课的同学的学号、姓名,这里使用not in 就表示所要查询的s#不在后面的结果中。

select distinct( from sc,course,teacher where and and '叶平');

这段语句是查询学过“叶平”老师课的同学的学号,这里其实查询的sc表,但可看到查的表是sc,course,teacher表,根据后面的条件语句,用来关联查询的。三个条件要同时满足,从而查询出“叶平”老师课的同学的学号。

由于sc与teacher并无直接联系,这里将sc与teacher表联系起来, '叶平':教师名为叶平,从而对应到sc表。

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

select from student,sc where and '001'and exists( select * from sc as sc_2 where sc_ and sc_'002');

解析:select from student,sc where and '001' 这一句查询到的是学过“001”课程的同学的学号、姓名;

and exists( select * from sc as sc_2 where sc_ and sc_'002');

使用 exists关键字,用来判断是否存在的,当exists(查询)中的查询结果存在时则返回真,否则返回假。这里指查询学过002课程的同学,使用 and则将得到的结果与上一句匹配得上就都为真,返回结果。若不匹配,则不返回结果。

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

select s#,sname

from student

where s# in (select s# from sc ,course ,teacher where and and '叶平' group by s# h**ing count( count(c#) from course,teacher where and tname='叶平'))

解析:这一道题与第5题接近,只是答案后面多了:group by s# h**ing count( count(c#) from course,teacher where and tname='叶平')。

group by...h**ing..的句式我们已经知道是因为where 关键字无法与合计函数一起使用。

合计函数 (比如 sum) 常常需要添加 group by 语句,根据一个或多个列对结果集进行分组。

接下来就是理解count( count(c#) from course,teacher where and tname='叶平')的含义了,count(是基于前面条件叶平老师在sc表中课程号的数量,select count(c#) from course,teacher where and tname='叶平',是从course中查询叶平老师的所有课程号数量。

这里条件是说course中叶平老师的课程数与成绩表中的课程数相等。(sb我也不知为什么这么写)

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

select s#,sname from (select ,(select score from sc sc_2 where sc_ and sc_'002') score2

from student,sc where and c#='001') s_2 where score2 解析: score as score2 from sc sc_2 where sc_ and sc_'002';,score2 as s_2 from student,sc where and c#

s#,sname from s_2 where score2 score2在c#='001'的条件中可select,score包含score2?

SQL查询语句练习

1 查询student表中的所有记录的sname ssex和class列。语句 select sname,ssex,class from student 查询结果 2 查询教师所有的单位即不重复的depart列。语句 select distinct depart from teacher 查询结果 ...

SQL语句查询练习

1.检索出课程表中所有信息所有字段,查询名为 sql课程表查询 2.检索出学生来自于那些民族,只显示 民族 字段,要求消除重复行,查询名为 sql民族查询 3.检索出 学号 课程号 及 总评成绩 字段,并按总评成绩降序排列,查询名为 sql成绩查询 4.检索出1993年出生的学生的 姓名 性别 和 ...

sql查询语句练习

student s sname,sage,ssex 学生表 course c cname,t 课程表 sc s c score 成绩表 teacher t tname 教师表。问题 1 查询 001 课程比 002 课程成绩高的所有学生的学号 2 查询平均成绩大于60分的同学的学号和平均成绩 3 查...