当前位置:网站首页>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
边栏推荐
- Opencv reports an error. Expected PTR < CV:: UMAT > for argument '% s'‘
- YOLOv5 Unable to find a valid cuDNN algorithm to run convolution
- [leetcode refers to the substructure of offer 26. Tree (medium)]
- ubutnu20安装CenterNet
- flomo软件推荐
- 亚马逊和Epic将入驻,微软应用商城向第三方开放
- Daily operation and maintenance knowledge -- 1
- Tensorflow1. X and 2 How does x read those parameters saved in CKPT
- Display, move, rotate
- C reads excel specific data into specific columns of DataGridView
猜你喜欢

Yolov5 NMS source code understanding

Display, move, rotate

ROS学习笔记-----ROS的使用教程

Express③(使用Express编写接口、跨域有关问题)

airbase 初步分析

Two Stage Detection
![[leetcode refers to offer 10 - I. Fibonacci sequence (simple)]](/img/f9/22a379f330c3ee21a2a386bbd4a98f.png)
[leetcode refers to offer 10 - I. Fibonacci sequence (simple)]

Thinkphp5 + data large screen display effect

Pycharm download and installation

2. Finishing huazi Mianjing -- 2
随机推荐
从严从重从快 上海全面加强疫情期间食品安全监管
Deep analysis of C language pointer (Part I)
Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
Oracle updates the data of different table structures and fields to another table, and then inserts it into the new table
ubutnu20安裝CenterNet
Automatic heap dump using MBean
在线时序流程图制作工具
Is rust more suitable for less experienced programmers?
ROS learning notes - tutorial on the use of ROS
管道和xargs
IOT design and development
Lunch on the 23rd day at home
[※ leetcode refers to offer 48. The longest substring without repeated characters (medium)]
Online timing flow chart making tool
Sequential state
Idea import a project
Error message: b04access.00f eve'. Read of address 000001B4
1.整理华子面经--1
How Axure installs a catalog
YOLOv5 Unable to find a valid cuDNN algorithm to run convolution