理解 SQL 程序设计基本规范,熟练运用 SQL 语言实现数据查询,包括单表查询、分组统计查询和连接查询、嵌套查询和集合查询。
针对自建数据库 test 设计各种单表查询、分组统计查询和连接查询、嵌套查询合和集合查询,理解和掌握 SQL 查询语句各个子句的特点和作用,按照 SQL 程序设计规范写出具体的 SQL 查询语句,并调试通过。
要求每个查询语句都要有结果,并把查询结果截屏作为实验报告的内容,如果查询结果为空,需要自行修改所给表的数据,并把修改过程在实验报告里说明。
(1)查询地区的编号和名称:
SELECT
regionkey,
NAME
FROM
region;
(2)查询供应商的编号、名称、地址、电话和国家:
SELECT
suppkey,
supplier.NAME,
address,
phone,
nation.NAME
FROM
supplier,
nation
WHERE
supplier.nationkey = nation.nationkey;
(3)查询 2022 -9,订单金额> 1 万元,订单的编号、顾客姓名、顾客国家和日期,发现没有2022年份,更新日期(都增加一年):
UPDATE orders
SET orderdate = DATE_SUB( orderdate, INTERVAL 1 YEAR );
UPDATE orders
SET orderdate = DATE_ADD( orderdate, INTERVAL 1 YEAR );
SELECT
orderkey,
customer.NAME,
nation.NAME,
orderdate
FROM
orders,
customer,
nation
WHERE
customer.custkey = orders.custkey
AND customer.nationkey = nation.nationkey
AND YEAR ( orderdate )= '2022'
AND MONTH ( orderdate )= '9'
AND totalprice > 10000;
(4)统计2022的顾客的订购总金额,显示顾客的编号、姓名、订购总金额:
SELECT
customer.custkey,
customer.NAME,
SUM( totalprice ) AS sum_price
FROM
customer,
orders
WHERE
customer.custkey = orders.custkey
AND YEAR ( orderdate )= '2022'
GROUP BY
customer.custkey
ORDER BY
customer.custkey;
(5)统计2022年销售总金额>100万的零件的编号、名称、品牌、制造厂、销售总金额:
SELECT
part.partkey,
part.NAME,
part.brand,
mfgr,
SUM( totalprice ) AS sum_price
FROM
part,
orders,
lineitem
WHERE
YEAR ( orderdate )= '2022'
AND part.partkey = lineitem.partkey
AND orders.orderkey = lineitem.orderkey
GROUP BY
part.partkey
HAVING
SUM( totalprice )> 200000;
查询结果为空,语句没有问题,适当改变销售金额下线:
销售总金额大于50万:
(6)统计在 2022 销售总金额>100万的供应商信息,显示供应商的编号、供应商名称、销售总金额:
SELECT supplier.suppkey,supplier.name,SUM(totalprice)
FROM supplier,orders,lineitem
WHERE YEAR(orderdate)='2022'
AND supplier.suppkey=lineitem.suppkey
AND orders.orderkey=lineitem.orderkey
GROUP BY supplier.suppkey
HAVING SUM(totalprice)>1000000;
查询之后发现数据依然没有,再次修改销售总金额:
销售总金额大于10万元:
(7)查询订单平均金额超过 5 万元的顾客编号、姓名、所在国家名称和订单平均金额:
SELECT customer.custkey,customer.name,nation.name,
AVG(totalprice)
FROM customer,nation,orders
WHERE customer.custkey=orders.custkey
AND customer.nationkey=nation.nationkey
GROUP BY custkey
HAVING AVG(totalprice)>50000;
(8)查询与“金石印刷有限公司”在同一个国家的供应商编号、名称和地址信息:
SELECT A.suppkey,A.name,A.address
FROM supplier A,supplier B
WHERE A.nationkey=B.nationkey
AND B.`name`='金石印刷有限公司';
(9)查询供应价格小于零售价格的零件,显示零件的编号、名称、制造厂、品牌、零售价格、供应商名称和供应价格:
SELECT part.partkey,part.name,part.mfgr,brand,retailprice,
supplier.name,supplycost
FROM part,supplier,partsupp
WHERE partsupp.partkey=part.partkey
AND partsupp.suppkey=supplier.suppkey
GROUP BY partkey
HAVING supplycost<retailprice
(10)查询顾客“曹玉书”的订单,显示订单的编号、金额及零件的编号、数量和订单明细价格:
SELECT orders.orderkey,orders.totalprice,
lineitem.partkey,lineitem.quantity,extendedprice
FROM orders,lineitem,customer
WHERE lineitem.orderkey=orders.orderkey
AND orders.custkey=customer.custkey
AND customer.name='曹玉书';
(11)查询订购“南昌矿山机械厂”制造的“缝盘机”的顾客,显示顾客的编号、姓名:
SELECT customer.custkey,customer.name
FROM customer,orders,part,lineitem
WHERE customer.custkey=orders.custkey
AND lineitem.orderkey=orders.orderkey
AND part.partkey=lineitem.partkey
AND mfgr='南昌矿山机械厂'
AND part.name='缝盘机';
(12)查询订单平均金额>1 万元的顾客中的中国籍顾客的顾客编号、姓名:
SELECT customer.custkey,customer.name
FROM nation,customer,orders
WHERE nation.nationkey=customer.nationkey
AND customer.custkey=orders.custkey
AND nation.`name`='中国'
GROUP BY customer.custkey
HAVING AVG(orders.totalprice)>10000;
(13)查询顾客“刘玉龙”和“钱岚”都订购过的全部零件的信息:
SELECT *
FROM part
WHERE partkey IN(
SELECT part.partkey
FROM part,lineitem,orders,customer
WHERE part.partkey=lineitem.partkey
AND orders.orderkey=lineitem.orderkey
AND orders.custkey=customer.custkey
AND customer.name='刘玉龙'
)
AND partkey IN(
SELECT part.partkey
FROM part,lineitem,orders,customer
WHERE part.partkey=lineitem.partkey
AND orders.orderkey=lineitem.orderkey
AND orders.custkey=customer.custkey
AND customer.name='钱岚'
);
(14)查询顾客“刘玉龙”或“钱岚”订购的全部零件的信息:
SELECT *
FROM part
WHERE partkey IN(
SELECT part.partkey
FROM part,lineitem,orders,customer
WHERE part.partkey=lineitem.partkey
AND orders.orderkey=lineitem.orderkey
AND orders.custkey=customer.custkey
AND customer.name='刘玉龙'
)
OR partkey IN(
SELECT part.partkey
FROM part,lineitem,orders,customer
WHERE part.partkey=lineitem.partkey
AND orders.orderkey=lineitem.orderkey
AND orders.custkey=customer.custkey
AND customer.name='钱岚'
);
(15)查询顾客“刘玉龙”订购过而“钱岚”没订购过的零件的信息。
SELECT *
FROM part
WHERE partkey IN(
SELECT part.partkey
FROM part,lineitem,orders,customer
WHERE part.partkey=lineitem.partkey
AND orders.orderkey=lineitem.orderkey
AND orders.custkey=customer.custkey
AND customer.name='刘玉龙'
)
AND partkey NOT IN(
SELECT part.partkey
FROM part,lineitem,orders,customer
WHERE part.partkey=lineitem.partkey
AND orders.orderkey=lineitem.orderkey
AND orders.custkey=customer.custkey
AND customer.name='钱岚'
);
(1)HAVING:分组后筛选聚合函数来找到特定的组
使用HAVING前必须分组使用GROUP BY,但使用GROUP BY不一的要用HAVING。
(2)SQL语句要注意基本的执行顺序:SELECT、FROM、WHERE、AND、 GROUP BY、ORDER BY、LIMIT