以下是一个订货管理数据库,其中有仓库表、职工表、订购单表、供货商表。
仓库表:
仓库号
城市
面积
wh1
北京
370
wh2
上海
500
wh3
广州
200
wh4
武汉
400
职工表:
仓库号
职工号
工资
wh2
e1
1220
wh1
e3
1210
wh2
e4
1250
wh3
e6
1230
wh1
e7
1250
 
 
 
 
 
订购单表:
职工号
供应商号
订购单号
订购日期
e3
s7
or67
2001-6-23
e1
s4
or73
2001-7-28
e7
s4
or76
2001-5-25
e6
null
or77
  -   -
e3
s4
or79
2001-6-13
e1
null
or80
  -   -
e3
null
or90
  -   -
e3
s3
or91
2001-7-13
供应商表:
供应商号
供应商名
地址
s3
振华电子厂
西安
s4
华通电子公司
北京
s6
607
郑州
s7
爱华电子厂
北京
参考答案:
1.     从职工关系中检索所有工资值。
答:select 工资 from 职工表
2.     检索仓库关系中的所有记录
答:select * from 仓库表
3.     检索工资多于1230元的职工号
答:select 职工号 from 职工表 where 工资>1230
4.检索哪些仓库有工资多于1210元的职工。
答:select distinct 仓库号 from 职工表 where 工资>1210
5.       给出在仓库“wh1”或“wh2”工作,并且工资少于1250元的职工号。
答:select 职工号 from 职工表 where 工资<1250 ;
and (仓库号=”wh1” or 仓库号=”wh2”)
注意:逻辑运算符的优先级从高到低依次为notandor。运算符的优先级:括号  算术运算  关系运算  逻辑运算.
说明:前面的几个例子在from之后只指定了一张表,也就是说这些检索只基于一张表。如果有where子句,系统首先根据指定的条件依次检验关系中的每条记录,然后选出满足条件的记录(相当于关系的选择操作),并显示select子句中指定属性的值(相当于关系的投影操作)。
6.       找出工资多于1230元的职工号和他们所在的城市。
答:select 职工表.职工号, 仓库表.城市 from 职工表,仓库表 ;
where 职工表.仓库号=仓库表.仓库号 and 工资>1230
7.       找出工作在面积大于400的仓库的职工号以及这些职工工作所在的城市。
:select 职工表.职工号, 仓库表.城市, 仓库表.面积 ;
from 职工表,仓库表 where 职工表.仓库号=仓库表.仓库号 ;
and 仓库表.面积>400
说明:以上两题为简单的联接查询.
8.       哪些城市至少有一个仓库的职工工资为1250
:
方法一:
Select 仓库表.城市 from 职工表,仓库表 where 职工表.仓库号=仓库表.仓库号 and 职工表.工资=1250
方法二:
select 仓库号 from 职工表 where 工资=1250 into dbf  abc.dbf
select 仓库表.城市 from 仓库表,abc where 仓库表.仓库号=abc.仓库号
方法三:
select 城市 from 仓库表 where 仓库号 in (select 仓库号 from 职工表 where 工资=1250)
说明: 这属于嵌套查询. 这类查询所要求的结果出自一个关系,但相关的条件却涉及多个关系.
可以看到,方法三的命令中含有两个select-from-where查询块,即内层查询块和外层查询块,内层查询块检索到的仓库值是wh1wh2,这样就可以写出等价命令:
select 城市 from 仓库表 where 仓库号 in ("wh1","wh2")
或者
select 城市 from 仓库表 where 仓库号="wh1" or 仓库号="wh2"
9.       查询所有职工的工资多于1210元的仓库的信息。
:
方法一:
select 仓库号,min(工资) as 工资 from  职工表 group by 仓库号 into dbf 仓库min工资.dbf
select 仓库表.* from 仓库表,仓库min工资 where 仓库表.仓库号=仓库min工资.仓库号 and 仓库min工资.工资>1210
方法二:
select * from 仓库表 where 仓库表.仓库号 not in (select 仓库号 from 职工表 where 工资<=1210 ) and 仓库表.仓库号 in (select 仓库号 from 职工表)
(错误方法)
 select * from 仓库表 where 仓库表.仓库号 not in (select 仓库号 from 职工表 where 工资<=1210 )
