当前位置:网站首页>SQL programming task02 job - basic query and sorting
SQL programming task02 job - basic query and sorting
2022-06-23 01:21:00 【Shallow look】
List of articles
1 Operator
1.1 Arithmetic operator
| meaning | Operator |
|---|---|
| Add | + |
| Subtraction | - |
| Multiplication | * |
| division | / |
1.2 Comparison operator
| Operator | meaning |
|---|---|
| = | and ~ equal |
| <> | and ~ It's not equal |
| >= | Greater than or equal to |
| > | Greater than |
| <= | Less than or equal to |
| < | Less than |
2 Exercise part 1 - Inquire about
2.1 To write product form
SQL The code is as follows :
CREATE TABLE product
(product_id CHAR(4) NOT NULL,
product_name VARCHAR(100) NOT NULL,
product_type VARCHAR(32) NOT NULL,
sale_price INTEGER DEFAULT 0,
purchase_price INTEGER ,
regist_date DATE ,
PRIMARY KEY (product_id));
INSERT INTO product VALUES('0001', 'T shirt ', ' clothes ', 1000, 500, '2009-09-20');
INSERT INTO product VALUES('0002', ' Punch ', ' Office Supplies ', 500, 320, '2009-09-11');
INSERT INTO product VALUES('0003', ' motion T T-shirt ', ' clothes ', 4000, 2800, NULL);
INSERT INTO product VALUES('0004', ' kitchen knife ', ' Kitchenware ', 3000, 2800, '2009-09-20');
INSERT INTO product VALUES('0005', ' pressure cooker ', ' Kitchenware ', 6800, 5000, '2009-01-15');
INSERT INTO product VALUES('0006', ' Fork ', ' Kitchenware ', 500, NULL, '2009-09-20');
INSERT INTO product VALUES('0007', ' Clean the board ', ' Kitchenware ', 880, 790, '2008-04-28');
INSERT INTO product VALUES('0008', ' Ball pen ', ' Office Supplies ', 100, NULL, '2009-11-11');
give the result as follows :
2.2 Query statement
Write a SQL sentence , from product( goods ) Select from the table “ Registration date (regist stay 2009 year 4 month 28 After the day ” The goods , Query results should contain product name and regist_date Two .
SQL Code :
select product_name,regist_date
from product
where not regist_date = '2009-01-15'
and not regist_date = '2008-04-28';
Query results :
2.3 NULL Several incorrect query formats
Please say yes to product The table is executed as follows 3 strip SELECT The return result of statement
SELECT *
FROM product
WHERE purchase_price = NULL;
SELECT *
FROM product
WHERE purchase_price <> NULL;
SELECT *
FROM product
WHERE product_name > NULL;
No record can be obtained from the above three query results , give the result as follows :
reason :
selection NULL When recording , You need to use... In conditional expressions IS NULL Operator . I hope the choice is not NULL When recording , You need to use... In conditional expressions IS NOT NULL Operator .
2.4 Conditional query statements + Operator
SELECT Statements can be made from product Take out from the table “ Unit sales price (saleprice) Compared with the purchase price (purchase price) Higher than 500 Above JPY ” The goods . Please write two items that can get the same result SELECT sentence . The results are shown below .
SQL The statement is as follows :
SELECT product_name, sale_price, purchase_price
FROM product
WHERE sale_price-purchase_price >= 500;
SELECT product_name, sale_price, purchase_price
FROM product
WHERE sale_price-500 >= purchase_price;
SELECT product_name, sale_price, purchase_price
FROM product
WHERE sale_price >= purchase_price +500;
2.5 Conditions of the query + The query result is set to another column
SQL Code :
SELECT product_name, product_type,sale_price*0.9-purchase_price as profit
FROM product
WHERE sale_price*0.9-purchase_price>100
and (product_type=' Office Supplies ' or product_type=' Kitchenware ');
give the result as follows :
3 polymerization & Sort
3.1 Aggregate functions
COUNT: Count the number of records in the table ( Row number )
SUM: Calculate the total value of the data in the numerical column of the table
AVG: Calculate the average value of the data in the numerical column in the table
MAX: Find the maximum value of the data in any column of the table
MIN: Find the minimum value of data in any column in the table
3.2 WHERE Under the condition of GROUP BY sentence
SELECT purchase_price, COUNT(*)
FROM product
WHERE product_type = ' clothes '
GROUP BY purchase_price;
GROUP BY There are strict requirements for the writing order of clauses , Failure to comply with the requirements will result in SQL Unable to execute normally , The order of clauses that have appeared at present is :
1 SELECT → 2. FROM → 3. WHERE → 4. GROUP BY
The first three items are used to filter the data ,GROUP BY Process the filtered data
3.3 use HAVING Get specific groups
-- Numbers
SELECT product_type, COUNT(*)
FROM product
GROUP BY product_type
HAVING COUNT(*) = 2;
3.4 ORDER BY Sort
-- Descending order
SELECT product_id, product_name, sale_price, purchase_price
FROM product
ORDER BY sale_price DESC;
-- Multiple sort keys
SELECT product_id, product_name, sale_price, purchase_price
FROM product
ORDER BY sale_price, product_id;
-- When the column name used for sorting contains NULL when ,NULL It will be summarized at the beginning or end .
SELECT product_id, product_name, sale_price, purchase_price
FROM product
ORDER BY purchase_price;
problem : GROUP BY Cannot use... In clause SELECT Clause , But in ORDER BY You can use aliases in clauses . Why is it GROUP BY Not in... But in ORDER BY It's ok ?
This is because SQL In the use of HAVING When clause SELECT The order of statements is :
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY
among SELECT The order of execution is GROUP BY After Clause ,ORDER BY Before clause . in other words , When in ORDER
BY When using aliases in , I already know SELECT The alias set does not exist , But in GROUP BY When you use aliases in, you don't know the existence of aliases , So it can't be ORDER
BY You can use aliases in , But in GROUP BY Alias cannot be used in
4 The second part of the exercise - polymerization & Sort
4.1 Please point out the following SELECT All the grammatical errors in the statement .
SELECT product_id, SUM(product_name)
-- Ben SELECT There is an error in the statement .
FROM product
GROUP BY product_type
WHERE regist_date > '2009-09-01';
Explain :
- SUM, Calculate the total value of the data in the numerical column of the table ,product_name Column is not numeric column data .
- Punctuation marks, such as parentheses, commas, semicolons, use full angles , Half width should be used .
- GROUP BY And WHERE The order is reversed , And now WHERE A semicolon is not required after the statement .
- SELECT If the column name appears in the clause , Can only be GROUP BY The column name specified in clause ( That's the aggregate bond ).
4.2 HAVING Filter aggregate groups
Please write one SELECT sentence , Find out the sales unit price ( sale_price Column ) The total value is greater than the purchase unit price ( purchase_price Column ) Total value 1.5 Times the commodity category . The results are shown below .
SQL Code :
SELECT product_type,sum(sale_price),sum(purchase_price)
FROM product
GROUP BY product_type
HAVING sum(sale_price) > 1.5*sum(purchase_price);
4.3 ORDER BY Sort
We used it before SELECT The sentence is selected product( goods ) All the records in the table . We used ORDERBY Clause to specify the sort order , But now I can't remember how it was designated . Please refer to the following execution results , reflection ORDERBY The content of the clause .
SQL Code :
SELECT *
FROM product
ORDER BY if(isnull(regist_date),0,1),regist_date DESC, sale_price;
Reference resources :DataWhale SQL Team learning
边栏推荐
- Sélecteur de hiérarchie
- Binary tree to string and string to binary tree
- 人民币的单位的大写
- JMeter associated login 302 type interface
- 62. different paths
- Vector 2 (friend and copy construction)
- “听觉”营销价值凸显,喜马拉雅迎来新局点
- Read Amazon memorydb database based on redis
- 層次選擇器
- 【滑动窗口】leetcode992. Subarrays with K Different Integers
猜你喜欢

MySQL - SQL execution process

LeetCode 206. Reverse linked list (iteration + recursion)

Installing MySQL for Linux

LeetCode 206. 反转链表(迭代+递归)

E-R图

TiDB VS MySQL

Binary tree to string and string to binary tree

Overview of visual object detection technology based on deep learning

How to solve the problem that easycvr does not display the interface when RTMP streaming is used?

BGP联邦综合实验
随机推荐
【机器学习-西瓜书】更文挑战【Day1】:1.1 引言
中金证券开户安全吗?它和中金银行是什么关系呢?
SAP ui5 application development tutorial 102 - detailed trial version of print function implementation of SAP ui5 application
Flowable global listener monitors the start and end of a process
Development status of full color LED display
cadence SPB17.4 - 中文UI设置
Shell view help
关于打算做一个web的问题看板,需要使用哪些方面语言及数据库知识!
How about precious metal spot silver?
Dig three feet to solve the data consistency problem between redis and MySQL
Swiftui swift tutorial 14 useful array operators
07 project cost management
Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data
魔王冷饭||#099 魔王说西游;老板的本质;再答中年危机;专业选择
Tidb monitoring upgrade: a long way to solve panic
JS to read the picture of the clipboard
打新债开户安全么,怎么开
Leetcode question brushing - 715 Range module
Shell logs and printouts
62. 不同路径