SQL Server笔试题 解答

来源:百度文库 编辑:神马文学网 时间:2024/04/26 07:43:59
SQL Server笔试题(Sql2000常见笔试题)
一 单词解释(2分/个) 34分
Data 数据 Database 数据库 RDBMS 关系数据库管理系统 GRANT 授权
REVOKE 取消权限 DENY 拒绝权限 DECLARE 定义变量 PROCEDURE存储过程
事务 Transaction 触发器 TRIGGER 继续  continue 唯一 unqiue
主键 primary key  标识列 identity 外键 foreign key  检查 check
约束 constraint
二 编写SQL语句(5分/题) 50分 (包含 笔试题问题和解答答案)
1) 创建一张学生表,包含以下信息,学号,姓名,年龄,性别,家庭住址,联系电话
Create table stu (学号 int ,
姓名 varchar(8),
年龄 int,
性别 varchar(4),
家庭地址 varchar(50),
联系电话 int
);
2) 修改学生表的结构,添加一列信息,学历
Alter table stu add 学历 varchar(6);
3) 修改学生表的结构,删除一列信息,家庭住址
Alter table stu drop column 家庭地址
4) 向学生表添加如下信息:
学号 姓名年龄性别联系电话学历
1A22男123456小学
2B21男119中学
3C23男110高中
4D18女114大学
Insert into stu values(1,’A’,22,’男’,123456,’小学’)
Insert into stu values(2,’B’,21,’男’,119,’中学’)
Insert into stu values(3,’C’,23,’男’,110,’高中’)
Insert into stu values(4,’D’,18,’女’,114,’大学’)
5) 修改学生表的数据,将电话号码以11开头的学员的学历改为“大专”
Update stu set 学历=’大专’ where 联系电话 like ‘11%’
6) 删除学生表的数据,姓名以C开头,性别为‘男’的记录删除
Delect from stu where 性别=’男’ and 姓名 like ‘c%’
7) 查询学生表的数据,将所有年龄小于22岁的,学历为“大专”的,学生的姓名和学号示出来
Select 姓名,学号 from stu where 年龄<22 and 学历=’大专’
8) 查询学生表的数据,查询所有信息,列出前25%的记录
Select top 25 percent * from stu
9) 查询出所有学生的姓名,性别,年龄降序排列
Select 姓名,性别 from stu order by 年龄 desc
10) 按照性别分组查询所有的平均年龄
Select avg(年龄) from stu group by 性别
三 填空(3分/题) 36分 (包含 笔试题问题和解答答案)
1) 索引分为__聚集索引___和__非聚集索引__在一张表上最多可以创建1个 聚集索引_索引。但是可以创建_249个非 聚集索引索引。
2) 系统存储过程_sp-helptext__是用来显示规则,默认值,未加密的存储过程,用户定义函数,触发或视图的文本
3) 事务开始:begin Transction
提交事务:commit Transction
回滚事务:rollback Transction
四 问答题(5分/题) 60分 (包含 笔试题问题和解答答案)
1) 数据库包含哪些那几种后缀名的文件必须,这些文件分别存放在什么的信息?
主要数据文件(.mdf) 包含数据用户收集的信息,还有数据库其他相关的信息,
日志数据文件(.ndf) 存放用户对数据库的增删改查的信息,用于备份恢复使用
2) TRUNCATE TABLE 命令是什么含义?和Delete from 表名有什么区?
TRUNCATE TABLE: 提供了一种删除表中所有记录的快速方法
Delete from 表名:可以删除表的一个或多条记录
3) 说出以下聚合数的含义:avg ,sum ,max ,min , count ,count(*)
AVG:求平均值
SUM:求和
MAX:求最大值
MIN:求最小值
COUNT(*):返回所有行数
COUNT返回满足指定条件的记录值
4) inner join 是什么意思?作用是什么?写出基本语法结构
INNER JOIN 内联接,用于返回两个表中要查询的列数据通信
Select * from 表名1 inner join 表名2 on 条件表达式
5) 左向外联接,右向外联接,全联接的关健字如何写?
Left outer join 左向外联接
Right outer join 右向外联接
Full outer join 全联接
6) 子查询分为几类,说明相互之间的别
了查询分三种基本子查询: 1.使用in 查询返回一列或更多值
2.比较运算符,返回单个值勤做为外查询的参数
3.用exists 查询时相当于进行一次数据测试
7) 实现实体完整性,实现域完整性,实现 完整性(引用完整性),实现自定义完整性分别使用什么手段?
实现实体完整性: 主键约束 唯一约束 标识列
实现域完整性: 默认值约束 检查约束 非空属性
引和完整性: 外键引用
8) 视图可以更新吗?会影响到实际表吗?
视图是可以更新的,视图只是基于基本表上的虚拟表,对视图的更新会直接影响到实际表
9) 谈谈这样几个角色, dbo , Sysadmin public
Dbo : 是数据库的拥有者,对数据库拥有所有操作的权限
Sysadmin : 可以对SQL SERVER执行任何活动
Public : 自动创建的,能捕获数据库中用户的所有默认权限
10) 何为动态游标?何为静态游标?
动态游标与静态游标相对,反映结果集中所做的所有更改,
静态游标的结果集在游标打开时,建立在tempdb中,总按照游标打开时的原样显示
11) 什么是存储过程?为什么存储过程要比单纯的Sql 语句执行起来要快?
存储过程:是一组预先编译好的T-SQL代码
在创建存储过程时经过了语法和性能优化,执行不必重复的步骤,使用存储过程可提高运行效率
12)什么是Inserted 表 ?什么是Deleted 表?
Inserted表用于存储inserted和update语句影响的副本
Deleted 表用于存储delect 和 update语句影响的行的副本
1、怎么把下面的表
year month amount
1991   1     1.1
1991   2     1.2
1991   3     1.3
1991   4     1.4
1992   1     2.1
1992   2     2.2
1992   3     2.3
1992   4     2.4
查成这样一个结果
year m1 m2 m3 m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
解一:
select [year],
(select amount from tab t where [month] = 1 and t.year = tab.year) as 'm1',
(select amount from tab t where [month] = 2 and t.year = tab.year) as 'm2',
(select amount from tab t where [month] = 3 and t.year = tab.year) as 'm3',
(select amount from tab t where [month] = 4 and t.year = tab.year) as 'm4'
from tab
group by [year]
解二:
select t1.year,
t1.amount as 'm1',
t2.amount as 'm2',
t3.amount as 'm3',
t4.amount as 'm4'
from tab t1,
tab t2,
tab t3,
tab t4,
where t1.month < t2.month
and t2.month < t3.month
and t3.month < t4.month
and t1.year = t2.year
and t2.year = t3.year
and t3.year = t4.year
2、用一条SQL语句 查询出每门课都大于80分的学生姓名
name   kecheng   fenshu
张三     语文       81
张三     数学       75
李四     语文       76
李四     数学       90
王五     语文       81
王五     数学       100
王五     英语       90
解:
select distinct [name]
from student
where [name] not in (
select distinct [name]
from student
where fenshu <= 80)
create table dept
(
deptno varchar(10) primary key,
dname varchar(10)
);
create table emp
(
empno varchar(10) primary key,
ename varchar(10),
job varchar(10),
mgr varchar(10),
sal varchar(10),
deptno varchar(10) references dept(deptno)
);
drop table dept;
drop table emp;
insert into dept values ('1','事业部');
insert into dept values ('2','销售部');
insert into dept values ('3','技术部');
insert into emp values ('01','jacky','clerk','tom','1000','1');
insert into emp values ('02','tom','clerk','','2000','1');
insert into emp values ('07','biddy','clerk','','2000','1');
insert into emp values ('03','jenny','sales','pretty','600','2');
insert into emp values ('04','pretty','sales','','800','2');
insert into emp values ('05','buddy','jishu','canndy','1000','3');
insert into emp values ('06','canndy','jishu','','1500','3');
select * from dept;
select * from emp;
--1列出emp表中各部门的部门号,最高工资,最低工资
select deptno as 部门号,max(sal) as 最高工资,min(sal) as 最低工资 from
emp group by deptno;
--2 列出emp表中各部门job为'CLERK'的员工的最低工资,最高工资
select max(sal) as 最高工资,min(sal) as 最低工资,deptno as 部门号 from emp where
job='clerk' group by deptno;
--3 对于emp中最低工资小于2000的部门,列出job为'CLERK'的员工的部门号,最低工资,最高工资
select b.deptno as 部门号,max(sal) as 最高工资,min(sal) as 最低工资 from emp as b
where job='clerk' and (select min(sal)from emp as a where a.deptno=b.deptno)<2000 group by
b.deptno;
--4 根据部门号由高而低,工资有低而高列出每个员工的姓名,部门号,工资
select ename as 姓名,deptno as 部门号,sal as 工资 from emp order by deptno desc,sal asc;
--5 列出'buddy'所在部门中每个员工的姓名与部门号
select b.ename as 姓名,b.deptno as 部门号 from emp as b where b.deptno=
(select a.deptno from emp as a where a.ename='buddy');
--6 列出每个员工的姓名,工作,部门号,部门名
select ename as 姓名,job as 工作,dept.deptno as 部门号,dept.dname as 部门名 from emp,dept
where emp.deptno=dept.deptno;
--7列出emp中工作为'CLERK'的员工的姓名,工作,部门号,部门名
select ename as 姓名,job as 工作,dept.deptno as 部门号,dept.dname as 部门名 from emp,dept
where emp.deptno=dept.deptno and job='clerk';
--8对于emp中有管理者的员工,列出姓名,管理者姓名(管理者外键为mgr)
select a.deptno as 部门号,a.ename as 员工,b.ename as 管理者 from emp as a,emp as b where a.mgr is not null and a.mgr=b.ename;
--9 对于dept表中,列出所有部门名,部门号,同时列出各部门工作为'CLERK'的员工名与工作
select a.deptno as 部门号,a.dname as 部门名,b.ename as 员工名,b.job as 工作 from dept as a,
emp as b where a.deptno=b.deptno and b.job='clerk';
--10 对于工资高于本部门平均水平的员工,列出部门号,姓名,工资,按部门号排序
select b.deptno as 部门号,b.ename as 姓名,b.sal as 工资 from emp as b
where b.sal>(select avg(a.sal) from emp as a where a.deptno=b.deptno) order by b.deptno;
--11对于emp,列出各个部门中工资高于本部门平均工资的员工数和部门号,按部门号排序
select a.deptno as 部门号,count(a.sal) as 员工数 from emp as a
where a.sal>(select avg(b.sal) from emp as b where a.deptno=b.deptno) group by a.deptno order
by a.deptno;
--12对于emp中工资高于本部门平均水平,人数多与1人的,列出部门号,人数,平均工资,按部门号排序
select count(a.empno) as 员工数,a.deptno as 部门号,avg(sal) as 平均工资
from emp as a where (select count(c.empno) from emp as c where c.deptno=a.deptno and
c.sal>(select avg(sal) from emp as b where c.deptno=b.deptno))>1
group by a.deptno order by a.deptno;
--13对于emp中低于自己工资至少5人的员工,列出其部门号,姓名,工资,以及工资少于自己的人数
select a.deptno as 部门号,a.ename as 姓名,a.sal as 工资,(select count(b.ename) from emp as b
where b.salwhere (select count(b.ename) from emp as b where b.sal=5
TableX有三个字段Code、 Name、 Age、 其中Code为主键;
TableY有三个字段Code、 Class、Score, 其中Code + Class 为主键。两表记录如下:
Code Name Age Code Class Score
97001 张三 22 97001 数学 80
97002 赵四 21 97002 计算机 59
97003 张飞 20 97003 计算机 60
97004 李五 22 97004 数学 55
1、请写出SQL,找出所有姓张的学生,并按年龄从小到大排列;
select * from TableX where name like '张%' order by age
2、请写出SQL,取出计算机科考成绩不及格的学生;
select * from tableX where code in (select code from tableY WEHRE class='计算机' and score <60)
3、通过等值联接,取出Name、Class、Score,请写出SQL即输出结果
select a.name,b.class,b.score from tableX a,tableY b where a.code=b.code
4、通过外联接,取出每个学生的Name、Class、Score、请写SQL输出结果
select a.name,b.class,b.score from tableX full join tableY on a.code=b.code
5、请写SQL,在TableX 表中增加一条学生记录(学号:97005 姓名:赵六 年龄:20);
insert into tablex values('97005','赵六',20)
6、李五的年龄记录错了,应该是21,请写SQL,根据主键进行更新;
update tablex set age=21 where code='97004'
7、请写SQL,删除TableX中没有考试成绩的学生记录,请使用not in条件;
delete tablex where code not in (select code from tabley)
.用一条SQL语句 查询出每门课都大于80分的学生姓名
name   kecheng   fenshu
张三     语文       81
张三     数学       75
李四     语文       76
李四     数学       90
王五     语文       81
王五     数学       100
王五     英语       90
A: select distinct name from table  where  name not in (select distinct name from table where fenshu<=80)
2.学生表 如下:
自动编号   学号   姓名 课程编号 课程名称 分数
1        2005001  张三  0001      数学    69
2        2005002  李四  0001      数学    89
3        2005001  张三  0001      数学    69
删除除了自动编号不同,其他都相同的学生冗余信息
A: delete tablename where 自动编号 not in(select min(自动编号) from tablename group by 学号,姓名,课程编号,课程名称,分数)
一个叫department的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合.
你先按你自己的想法做一下,看结果有我的这个简单吗?
答:select a.name, b.name
from team a, team b
where a.name < b.name
请用SQL语句实现:从TestDB数据表中查询出所有月份的发生额都比101科目相应月份的发生额高的科目。请注意:TestDB中有很多科目,都有1-12月份的发生额。
AccID:科目代码,Occmonth:发生额月份,DebitOccur:发生额。
数据库名:JcyAudit,数据集:Select * from TestDB
答:select a.*
from TestDB a
,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b
where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur
************************************************************************************
面试题:怎么把这样一个表儿
year  month amount
1991   1     1.1
1991   2     1.2
1991   3     1.3
1991   4     1.4
1992   1     2.1
1992   2     2.2
1992   3     2.3
1992   4     2.4
查成这样一个结果
year m1  m2  m3  m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
答案一、
select year,
(select amount from  aaa m where month=1  and m.year=aaa.year) as m1,
(select amount from  aaa m where month=2  and m.year=aaa.year) as m2,
(select amount from  aaa m where month=3  and m.year=aaa.year) as m3,
(select amount from  aaa m where month=4  and m.year=aaa.year) as m4
from aaa  group by year
这个是ORACLE  中做的:
select * from (select name, year b1, lead(year) over
(partition by name order by year) b2, lead(m,2) over(partition by name order by year) b3,rank()over(
partition by name order by year) rk from t) where rk=1;
作者:不详 发文时间:2003.05.29 10:55:05
说明:复制表(只复制结构,源表名:a 新表名:b)
SQL: select * into b from a where 1<>1
说明:拷贝表(拷贝数据,源表名:a 目标表名:b)
SQL: insert into b(a, b, c) select d,e,f from b;
说明:显示文章、提交人和最后回复时间
SQL: select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
说明:外连接查询(表名1:a 表名2:b)
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
说明:日程安排提前五分钟提醒
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
说明:两张关联表,删除主表中已经在副表中没有的信息
SQL:
delete from info where not exists ( select * from infobz where info.infid=infobz.infid )