-
精品文档
例题
p>
1
、写出一条
Sql
语句:取出表
A
中第
31
到第
40
记录
(
Mysql
)
select * from A limit 30, 10
MS-SQLServer
解
1
p>
:
select top 10 * from A where
id not in (select top 30 id from A)
解
p>
2
:
select top 10 *
from A where id > (select max(id) from (select top
30 id from A )as A)
解
3
:
select * from (select *,
Row_Number() OVER (ORDER BY id asc) rowid FROM A)
as A where rowid
between 31 and 40
Oracle
select *
from (select A.*,
row_number() over (order by id asc) rank
FROM A)
where rank >=31 AND rank<=40;
2
、用一条
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
tabl
e
where
fenshu<=80)
select
name
from
table
group
by
name
having
min(fenshu)>80
3
、学生表
如下
:
自动编号
学号
姓名
课程编号
课程名称
分数
1
2005001
张三
0001
数学
69
2
2005002
李四
0001
数学
89
3
2005001
张三
0001
数学
69
删除除了自动编号不同
,
其他都相同的学生冗余信息
A:
delete
tablename
where
自动编号
not
in(select
min(
自动编号
)
from
tablename group
by
学号
,
姓名
,
课程编号
,
课程名称
,
分数
)
4
、请用
SQL
语句实现:
从
TestDB
数据表中查询出所有月份的发生额都比
101
科目相应月份的发生额高的科目。请
注意:
TestD
B
中有很多科目,都有
1
-
12
月份的发生额。
AccID
:科目代码,
Occmonth
:发生额月份,
DebitOccur
:发生额。
数据库
名:
JcyAudit
,数据集:
Select
*
from
TestDB
答:
select
a.*
from
TestDB
a
,(select
Occmonth,max(DebitOccur)
Debit101ccur
from
TestDB
where
AccID='101'
gro
up
by
Occmonth)
b
where
th=th
and
ccur>101ccur
精品文档
精品文档
5
、一个叫
team
的表,
里面只有一个字段
name,
一共有
4
条纪录,分别是
a,b,c,d,
对
应四个球对,现在四个球对进
行比赛,用一条
sql
语句显示所有可能的比赛组合
.
你先按你自己的想法做一下,看结果有我的这个简单吗?
select ,
from team a, team
b
where <
6
、面试题:怎么把这样一个表儿
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
m1,
(select
amount from
aaa
m
where
month=2
m2,
精品文档
and
=)
as
and
=)
as
精品文档
(select
amount from
aaa
m
where
month=3
and
=)
as
m3,
(select
amount from
aaa
m
where
month=4
and
=)
as
m4
from aaa
group by year
7
、说明:复制表
(
只复制结构
,
源表名:
a
新表名:
b)
SQL: select * into b from a where 1<>1
(where1=1
,
拷贝表结构和数
据内容
)
Oracle
:create table b
As
Select * from
a where 1=2
[<>
(不等于)
(SQL
Server Compact)
比较两个表达式。
当使用此运算符比较非空表达式时,如果左操作数不等于右操作数,则结
果为
TRUE
。
否则,结果为
FALSE
。
]
8
、说明:拷贝表
(
拷贝数据
,
源表名:
a
目标表名:
b)
SQL: insert into b(a, b, c) select
d,e,f from a;
9
、说明:显示文章、提交人
和最后回复时间
SQL:
select
,me,e
from
table
a,(select
max(adddate)
adddate
fr
om
table
where
=)
b
10.
说明:外连接查询
(
表名
1
:
a
表名
2
:
b)
精品文档
精品文档
SQL:
select
a.a,
a.b,
a.c,
b.c,
b.d,
b.f
from
a
LEFT
OUTER
JOIN
b
ON
a.a
=
b.c
ORACLE
:
select
a.a,
a.b,
a.c,
b.c,
b.d,
b.f
from
a
,b
where
a.a
=
b.c(+)
11.
说明:日程安排提前五分钟提醒
SQL:
select
*
from
日程安排
where
datediff('minute',f
开始时间
,getdate())>5
12.
说明:两张关联表,删除主表中已经在副表中没有的信息
SQL:
Delete
from
info
where
not
exists
(select
*
from
infobz
where
=
)
1
3.
有两个表
A
和
B
,均有
key
和
value
两个字段,如果
B
的
key
在
A
中也有,就把
B
的
value
换为
A
中对应的
value
这道题的
SQL
语句怎么写?
update
b
set
=(select
from
a
where
=)
where
in(select
from
b,a
where
=);
高级
sql
面试题
14.
原表
:
精品文档
精品文档
courseid
coursename
score
-------------------------------------
1
Java
70
2
oracle
90
3
xml
40
4
jsp
30
5
servlet
80
-------------------------------------
为了便于阅读
,
查询此表后的结果显式如下
(
及格分数为
60):
courseid
coursename
score
mark
------
---------------------------------------------
1
Java
70
pass
2
oracle
90
pass
3
xml
40
fail
4
jsp
30
fail
5
servlet
80
pass
---------
------------------------------------------
写出此查询语句
select
courseid,
coursename
,score
,decode
(
sign(score-60),-1,'
fail','pass')
as
mark
fro
m
course
(SQL:
select
courseid,
coursename
,score
,
(
case
when
score<60
then
'fail'
else
'pas
s'
end)
as
mark
from
course
)
ORACLE
:
select
courseid,
coursename
,score
,decode
(
p>
sign(score-60),-1,'fail','pass')
as
mark
from
course
(DECODE
函数是
ORACLE
PL/SQL
是功能强大的函数之一,目前还只有
ORACLE
公司的
SQL
提供了此函数
)
完全正确
精品文档
精品文档
SQL>
desc
course_v
Name
Null?
Type <
/p>
----------------------------------------
-
--------
----------------------------
COURSEID
NUMBER
COURSENAME
VARCHAR2(10)
SCORE
NUMBER
SQL>
select
*
from
course_v;
COURSEID
COURSENAME
SCORE
----------
----------
----------
1
java
70
2
oracle
90
3
xml
40
4
jsp
30
5
servlet
80
SQL>
select
courseid,
coursename
,score
,decode(sign(score-60
),-1,'fail','pass')
as
ma
rk
from
course_v;
COURSEID
COURSENAME
SCORE
MARK
----------
----------
----------
----
1
java
70
pass
2
oracle
90
pass
3
xml
40
fail
4
jsp
30
fail
5
servlet
80
pass
面试题(
< br>1
)
create
table
testtable1
(
id
int
IDENTITY,
department
varchar(12)
)
精品文档
精品文档
select
*
from
testtable1
insert
into
testtable1
values('
设计
')
insert
into
testtable1
values('
市场
')
insert
into
testtable1
values('
售后
')
/*
结果
id
department
1
设计
2
市场
3
售后
*/
create
table
testtable2
(
id
int
IDENTITY,
dptID
int,
name
varchar(12)
)
insert
into
testtable2
values(1,'
张三
')
insert
into
testtable2
values(1,'
李四
')
insert
into
testtable2
values(2,'
王五
')
insert
into
testtable2
values(3,'
彭六
')
insert
into
testtable2
values(4,'
陈七
')
/*
用一条
SQL
< br>语句,怎么显示如下结果
id
dptID
department
name
1
1
设计
张三
2
1
设计
李四
3
2
市场
王五
4
3
售后
彭六
5
4
黑人
陈七
*/
答案:
SELECT
testtable2.*
,
IS
NULL(department,'
黑人
')
FROM
testtable1
right
join
testtable2
on
=
精品文档
精品文档
也做出来了可比这方法稍复杂。
/
<
/p>
面试题(
2
)
有表
A
,结构如下:
< br>
A:
p_ID
p_Num
s_id
1
10
01
1
12
02
2
8
01
3
11
01
3
8
03
其中:
< br>p_ID
为产品
ID
,
p_Num
为产品库存量,
s_id
为仓库
ID
。请用
SQL
语句实现将上表
中的数据合并,合并后的数据为:
p_ID
s1_id
s2_id
s3_id
1
10
12
0
2
8
0
0
3
11
0
8
其中:
s1_id
为仓库
1
的库存量,<
/p>
s2_id
为仓库
2
的库存量,
s3_id
为仓库
3<
/p>
的库存量。如果
该产品在某仓库中无库存量,那么就是
0
代替。
结果:
select
p_id
,
sum(case
when
s_id=1
then
p_num
else
0
end)
as
s1_id
,sum(case
when
s_id=2
then
p_num
else
0
end)
as
s2_id
,sum(case
when
s_id=3
then
p_num
else
0
end)
as
s3_id
from
myPro
group
by
p_id
什么是相关子查询
?
如何使用这些查询
?
经验更加丰富的开发人员将能够准确地描述这种类型的查询。
相关子查询是一种包含子查询的特殊类型的查询。查询里包含的子查询会真正请求外部查
询
的值,从而形成一个类似于循环的状况。
精品文档
精品文档
19
、为管理业务培训信息,建立
3
个表:
S(S#,SN,SD,SA)S#,SN,S
D,SA
分别代表学号,学员姓名,所属单位,学员年龄
C(C#,CN)C#,CN
分别代表课程编号,课程名称
SC(S#,C#,G)
S#,C#,G
分别代表学号,所选的课程编号,学习成绩
p>
(
1
)使用标准
S
QL
嵌套语句查询选修课程名称为
’
税
收基础
’
的学员学号和姓名
?
答案:
select
s#
,sn
from
s
where
S#
in(select
S#
from
c,sc
where
c.c#=sc.c#
and
cn
=’
税收基础
’)
(2)
使用标准
SQL
嵌套语句查询选修课程编号为
’C2’
的学员姓名和所属单位
?
答:
select
sn,sd
from
s,sc
where
s.s#=sc.s#
and
sc.
c#=’c2’
(3)
使用标准
SQL
嵌套语句查询不选修课程编号为
’C5
’
的学员姓名和所属单位
?
答:
select
sn,sd
from
s
where
s#
not
in(select
s#
from
sc
where
c#=’c5’)
(4)
查询选修了课程的学员人数
答:
select
学员人数
=count(distinct
s#)
from
sc
(5)
查询选修课程
超过
5
门的学员学号和所属单位
?
答:
select
sn,sd
from
s
where
s#
in(select
s#
from
sc
group
by
s#
having
count(disti
nct
c#)>5)
18
、
SQL
面试题(
4
)
1.
查询
A(ID
,Name)
表中第
31
至
40
条记录,
ID
作为主
键可能是不是连续增长的列,
完整的
查询语句如下:
select
top
10
*
from
A
where
ID
>(select
max(ID)
from
(select
top
30
ID
from
A
order
by
A
)
T)
order
by
A
精品文档
精品文档
2.
查询表
A
中存在
ID
重复三次以上的记录
,
完整的查询语句如下:
select
*
from(select
count(ID)
as
count
from
table
group
by
ID)T
where
>3
简答题部分
1
.触发器的作用?
答:触发器是一中特殊的存储过程,主要是通过事件来触发
而被执行的。它可以强化约束,
来维护数据的完整性和一致性,
可以跟踪数据库内的操作从而不允许未经许可的更新和变化。
可以联级运算。如,某表上
的触发器上包含对另一个表的数据操作,而该操作又会导致该表
触发器被触发。
2
。什么是存储过程?用什么来调用?
答:存储过程是一个预编译的
SQL
语句,优点是允许模块化的设计,就是说只需创建一次,
以后在该程序中就可以调用多次
。
如果某次操作需要执行多次
SQL
,
使用存储过程比单纯
S
QL
语句执行要快。可以用一个命令对象来调用存储过程。
3
。索引的作用?和它的优点缺点是什么?
答:索引就一种特殊的查询表,数据库的搜索引擎可以利用它加速对数据的检索。它很类
似
与现实生活中书的目录,
不需要查询整本书内容就可以找到想
要的数据。
索引可以是唯一的,
创建索引允许指定单个列或者是
多个列。缺点是它减慢了数据录入的速度,同时也增加了数
据库的尺寸大小。
3
。什么是内存泄漏?
答:一般我们所说的内存泄漏指的是堆内存的泄漏。堆内存是程序从堆中为其分配的,大
小
任意的,使用完后要显示释放内存。当应用程序用关键字
ne
w
等创建对象时,就从堆中为它
分配一块内存,
使用完后程序调用
free
或者
delete
释放该内存,
p>
否则就说该内存就不能被使
用,我们就说该内存被泄漏了。
4
。维护数据库的完整性和一致性,你喜欢用
触发器还是自写业务逻辑?为什么?
答:我是这样做的,尽可能使用约束,如
check,
主键,外键,非空字段等来约束,这样做效
率最高,也最方便。其次是
使用触发器,这种方法可以保证,无论什么业务系统访问数据库
都可以保证数据的完整新
和一致性。
最后考虑的是自写业务逻辑,
但这样做麻烦,
编程复杂,
效率低下。
精品文档
精品文档
5
。什么是事务?什么是锁?
答:
事务就是被绑定在一起作为一个逻辑工作单元的
< br>SQL
语句分组,
如果任何一个语句操作
失败那么整个操作就被失败,以后操作就会回滚到操作前状态,或者是上有个节点。为了确
保要么执行,要么不执行,就可以使用事务。要将有组语句作为事务考虑,就需要通过
ACI
D
测试
,即原子性
,一致性,隔离性和持久性。
锁:在所以的
DBMS
中,锁是实现
事务的关键,锁可以保证事务的完整性和并发性。与现
实生活中锁一样,
它可以使某些数据的拥有者,
在某段时间内不能使用某些数据或
数据结构
。
当然锁还分级别的。
6
。什么叫视图?游标是什么?
答:视图是一种虚拟的表,具有和物理表相同的功能。可以对视图进行增,改,查,操作
,
试图通常是有一个表或者多个表的行或列的子集。对视图的修改不影响基本表。它使得
我们
获取数据更容易,相比多表查询。
游标:
是对查询出来的结果集作为
一个单元来有效的处理。
游标可以定在该单元中的特定行,
从结
果集的当前行检索一行或多行。可以对结果集当前行做修改。一般不使用游标,但是需
要
逐条处理数据的时候,游标显得十分重要。
SQL
数据库面试题以及答案(
50
< br>例题)
/hundan_520520/articl
e/details/54881208
Student(S
id,Sname,Sage,Ssex)
学生表
Sid
:学号
Sname
:学生姓名
Sage
:学生年龄
Ssex
:学生性别
Course(Cid,Cname,T#)
课程表
Cid
:课程编号
精品文档
精品文档
Cname
:课程名称
Tid
:教师编号
< br>SC(Sid,Cid,score)
成绩表
Sid
:学号
Cid
:课程编号
score
:成绩
< br>Teacher(Tid,Tname)
教师表
Tid
:教师编号:
Tname
:教师名字
问题:
1
、
查询
“001”
课程比
“002”
p>
课程成绩高的所有学生的学号
select
from
(
select
sid,score
from
sc
where
cid=
'001'
)a,
(
select
sid,score
from
sc
where
cid=
'002'
)b
where
=
and
>;
2
、查询平均成绩大于
60
分的同学的学号和平均成绩
select
sid,
avg
(score)
from
sc
group
by
sid
精品文档
精品文档
having
avg
(score)>
60
;
3
、查询所有同学的学号、姓名、选课数、总成绩
select
,,count_cid
as
选课数
,
sum_score
as
总成绩
from
student s
left
join
(
select
sid,
count
(cid)
as
count_cid,
sum
(score)
as
sum_score
from
sc
group
by
sid )sc
on
=
4
< br>、查询姓
‘
李
’
的老师的个数:
select
count
(tname)
from
teacher
where
tname
like
'
李
%'
;
5
、查询没有学过
“
< br>叶平
”
老师可的同学的学号、姓名:
select
,
from
student
as
s
where
not
in
(
select
DISTINCT
sid
精品文档
精品文档
from
sc
as
sc
where
in
(
select
cid
from
course
as
c
left
join
teacher
as
t
on
=
where
=
'
叶平
'
)
);
6
、查询学过
< br>“
叶平
”
老师所教的所有课的同
学的学号、姓名:
select
,
from
student
as
s
where
in
(
select
distinct
from
sc
as
sc
where
in
(
select
cid
from
course
as
c
left
join
teacher
as
t
on
=
where
=
'
叶平
'
)
精品文档
精品文档
group
by
HAVING
count
(cid)=
(
select
count
(cid)
from
course
as
c
left
join
teacher
as
t
on
=
where
=
'
叶平
'
)
);
7
、查询学过
< br>“011”
并且也学过编号
“002”
< br>课程的同学的学号、姓名:
SELECT
,
from
student
as
s
left
join
sc
as
sc
on
=
where
=
'001'
and
EXISTS
(
select
*
from
sc
as
sc_2
where
= sc_
and
sc_=
'002'
);
select
,
from
student
as
s
精品文档
精品文档
left
join
sc
as
sc
on
=
where
=
'001'
and
in
(
select
sid
from
sc
as
sc_2
where
sc_=
'002'
and
sc_ = );
8
、查询课程编号
“002”
的成绩比课程编号
“001”
课程低的所有同学的
学号、
姓名:
select
sid,sname
from
(
select
,,score,
(
select
score
from
sc
as
sc_2
where
sc_ =
and
sc_ =
'002'
)
as
score2
from
student,sc
where
=
and
cid =
'001'
) s_2
where
、查询没有学全所有课的同学的学号、姓名:
score2
?
?
1
2
精品文档
精品文档
?
?
?
?
?
?
3
4
5
6
7
8
9
、查询所有课程成绩小于
60
的同学的学号、姓名:
select
sid,sname
from
student
where
sid
not
in
(
select
from
student s,sc
where
=
and
score>
60
);
select
sid,sname
from
student s
where
not
EXISTS
(
select
from
sc
where
=
and
>
60
);
10
select
,
精品文档
精品文档
from
student s ,sc sc
where
=
group
by
,
having
count
()<(
select
count
(cid)
from
course);
select
,
from
student s
right
join
sc sc
on
=
group
by
,
having
count
()<
(
select
count
(cid)
from
course);
11<
/p>
、
查询至少有一门课与学号为
“1001
”
同学所学相同的同学的学号和姓名:
select
,sname
from
student,sc
where
=
and
cid
in
(
select
cid
from
sc
where
sid=
'1001'
);
精品文档
-
-
-
-
-
-
-
-
-
上一篇:小说鉴赏技巧术语
下一篇:剑桥通用英语(KET)考试介绍