SQL面试题
Insert Into 数据表名称(字段名称1,字段名称2,...) values(字段值1,字段值2,...)
insert into user(username,password,age) values('李老四','6666',45)
Update 数据表名称 Set 字段名称=字段值,字段名称=字段值,...[Where 条件]
Delete from 数据表
下列查询返回在LONDON(伦敦)或SEATTLE(西雅图)的所有雇员:
SELECT * from employees WHERE UPPER(city) IN ('LONDON','SEATTLE')
下面示例利用DATEDIFF函数,确定在 pubs 数据库中标题发布日期和当前日期间的天数。
SELECT DATEDIFF(day, OrderDate, getdate()) AS no_of_days from table1
返回字符串"wonderful"在 titles 表的 notes 列中开始的位置。
SELECT CHARINDEX('wonderful', notes)
以下是返回的结果:(第47个字符位置)
显示工作站的名称:select host_name() as [Client Computer Name]
下例是检索 titles 表中百分之五十的书。如果 titles 表中包含了 18 行,则将检索前 9 行。
SELECT TOP 50 PERCENT title from titles
字段名称 [Not] Between 起始值 and 终止值
列出BOOK表中30至50元的书
select * from book where price between 30 and 50
字段名称 [Not] In(列出值1,列出值2,...)
从BOOK表中列出价格为30,40,50,60的所有书
select * from book where price in(30,40,50,60)
字段名称 [Not] Like "通配符"
列出BOOK表中出版社含电的所有记录
select * from book where publishing like '*电*'
列出BOOK表中出版社第一个字是电的所有记录
select * from book where publishing like '电*'
---
select Sum/Count/Avg/Max/Min(字段名称) [As 新名称] from 数据表名称
sum求和:
求出总价格做为合计字段
select sum(price ) as 合计 from book
count统计数量:
统计BOOK表中有多少条记录做为数量字段
select count(id) as 数量 from book
AVG平均:
算出BOOK表中所有书的平均价格
select avg(price) as 平均价格 from book
MAX最大:
列出BOOK表中最贵的书
select max(price) as 最贵书 from book
MIN最小:
select min(price) as 最便宜书 from book
交叉联接: SELECT * from table1 CROSS JOIN table2
select x.[name], y.[name] from x left join y on x.[refid] = y.id
select y.[name], x.[name] from x right join y on x.[refid] = y.id
表联接查询
SEL
相关文档:
exists (sql 返回结果集为真)
not exists (sql 不返回结果集为真)
如下:
表A
ID NAME
1 A1
2 A2
3 A3
表B
ID AID NAME
1 1 B1
2 2 B2
3 2 B3
表A和表B是1对多的关系 A.ID => B.AID
......
--SQL Server:
Select TOP N * from TABLE Order By NewID()
--Access:
Select TOP N * from TABLE Order By Rnd(ID)
Rnd(ID) 其中的ID是自动编号字段,可以利用其他任何数值来完成,比如用姓名字段(U ......
/*----------------------------------------------------------------
-- Author :feixianxxx(poofly)
-- Date :2010-05-12 19:43:03
-- Version:
-- Microsoft SQL Server 2008 (SP1) - 10 ......
declare @t varchar(255),@c varchar(255)
declare table_cursor cursor for select a.name,b.name
from sysobjects a,syscolumns b ,systypes c
where a.id=b.id and a.xtype='u'&n ......
syscolumns
每个表和视图中的每列在表中占一行,存储过程中的每个参数在表中也占一行。该表位于每个数据库中。
列名数据类型描述
name
sysname
列名或过程参数的名称。
id
int
该列所属的表对象 ID,或与该参数关联的存储过程 ID。
xtype
tinyint
systypes 中的物理存储类型。
typestat
tinyint
仅限内部使 ......