教学思路SQL之入门习题《学员成绩》 三、多表复杂子查询

简介:
1、查询出所有大连地区的学生的成绩。
2、查询从来没有参加任何考试的学生的信息。
3、查询有考试成绩纪录的学生的信息.
4、将所有大连地区学生的课程编号为3的课程成绩加10分。
5、删除所有C#课的成绩。
6、查询课程编号为3的课程的平均成绩,显示有此门课程成绩的学生编号,成绩及距平均分之间的差值。
7、查询参加了所有考试的学生的姓名。
8、查询有成绩纪录的学生人数。
9、查询有2门以上成绩的学生的姓名和联系方式
10、查询哪些学生有c#考试成绩,显示这些学生的姓名
11.查询出科目中教师的姓名,同时增加编号。
12.随机显示学生信息表中的两条数据:使用newid()函数
13.查询出联系电话为null的学生信息,联系电话用‘无’代替。
14.将查询出的地区为大连的都显示为null.
15.查询科目平均成绩超过70分的教师姓名。
新知识:
对空值进行显示操作:
  • isnull(A,‘aa’)函数可以将空值转换为有效的值。将列名A的值null改为‘aa’显示
  • nullif(A,‘aa’)函数可以根据指定的条件来生成空值。将列名A的值aa改为null显示
newid函数:
  •      
    使用newid()函数会产生随机行 
答案
1、查询出所有大连地区的学生的成绩。 
        select s.number,c.score    from chengji as c ,student as s where c.number=s.number and s.diqu='大连' 
        select * from chengji where number inselect number from student where diqu='大连'

2、查询从来没有参加任何考试的学生的信息。 
select * from student where number not in (select number from chengji) 
3、查询有考试成绩纪录的学生的信息. 
select * from student where number    in (select number from chengji) 
4、将所有大连地区学生的课程编号为3的课程成绩加10分。 
update chengji set score=score+10 where scorenumber=3 
5、删除所有C#课的成绩。 
delete chengji where scorenumber=(select scorenumber from score where kemu='C#'
6、查询课程编号为3的课程的平均成绩,显示有此门课程成绩的学生编号,成绩及距平均分之间的差值。 
select number,score-(select avg(score) from chengji where scorenumber=3) from chengji where scorenumber=3 
7、查询参加了所有考试的学生的姓名。 
select name from student where number inselect max(number) from chengji group by number) 
8、查询有成绩纪录的学生人数。 
    selectnumber ,name from student where number in 
select max(number) from chengji     group by number having count(scorenumber)=(select count(*) from score) ) 
9、查询有2门以上成绩的学生的姓名和联系方式。 
select name,lianxidianhua from student where number inselect number from chengji group by number having count(scorenumber)>2) 
10、查询哪些学生有c#考试成绩,显示这些学生的姓名。 
select name from student where number inselect number from chengji where scorenumber =(select scorenumber from score where kemu='c#')) 
给结果集添加编号 
11.查询出科目中教师的编号,同时增加序号。 
        select 序号=(select count(*) from score as a where a.scorenumber<=b.scorenumber ),teacherid    from score as b 
        select 序号=identity(1,1),teachername into #newtable from score 
12.随机显示学生信息表中的两条数据:使用newid()函数 
select top 2 * from student    order by newid() 
13.查询出联系电话为null的学生信息,联系电话用‘无’代替。 
select name,isnull(convert(char(20),lianxidianhua),'无'from student     
14.将查询出的地区为大连的都显示为null 
select name,nullif(diqu,'大连'from student 
15.查询科目平均成绩超过70分的教师姓名。 
select teachername from teacherinfo where teacherid in    
(select teacherid from score where scorenumber in 
(select scorenumber from chengji group by scorenumber having avg(score)>=70))
本文转自叶子文文博客51CTO博客,原文链接http://blog.51cto.com/leafwf/185781如需转载请自行联系原作者

叶子文文
相关文章
|
1天前
|
SQL Java 数据库连接
Java从入门到精通:2.3.2数据库编程——了解SQL语言,编写基本查询语句
Java从入门到精通:2.3.2数据库编程——了解SQL语言,编写基本查询语句
|
6天前
|
SQL 自然语言处理 数据库
NL2SQL实践系列(2):2024最新模型实战效果(Chat2DB-GLM、书生·浦语2、InternLM2-SQL等)以及工业级案例教学
NL2SQL实践系列(2):2024最新模型实战效果(Chat2DB-GLM、书生·浦语2、InternLM2-SQL等)以及工业级案例教学
NL2SQL实践系列(2):2024最新模型实战效果(Chat2DB-GLM、书生·浦语2、InternLM2-SQL等)以及工业级案例教学
|
2月前
|
SQL XML 数据库
SQL 入门
SQL 入门
|
2月前
|
SQL 人工智能 运维
数据库基础入门 — SQL排序与分页
数据库基础入门 — SQL排序与分页
25 0
|
2月前
|
SQL 人工智能 运维
数据库基础入门 — SQL运算符
数据库基础入门 — SQL运算符
23 0
|
2月前
|
SQL 人工智能 运维
数据库基础入门 — SQL
数据库基础入门 — SQL
35 0
|
3月前
|
SQL Java 关系型数据库
JDBC技术【SQL注入、JDBC批量添加数据、JDBC事务处理、其他查询方式】(三)-全面详解(学习总结---从入门到深化)
JDBC技术【SQL注入、JDBC批量添加数据、JDBC事务处理、其他查询方式】(三)-全面详解(学习总结---从入门到深化)
33 0
|
3月前
|
SQL Oracle 关系型数据库
七、SQL子查询
七、SQL子查询
32 0
|
14天前
|
SQL 人工智能 算法
【SQL server】玩转SQL server数据库:第二章 关系数据库
【SQL server】玩转SQL server数据库:第二章 关系数据库
52 10
|
1月前
|
SQL 数据库 数据安全/隐私保护
Sql Server数据库Sa密码如何修改
Sql Server数据库Sa密码如何修改