当前位置:网站首页>SQL programming task02 job - basic query and sorting

SQL programming task02 job - basic query and sorting

2022-06-23 01:21:00 Shallow look

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 :
 Insert picture description here

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 :
 Insert picture description here

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 :
 Insert picture description here

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 .
 Insert picture description here
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 :
 Insert picture description here

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 .
 Insert picture description here
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 .
 Insert picture description here
SQL Code :

SELECT *
  FROM product
 ORDER BY if(isnull(regist_date),0,1),regist_date DESC, sale_price;

Reference resources :DataWhale SQL Team learning

原网站

版权声明
本文为[Shallow look]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220918143235.html