1.单表查询(实现投影操作)
查询供应商的名称、地址和联系电话。
Select s_name,s_address,s_phone from Supplier;
2.单表单条件查询(实现选择操作)
查询零售价格不超过20元的零件编号、名称及其零售价格。
select p_partkey,p_name,p_RETAILPRICE from Part where p_RETAILPRICE<=20;
3.不带分组过滤条件的分组统计查询
统计每个供应商的供应价格。
Select s_name,ps_SUPPLYCOST from Supplier,Partsupp where S_SUPPKEY=ps_SUPPKEY;
4.带分组过滤条件的分组统计查询
查询供应价格平均金额超过1000元的供应商编号及其姓名。
Select s_suppkey,s_name from supplier,partsupp where s_suppkey=ps_suppkey
group by s_suppkey
having avg(ps_SUPPLYCOST)>=1000;
聚集函数不能用于where,只能用于select子句和having。
5.两表连接查询(实现笛卡尔积操作)
查询供应商及其所在国家。
Select s_name,n_name from supplier,nation where s_nationkey=n_nationkey;
6.两表连接查询(实现普通连接操作,任意两个列做大于、小于等连接操作)
查询供应价格小于零售价格的零件及其供应信息。
Select * from part,supplier,partsupp
where p_partkey=ps_partkey
and ps_SUPPLYCOST<p_RETAILPRICE
and ps_suppkey=s_suppkey;
7.单表自身复合连接查询
查询与“深圳春晖航空售票处”在同一个国家的供应商编号、名称和地址信息。
Select S_SUPPKEY,S_NAME,s_ADDRESS from Supplier,nation
where n_nationkey in
(select s_nationkey from supplier
where s_name=”深圳春晖航空售票处”);
8.三表连接查询
查询顾客“刘称奇”订购的订单编号,及其订购的零件编号、数量和实际价格。
Select o_ORDERKEY,l_partkey,l_quantity,l_EXTENDEDPRICE from customer,orders,lineitem
where c_name=”刘称奇”
and c_custkey=o_custkey
and o_orderkey=l_orderkey;
结束!