2014-10-22日面试

商品销售数据库
Article(商品号 char(4),商品名char(16),单价 Numeric(8,2),库存量 int)      
Customer(顾客号char (4),顾客名 char (8),性别 char(2),年龄 int)
OrderItem(顾客号 char(4),商品号 char(4),数量 int, 日期 date)
1、检索至少定购商品号为‘0001’和‘0002’的顾客号。

方法一: 
    select  顾客号 from OrderItem where 商品号='0001' and 顾客号 in
             ( select 顾客号 from OrderItem where 商品号='0002');

方法二:select a.顾客号 from Ord erItem a,OrderItem b where a.顾客号=b.顾客号 and a.商品号='0001'  and b.商品号='0002' 

2、检索一次定购商品号‘0001’商品数量最多的顾客号和顾客名。

select 顾客号,顾客名 from Custommer where 顾客号 in
(select 顾客号 from OrderItem where 商品号='0001'and 数量=
(select MAX(数量)from OrderItem where 商品号='0001'));


3、检索至少订购了3单商品的顾客号和顾客名及他们定购的商品次数和商品总数量,并按商品总数量降序排序。

select Custommer.顾客号,顾客名,count( *),Sum(数量) from OrderItem,Custommer
where OrderItem.顾客号=Custommer.顾客号  group by Custommer.顾客号,顾客名
having count( *)>3 order by Sum(数量) desc;

4、 检索购买的商品单价价都高于或等于1000元的顾客号和顾客名。
    select Custommer.顾客号,顾客名 from Custommer where 顾客号 in
(select 顾客号 from OrderItem where 顾客号 not in
(select 顾客号 from OrderItem,Article
where OrderItem.商品号=Article.商品号 and 单价<=1000))

5、检索所有的顾客号和顾客名以及它们所购买的商品号。(包括没买商品的顾客)
   select Custommer.顾客号,顾客名,商品号
from Custommer left join OrderItem on Custommer.顾客号=OrderItem.顾客号  

6、 检索这样的顾客号,顾客名,他们定购了所有的商品 (除法)      
    select Custommer.顾客号,顾客名 from Custommer where not exists
(select * from Article where not exists
(select * from OrderItem
where OrderItem.顾客号=Custommer.顾客号 and OrderItem.商品号=Article.商品号))  
7、 查询字段a的值,连续三条以上相同的记录,结构如下:tmp2(a,b,c)

   此题谁能给出好的方法呢

原文地址:https://www.cnblogs.com/lisabi/p/4045980.html