注意:上述检索结果错误,会将没有职工的仓库检索出来.如果要求排除那些还没有职工的仓库,检索要求可以叙述为:检索所有职工的工资都大于1210元的仓库的信息,并且该仓库至少要有一名职工.
(错误方法)
select * from 仓库表 where 仓库表.仓库号 in (select 仓库号 from 职工表 where 工资>1210 )
注意:上述查询结果错误。它会查出仓库号为wh1的信息,但wh1的职工工资并不都大于1210
10.   找出和职工e4挣同样工资的所有职工。
: Select 职工号 from 职工表 where 工资 in (select 工资 from 职工表 where  职工号=”e4”)
说明:7910题都是基于多个关系的查询,这类查询所要求的结果出自一个关系,但相关的条件却涉及多个关系.我们称之为嵌套查询。嵌套查询优选含有两个select-from-where查询块的查询结构。
11.   检索出工资在1220元到1240元范围内的职工信息。
:select * from 职工表 where 工资 between 1220 and 1240
说明: “工资 between 1220 and 1240”等价于工资>=1220 and 工资<=1240”
如果要求查询工资不在1220元到1240元范围内的职工信息
说明: select * from 职工表 where 工资 not  between 1220 and 1240
12.   从供应商关系中检索出全部公司的信息,不要工厂或其他供应商的信息。
Select * from 供应商表 where "公司" $ 供应商名
13.   找出不在北京的全部供应商信息。
Select * from 供应商表 where 地址!=”北京
或者
Select *  from 供应商表 where not(地址=”北京”)
14.   按职工的工资值升序检索出全部职工信息。
:select * from 职工表 order by 工资
如果需要将结果按降序排列,只要加上desc
select * from 职工表 order by 工资 desc
说明:使用SQL SELECT可以将查询结果排序,排序的短语是order by ,具体格式如下: 
order by order_item [ASC|DESC] [,order_item [ASC|DESC]……]
15.   先按仓库号排序,再按工资排序并输出全部职工信息。
答:Select * from 职工表 order by 仓库号,工资
16.   找出供应商所在地的数目。
:select count(distinct 地址) from 供应商表
注意:除非对表中的记录数进行计数,一般count函数应该使用distinct
比如: select count(*) from 供应商表
查询结果是供应商表中的记录数.
说明:可用于计算检索的函数有:count——计数  sum——求和 
avg——计算平均值  max——求最大值  min——求最小值
17.   求支付的工资总数
答:select sum(工资) from 职工表
18.   求北京和上海的仓库职工的工资总和
: select sum(工资) from 职工表,仓库表 where 职工表.仓库号=仓库表.仓库号 and (城市="北京" or 城市="上海")
方法二:
select sum(工资) from 职工表 where 仓库号 in (select  仓库号 from 仓库表 where 城市=”北京” or 城市=”上海”)
19.   求所有职工的工资都多于1210元的仓库的平均面积
答:Select avg(面积) from 仓库表 where 仓库号 not in(select 仓库号 from 职工表 where 工资<=1210) and 仓库号 in(select 仓库号 from 职工表)
20.   求在wh2仓库工作的职工的最高工资值
:select max(工资) from 职工表 where 仓库号=”wh2”
21.   求每个仓库的职工的平均工资
:select 仓库号,avg(工资) from 职工表 group by 仓库号
说明:可以利用group by 子句进行分组计算查询.group by短语的格式如下:group by groupcolumn[,groupcolumn……][having filtercondition]
可以按一列或多列分组,还可以用having 进一步限定分组的条件.
注意:where /group by等子句都不能放在from子句之前.
22.   求至少有两个职工的每个仓库的平均工资。
: select 仓库号,count(*),avg(工资) from 职工表 group by 仓库号 having count(*)>=2
说明:having子句总是跟在group by 子句之后,不可以单独使用.having子句用于限定分组.
23.   找出尚未确定供应商的订购单
答:select * from 订购单表 where 供应商号 is null
24.   列出已经确定了供应商的订购单信息
答:select * from 订购单表 where 供应商号 is not null
25.   查询供应商名
答:select 供应商名 from 供应商表
26.   在订购单表中加入一个新字段总金额,说明完成该订购单所应付出的总金额数。
Alter table 订购单表 add 总金额 n(10)
27.   列出每个职工经手的具有最高总金额的订购单信息。
Select 职工号,供应商号,订购单号,订购日期,总金额 from 订购单表 where 总金额=(select MAX(总金额) from 订购单表 group by 职工号 )
28.   检索哪些仓库中还没有职工的仓库的信息
Select * from 仓库表 where 仓库号 not in (select 仓库号 from 职工表)
29.   检索哪些仓库中至少已经有一个职工的仓库的信息
Select * from 仓库表 where 仓库号  in (select 仓库号 from 职工表)
30.   检索有职工的工资大于或等于wh1仓库中任何一名职工工资的仓库号
Select distinct 仓库号 from 职工表 where 工资>=(select min(工资) from 职工表 where 仓库号=”wh1”)
31.   检索有职工的工资大于或等于wh1仓库中所有职工工资的仓库号。
Select distinct 仓库号 from 职工表 where 工资>=(select max(工资) from 职工表 where 仓库号=”wh1”)