数据库 sql sqlserver导出数据库 T-SQL 请问这道题怎么做

SQL数据库第一题怎么写程序。。速度速度。-中国学网-中国IT综合门户网站-提供健康,养生,留学,移民,创业,汽车等信息
> 信息中心 >
SQL数据库第一题怎么写程序。。速度速度。
来源:互联网 发表时间: 2:26:56 责任编辑:鲁晓倩字体:
为了帮助网友解决“SQL数据库第一题怎么写程序。。速度速度。”相关的问题,中国学网通过互联网对“SQL数据库第一题怎么写程序。。速度速度。”相关的解决方案进行了整理,用户详细问题包括:RT,我想知道:SQL数据库第一题怎么写程序。。速度速度。,具体解决方案如下:解决方案1:
group by name order by 学号 desc顺便评论下楼上的;张%'张%&#39,一楼的没有写条件 like &#39,应该根据学号排序,并且没有分组,执行必然失败 二楼的排序条件不对;, name as 学生姓名 from tableName where name like &#39select avg(成绩) as 平均成绩
追问:解决方案2:
数据表在哪里?select Avg(学生成绩.成绩A +[成绩B] + [成绩C] + [成绩D] + [成绩E]),学生姓名 from 表 order by 学号 desc
解决方案3:
Select avg(score) ascore,name from student where name like '张%' group by name order by 学号
解决方案4:
select 学号,姓名,avg(分数)平均分 from 表 where 姓名= like '张%' group by学号, 姓名 order by 学号 desc
解决方案5:
select name,avg(score) from table_name where name like '张%' order by 学号
解决方案6:
没得表结构,怎么给你写查询语句?把表结构贴出来
1个回答1个回答1个回答1个回答1个回答1个回答1个回答1个回答1个回答1个回答
相关文章:
最新添加资讯
24小时热门资讯
Copyright © 2004- All Rights Reserved. 中国学网 版权所有
京ICP备号-1 京公网安备02号sqlserver数据库编程题,求答案_百度知道怎样使用SQL SERVER新建立一个数据库_百度知道君,已阅读到文档的结尾了呢~~
SQL Server
试题 数据库数据表基本操作部分
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
SQL Server
试题 数据库、表基本操作部分
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口2918人阅读
习题之前,我们先建立了11张表:
create table classroom
varchar(15),
room_number
varchar(7),
numeric(4,0),
primarykey (building, room_number)
create table department
(dept_name
varchar(20),
varchar(15),
numeric(12,2) check (budget & 0),
primarykey (dept_name)
create table course
(course_id
varchar(8),
varchar(50),
varchar(20),
numeric(2,0) check (credits & 0),
primarykey (course_id),
foreignkey (dept_name) references department
on deleteset null
create table instructor
varchar(5),
varchar(20) not null,
varchar(20),
numeric(8,2) check (salary & 29000),
primarykey (ID),
foreignkey (dept_name) references department
on deleteset null
create table section
(course_id
varchar(8),
varchar(8),
varchar(6)
check (semester in('Fall', 'Winter', 'Spring', 'Summer')),
numeric(4,0) check (year & 1701 and year & 2100),
varchar(15),
room_number
varchar(7),
time_slot_id
varchar(4),
primarykey (course_id, sec_id, semester, year),
foreignkey (course_id) references course
on deletecascade,
foreignkey (building, room_number) references classroom
on deleteset null
create table teaches
varchar(5),
varchar(8),
varchar(8),
varchar(6),
numeric(4,0),
primarykey (ID, course_id, sec_id, semester, year),
foreignkey (course_id,sec_id, semester, year) references section
on deletecascade,
foreignkey (ID) references instructor
on deletecascade
create table student
varchar(5),
varchar(20) not null,
varchar(20),
numeric(3,0) check (tot_cred &= 0),
primarykey (ID),
foreignkey (dept_name) references department
on deleteset null
create table takes
varchar(5),
varchar(8),
varchar(8),
varchar(6),
numeric(4,0),
varchar(2),
primarykey (ID, course_id, sec_id, semester, year),
foreignkey (course_id,sec_id, semester, year) references section
on deletecascade,
foreignkey (ID) references student
on deletecascade
create table advisor
varchar(5),
varchar(5),
primarykey (s_ID),
foreignkey (i_ID) references instructor(ID)
on deleteset null,
foreignkey (s_ID) references student(ID)
on deletecascade
接下来我们再往各张表里插入数据(代码略)后,我们来看看11张表的信息:
classroom: & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&department:
&& & & & & &&
& & & & & & &&
course: & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&instructor:
&&&&& & & & &
student: & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&advisor:
& & & & & & & & & & & &&
下面是题目:
(Recommendation: With clause is strongly recommended for simplifying the query.)
1. Find the sections which have the minimum enrollment among sections registered by students. For each section as such, information displayed should involve:
① Identifier of section(i.e. the primary key for section)
②Course name corresponding to the section
③Enrollment of the section('enrollment' is the alias for the number of students who registered for the section)
④TOP keyword in SQL Server is denied.
翻译一下,这道题的意思是:
找出section中被学生注册数最少的section。连带的输出的信息有section的ID等主键,coursename,注册数。另外,不准使用TOP keyword.
(强烈建议使用with语句,这样会方便很多)
我的答案是:
with sec_enroll(course_id,sec_id,semester,year,enrollment) as
( select course_id,sec_id,semester,YEAR,COUNT(id)
from takes
group by course_id,sec_id,semester,year
min_enroll(course_id,sec_id,semester,year,title,enrollment) as
( select s.course_id,s.sec_id,s.semester,s.year,course.title,enrollment
from sec_enroll s,course
course.course_id=s.course_id
group by s.course_id,s.sec_id,s.semester,s.year,course.title,enrollment
select s.course_id,s.sec_id,s.semester,s.year,s.title,enrollment
from min_enroll s
where s.enrollment&=all(select enrollment from min_enroll)
group by s.course_id,s.sec_id,s.semester,s.year,s.title,enrollment
运行的结果为:
其中使用了两个with语句,第一个with定义了sec_enroll临时关系,选取出takes表中的course_id,sec_id,semester,year,另外number为count(id)用来记录选择某个特定section的人数。
第二个with定义了min_enroll临时关系,来自sec_enroll,course这两张表,将course表中的title加入。(其实我们知道,这张表主要是为了第14行表达的方便)
最后的查询中,用where s.enrollment&=all(select enrollment frommin_enroll)控制筛选使得enrollment最小,从而输出符合题意的section信息。
我们再看第二道题。
USE aggregation on outer join to construct the following query
For all students, list the registration information of the students. The students who have never registered for any courses should also be considered.In the case, the aggregative information
of such students should be set to 0.For each student, information displayed&should involve:
① Identifier of student(i.e. the primary key for student)
② Name of the student
③ Number of course registrations(Caution: Not the number of section registrations. E.g. ,student A registered the course B twice in 2 sections, the number of course registrations is 1
and the number of section registrations is 2).
④Number of section registrations
⑤TOP keyword in SQL Server is denied.
这道题的意思是:
列出所有学生的选课信息,那些一门课都没选的学生的选课信息同样要求被输出。这些学生的信息应该被置为0。对于每个学生,应包含的信息有学生的ID等主键,姓名,选课的课程数(不是section数),选课的section数。
由题意我们知道,肯定需要使用外连接。另外使用count函数即可。
select student.ID,student.name,count(distinct course_id) as course_num,count(course_id) as sec_num
from student left outer join takes on student.ID=takes.ID
group by student.ID,student.name
输出的结果:
注意:第一行中的distinct不可少!这是很容易犯的错误,比如某人选择了一门course的两次section,在总表中显示的是两行,如果不加distinct那么输出的course_num为2!而原本应该输出的是1!sec_num输出2是正确的,不要加distinct。
第三道题:
USE scalar subquery to construct the following query
Find the information for the instructors who have taught more than 1course (that is,he/she should have taught 2 distinct courses as least). For eachinstructor as such,information displayed
should involve:
① Identifier of instructor(i.e. theprimary key for instructor)
② Name of the instructor
③Average salary of the department forwhich the instructor works
④ Sum of credit points taught by theinstructor(for example, if instructor A has taught course A (2 credit points)twice, course B(3 credit points) once, then the sum of credit points taught
byinstructor A is 7)
题目的意思是:
(推荐使用标量子查询)
查找教的课程(课程不同)大于1门的导师的信息,需要包含的信息有ID等导师的主键,姓名,导师所在的系的平均薪水,这个导师教的学分总数。
我的答案:
with dept_avgsal(dept_name,avgsal)as
(select instructor.dept_name,AVG(salary)
from department,instructor
where department.dept_name=instructor.dept_name
group by instructor.dept_name
ins_credits(id,tot_credits)as
(select id,SUM(credits)
from course,teaches
where course.course_id=teaches.course_id
group by ID)
select instructor.ID,name,
(select avgsal
from dept_avgsal
where instructor.dept_name=dept_avgsal.dept_name) as avgsal,
(select tot_credits
from ins_credits
where instructor.ID=ins_credits.id) as tot_credits
from instructor left outer join teaches on (instructor.ID=teaches.ID)
group by instructor.ID,name,instructor.dept_name
having COUNT(distinct course_id)&1
这道题我用了with构建了两个临时关系,分别用来处理“导师所在的系的平均薪水,这个导师教的学分总数”这两个问题,这样处理可以让嵌套的层数减少,更清晰。当然也可以不用with,直接用标量子查询,一层层的进行嵌套也可以得出答案(后面代码即是)。
此处有个地方没弄明白,第21行里,如果不加instructor.dept_name的话,为什么会提示这样的错误呢:
另外注意,第16和第19行中,可以直接用外面from语句中的表。
附上学长的代码,没有用with语句,嵌套标量子查询:
select ins.ID,ins.name,
(select AVG(salary)
from instructor
where dept_name in
(select dept_name from instructor
where ID=ins.id)) as avg_sal,
(select SUM(credits)
from teaches join course on teaches.course_id=course.course_id
where teaches.ID=ins.ID) as cred_sum
from teaches join instructor as ins on ins.ID=teaches.ID
group by ins.ID,ins.name
having COUNT(distinct course_id)&1
第四道题:
4. Find students who have registered forsome but not allcourses taught by instructors of department '内功学院'.
Do this using the &not exists ... except...& structure. For each student as such, information displayed should involve:
① Identifier of student(i.e. the primary key for student)
②Name of the student
③Number of courses, taught by instructors of department '内功学院', registered by the student
翻译过来,这道题让我们找的是“选择了拳脚学院的部分课程的学生”。要求使用”not exists…except…”结构。对于每个输出的学生,包括的信息有学生ID等主键,姓名,被这个学生选的拳脚学院老师教的课程数。
实现代码:
select student.ID,student.name,COUNT(distinct takes.course_id) as course_num
from student join takes on student.ID=takes.ID
join teaches on takes.course_id=teaches.course_id
and takes.sec_id=teaches.sec_id
and takes.semester=teaches.semester
and takes.year=teaches.year
join instructor on teaches.ID=instructor.ID
where instructor.dept_name='内功学院' and student.ID not in (
from student S
where not exists(
select course_id
from course
where course.dept_name='内功学院'
select T.course_id
from takes as T
where T.ID=S.id
group by student.ID,student.name
1.&&&&&此处我们用join形式来连接4个表,on后面的是连接表之间的关联键值。(等同于使用from …再用where控制条件连接多张表)
2.&&&&&另外,not…exists…except语句用来表示all这样的关系,本题整体的思路是将所有选内功学院课程的学生减去选了所有内功学院课程的学生。一个定理:“关系A包含关系B”写成”not exists(B except A)”,运用这个定理可以实现全称量词的选择。
例:本题中
select course_id
&&&&&& & from
&&&&&& & where
course.dept_name='内功学院’表示内功学院开的所有课程的集合;
select T.course_id
&&& &&&&& from
takes as T
&&&&&& & where
T.ID=S.id表示S.id选的所有课程。这样,整个not…exists…except语句即表示选了内功学院开的所有课程的学生。
3. &最后一行groupby容易漏掉,可是它必不可少。
另外贴上我的另外一份代码,为什么报错呢?
select student.ID,student.name,(select COUNT (distinct takes.course_id)
from student,takes
where student.ID=takes.ID and course_id in (
select course_id
from course
where course.dept_name='内功学院'
group by student.ID
from student,takes,teaches
where takes.course_id=teaches.course_id and takes.sec_id=teaches.sec_id and takes.semester=teaches.semester and takes.year=teaches.year
and student.ID=takes.ID and teaches.ID in (select ID
from instructor
where instructor.dept_name='内功学院')
select S.ID,S.name,(select COUNT (distinct takes.course_id)
from student,takes
where student.ID=takes.ID and course_id in (
select course_id
from course
where course.dept_name='内功学院'
group by student.ID
from student as S
where not exists((select course.course_id
from course
where dept_name='内功学院')
(select T.course_id
from takes as T
where S.ID=T.ID
--select *
--from takes,teaches
--where takes.course_id=teaches.course_id and takes.sec_id=teaches.sec_id and takes.semester=teaches.semester and takes.year=teaches.year
提示错误:
第五道题:
5. As query requirement in Q4, Use matching of counts to fulfill the requirement. (don't forget the distinct clause!).
翻译过来的意思是:用比较的方法做第四题,别忘了distinct语句。
代码大致差不多,用having语句增加比较条件,筛选出符合题意的对象。请读者自行揣摩。
参考代码:
select student.ID,student.name,COUNT(distinct takes.course_id) as curs_cnt
from student join takes on student.ID=takes.ID
join teaches on takes.course_id=teaches.course_id
and takes.sec_id=teaches.sec_id
and takes.semester=teaches.semester
and takes.year=teaches.year
join instructor on teaches.ID=instructor.ID
where instructor.dept_name='内功学院'
group by student.ID,student.name
having COUNT(distinct takes.course_id)&(select COUNT(distinct course_id)
from teaches join instructor on instructor.ID=teaches.ID
where instructor.dept_name='内功学院')
总之,写SQL查询语句必须熟悉各张表之间的联系,另外要不停的试,加以对语法的掌握,才能游刃有余的完成任务。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:9118次
排名:千里之外

我要回帖

更多关于 sql server数据库下载 的文章

 

随机推荐