当前位置:网站首页>Database Experiment 2 data query
Database Experiment 2 data query
2022-04-23 21:45:00 【Hebuter-Rui】
1. The experiment purpose
understand SQL Basic specification of programming , Skilled use SQL Language to achieve data query , Including single table query 、 Grouping statistics query and connection query 、 Nested query and collection query .
2. The contents and requirements of the experiment
in the light of tpch Database or self built database test Design various single table queries 、 Grouping statistics query and connection query 、 Nested query and collection query , Understand and master SQL The characteristics and functions of each clause of the query statement , according to SQL Program design specifications write specific SQL Query statement , And debugging passed .
3. The experimental steps
(1) Query the number and name of each region .
Source code :
SELECT regionkey,name
FROM region;
Results screenshots :
(2) Query the number of each supplier 、 name 、 Address 、 Telephone number and supplier's country name .
Source code :
SELECT
suppkey,
supplier.name,
address,phone,
nation.name
FROM
nation,
supplier
WHERE
supplier.nationkey = nation.nationkey;
Results screenshots :
(3) Inquire about 2021 year 9 The amount of each order in the month exceeds 10000 Order number of yuan 、 Customer name 、 Customer's country name 、 And order date .
Source code :
SELECT
orderkey,
customer.name,
nation.name,
orderdate
FROM
nation,
customer,
orders
WHERE
customer.nationkey = nation.nationkey
AND customer.custkey = orders.custkey
AND YEAR(orderdate) = '2021'
AND MONTH(orderdate) = '9'
AND totalprice > 10000;
There is a problem of no result in the query process , It is verified that the date of the data imported into the table is empty , The data is processed and re imported, which solves the problem of no result .
Query results :
(4) Count each customer in 2020 Total order amount for the year , Display customer number 、 Customer name 、 Total order amount .
Source code :
SELECT
customer.custkey,
name,SUM(totalprice) AS sum_totalprice
FROM
customer,
orders
WHERE
customer.custkey = orders.custkey
AND YEAR(orderdate) = '2020'
GROUP BY customer.custkey
ORDER BY customer.custkey;
Query results :
(5) The average amount of the query order exceeds 5 Customer number of 10000 yuan 、 full name 、 Country name and average order amount .
Source code :
SELECT
customer.custkey,
customer.name,
nation.name,
AVG(totalprice)
FROM
customer,
orders,
nation
WHERE
customer.custkey = orders.custkey
AND customer.nationkey = nation.nationkey
GROUP BY customer.custkey
HAVING AVG(totalprice)>50000
ORDER BY customer.custkey;
Query results :
(6) Query and “ Jinshi Printing Co., Ltd ” Supplier number in the same country 、 Name and address information .
Source code :
SELECT
sup.suppkey,
sup.name,
sup.address
FROM
supplier sup,
supplier supp
WHERE
sup.nationkey = supp.nationkey
AND supp.name = ' Jinshi Printing Co., Ltd ';
Use two aliases , Will a supplier Tables are used as two tables
Query results :
(7) Query the part number where the supply price is less than the retail price 、 Part name 、 manufacturer 、 brand 、 wholesale price 、 Supplier name and supply price .
Source code :
SELECT
part.partkey,
part.name,
mfgr,
brand,
retailprice,
supplier.name,
supplycost
FROM
supplier,
part,
partsupp
WHERE
partsupp.supplycost < part.retailprice
AND partsupp.partkey = part.partkey
AND partsupp.suppkey = supplier.suppkey;
Query results :
(8) Enquiring customers “ Cao Yushu ” Order number of the order 、 The order amount and the part number ordered 、 Quantity and order details price .
Source code :
SELECT
orders.orderkey,
totalprice,
lineitem.partkey,
quantity,
extendedprice
FROM
lineitem,
orders,
customer
WHERE
customer. name = ' Cao Yushu '
AND lineitem.orderkey = orders.orderkey
AND customer.custkey = orders.custkey;
Query results :
(9) Inquired and ordered “ Nanchang Mining Machinery Factory ” Made of “ Sewing machine ” Your customer number 、 full name .
Source code :
SELECT
customer.custkey,
customer.name,
FROM
lineitem,
orders,
customer,
part
WHERE
part.mfgr = ' Nanchang Mining Machinery Factory '
AND part.name = ' Sewing machine '
AND part.partkey = lineitem.partkey
AND lineitem.orderkey = orders.orderkey
AND customer.custkey = orders.custkey;
Query results :
(10) The average amount of the query order exceeds 1 The customer number of Chinese customers among 10000 yuan customers 、 full name .
Source code :
SELECT
customer.custkey,
customer.name
FROM
nation,
orders,
customer
WHERE
customer.nationkey = nation.nationkey
AND nation.name = ' China '
AND customer.custkey = orders.custkey
GROUP BY customer.custkey
HAVING AVG(totalprice) > 10000
ORDER BY customer.custkey;
Query results :
(11) Enquiring customers “ Liu Yulong ” and “ Qian LAN ” Information on all parts ordered .
Source code :
SELECT
*
FROM
part
WHERE
part.partkey IN
(
SELECT lineitem.partkey
FROM
lineitem
WHERE
lineitem.orderkey IN
(
SELECT orders.orderkey
FROM
orders
WHERE
orders.custkey IN
(
SELECT customer.custkey
FROM
customer
WHERE
customer.name = ' Liu Yulong '
)
)
)
AND
part.partkey IN
(
SELECT lineitem.partkey
FROM
lineitem
WHERE
lineitem.orderkey IN
(
SELECT orders.orderkey
FROM
orders
WHERE
orders.custkey IN
(
SELECT customer.custkey
FROM
customer
WHERE
customer.name = ' Qian LAN '
)
)
);
Query results :
(12) Enquiring customers “ Liu Yulong ” or “ Qian LAN ” Information on all parts ordered .
Source code :
SELECT
*
FROM
part
WHERE
part.partkey IN
(
SELECT lineitem.partkey
FROM
lineitem
WHERE
lineitem.orderkey IN
(
SELECT orders.orderkey
FROM
orders
WHERE
orders.custkey IN
(
SELECT customer.custkey
FROM
customer
WHERE
customer.name = ' Liu Yulong '
OR customer.name = ' Qian LAN '
)
)
);
Query results ;
(13) Enquiring customers “ Liu Yulong ” Ordered but “ Qian LAN ” Information on parts not ordered .
Source code :
SELECT
*
FROM
part
WHERE
part.partkey IN
(
SELECT lineitem.partkey
FROM
lineitem
WHERE
lineitem.orderkey IN
(
SELECT orders.orderkey
FROM
orders
WHERE
orders.custkey IN
(
SELECT customer.custkey
FROM
customer
WHERE
customer.name = ' Liu Yulong '
AND customer.name != ' Qian LAN '
)
)
);
Query results :
版权声明
本文为[Hebuter-Rui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204200618536997.html
边栏推荐
- Question brushing plan - depth first search (II)
- ERP function_ Financial management_ The difference between red and blue words in invoices
- Problem brushing plan -- dynamic programming (IV)
- NVM introduction, NVM download, installation and use (node version management)
- Preliminary analysis of Airbase
- Sklearn function: Train_ Test_ Split (split training set and test set)
- Pycharm Chinese plug-in
- Online timing flow chart making tool
- Graph traversal - BFS, DFS
- JS merge duplicate data in array object
猜你喜欢
Two Stage Detection
1. Finishing huazi Mianjing -- 1
Rust更适合经验较少的程序员?
Correction of date conversion format error after Oracle adds a row total
wait、waitpid
Online Excel to CSV tool
thinkphp5+数据大屏展示效果
ROS learning notes - tutorial on the use of ROS
[leetcode refers to the two numbers of offer 57. And S (simple)]
随机推荐
Subcontracting of wechat applet based on uni app
Opencv application -- jigsaw puzzle
[leetcode refers to the substructure of offer 26. Tree (medium)]
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
在线时序流程图制作工具
Keywords static, extern + global and local variables
Thread safe sigleton (singleton mode)
ERP function_ Financial management_ The difference between red and blue words in invoices
Pytorch selects the first k maximum (minimum) values and their indexes in the data
Error message: b04access.00f eve'. Read of address 000001B4
Pipes and xargs
NVM introduction, NVM download, installation and use (node version management)
Plato Farm元宇宙IEO上线四大,链上交易颇高
[※ leetcode refers to offer 48. The longest substring without repeated characters (medium)]
ros功能包内自定义消息引用失败
How Axure installs a catalog
setInterval、setTimeout、requestAnimationFrame
[leetcode refers to offer 18. Delete the node of the linked list (simple)]
unity 功能扩展
ubutnu20安裝CenterNet