SQL study notes 2 - basic query and sorting

Create the database to be used

Select open and find the shop.sql file.
insert image description here
insert image description here
insert image description here
After the creation is complete, you can view the table data.
insert image description here

2.1 SELECT statement basics

2.1.1 Select data from the table

SELECT statement

When selecting data from a table, you need to use the SELECT statement, which means only selecting (SELECT) the necessary data from the table. The process of querying and selecting the necessary data through the SELECT statement is called a matching query or query (query).

The basic SELECT statement contains two clauses, SELECT and FROM. Examples are as follows:

SELECT <列名>, 
  FROM <表名>;

Among them, the SELECT clause enumerates the name of the column that you want to query from the table, and the FROM clause specifies the name of the table from which the data is selected.

2.1.2 Select qualified data from the table

WHERE statement

When it is not necessary to retrieve all the data, but to select data that meets certain conditions such as "the product type is clothing" and "the sales unit price is more than 1,000 yen", use the WHERE statement.

The SELECT statement specifies the conditions for querying data through the WHERE clause. In the WHERE clause, conditions such as "the value of a certain column is equal to this string" or "the value of a certain column is greater than this number" can be specified. Execute the SELECT statement containing these conditions, and you can query only the records that meet the conditions.

SELECT <列名>, ……
  FROM <表名>
 WHERE <条件表达式>;

Compare the difference between the following two output results:

-- 用来选取product type列为衣服的记录的SELECT语句
SELECT product_name, product_type
  FROM product
 WHERE product_type = '衣服';
-- 也可以选取出不是查询条件的列(条件列与输出列不同)
SELECT product_name
  FROM product
 WHERE product_type = '衣服';

insert image description here insert image description here

2.1.3 Related laws

  • An asterisk (*) represents the meaning of all columns.
  • Line breaks can be used freely in SQL without affecting statement execution (but blank lines cannot be inserted).
  • When setting a Chinese alias, you need to use double quotation marks (") to enclose it.
  • Use DISTINCT in the SELECT statement to remove duplicate rows.
  • A comment is a part of an SQL statement used to identify instructions or precautions. It is divided into two types: 1-line comment "-- " and multi-line comment "/* */".
-- 想要查询出全部列时,可以使用代表所有列的星号(*)。
SELECT *
  FROM <表名>-- SQL语句可以使用AS关键字为列设定别名(用中文时需要双引号(“”))。
SELECT product_id     As id,
       product_name   As name,
       purchase_price AS "进货单价"
  FROM product;
-- 使用DISTINCT删除product_type列中重复的数据
SELECT DISTINCT product_type
  FROM product;

insert image description here
insert image description here insert image description here

2.2 Arithmetic and comparison operators

2.2.1 Arithmetic operators

The main operators of the four arithmetic operations that can be used in SQL statements are as follows:

meaning operator
addition +
subtraction -
multiplication *
division /

2.2.2 Comparison operators

-- 选取出sale_price列为500的记录
SELECT product_name, product_type
  FROM product
 WHERE sale_price = 500;

insert image description here

Common comparison operators in SQL are as follows:

operator meaning
= equal to ~
<> is not equal to ~
>= greater than or equal to ~
> greater than ~
<= less than or equal to ~
< less than ~

2.2.3 Common rules

  • Constants or expressions can be used in the SELECT clause.
  • When using comparison operators, be sure to pay attention to the position of the inequality and equal signs.
  • In principle, data of the string type is sorted according to the dictionary order, which cannot be confused with the numerical order.
  • When you want to select NULL records, you need to use the IS NULL operator in the conditional expression. When you want to select records that are not NULL, you need to use the IS NOT NULL operator in the conditional expression.

The relevant code is as follows:

-- SQL语句中也可以使用运算表达式
SELECT product_name, sale_price, sale_price * 2 AS "sale_price x2"
  FROM product;
-- WHERE子句的条件表达式中也可以使用计算表达式
SELECT product_name, sale_price, purchase_price
  FROM product
 WHERE sale_price - purchase_price >= 500;
/* 对字符串使用不等号
首先创建chars并插入数据
选取出大于‘2’的SELECT语句*/
-- DDL:创建表
CREATE TABLE chars
(chr CHAR3NOT NULL, 
PRIMARY KEY(chr));
-- 选取出大于'2'的数据的SELECT语句('2'为字符串)
SELECT chr
  FROM chars
 WHERE chr > '2';
-- 选取NULL的记录
SELECT product_name, purchase_price
  FROM product
 WHERE purchase_price IS NULL;
-- 选取不为NULL的记录
SELECT product_name, purchase_price
  FROM product
 WHERE purchase_price IS NOT NULL;

insert image description here insert image description here
insert image description here insert image description here
insert image description here

2.3 Logical operators

2.3.1 NOT operator

不是……When you want to express , in addition to the above <> operator, there is another operator that expresses negation and has a wider range of use: NOT.

NOT cannot be used alone, it must be used in combination with other query conditions. For example:

Select the records whose sales unit price is greater than or equal to 1000 yen

SELECT product_name, product_type, sale_price
  FROM product
 WHERE sale_price >= 1000;

The query results are as follows:

insert image description here

Add the NOT operator to the query condition of the above SELECT statement

SELECT product_name, product_type, sale_price
  FROM product
 WHERE NOT sale_price >= 1000;

The query results are as follows:

insert image description here

It can be seen that by negating the query condition that the sales unit price is greater than or equal to 1000 yen (sale_price >= 1000), the products whose sales unit price is less than 1000 yen are selected. That is to say NOT sale_price >= 1000and sale_price < 1000are equivalent.

It is worth noting that although negating a condition through the NOT operator can obtain the result of the opposite query condition, its readability is obviously not as good as specifying the query condition explicitly, so this operator cannot be abused.

2.3.2 AND operator and OR operator

When you want to use multiple query conditions at the same time, you can use AND or OR operators.

AND is equivalent to "and", similar to the intersection in mathematics;

OR is equivalent to "or", similar to the union in mathematics.

As shown below:

AND operator working effect diagram

insert image description here

OR operator working effect diagram

insert image description here

Precedence by parentheses

What should I do if I want to find such a product?

"Product type is office supplies" and "registration date is September 11, 2009 or September 20, 2009" the
ideal result is "hole punch", but when you enter the following information, you will get the wrong result

-- 将查询条件原封不动地写入条件表达式,会得到错误结果
SELECT product_name, product_type, regist_date
  FROM product
 WHERE product_type = '办公用品'
   AND regist_date = '2009-09-11'
    OR regist_date = '2009-09-20';

insert image description here

The reason for the error is that the AND operator takes precedence over the OR operator . If you want to perform the OR operation first, you can use parentheses :

-- 通过使用括号让OR运算符先于AND运算符执行
SELECT product_name, product_type, regist_date
  FROM product
 WHERE product_type = '办公用品'
   AND ( regist_date = '2009-09-11'
        OR regist_date = '2009-09-20');

insert image description here

Please refer to the figure below for operator precedence

image

2.3.3 Truth table

How to understand complex operations?

When encountering sentences with complex conditions, it is not easy to understand the meaning of the sentence. At this time, the truth table can be used to sort out the logical relationship.

What is the truth value?

The three operators NOT, AND, and OR described in this section are called logical operators. The logic mentioned here means to operate on the truth value. A truth value is a value that is either TRUE or FALSE.

For example, for the query condition sale_price >= 3000, since the value of the sale_price column of the record whose product_name is 'sports T-shirt' is 2800, it will return false (FALSE), while the value of the record whose product_name is 'pressure cooker' The value of the sale_price column is 5000, so it returns TRUE.

The AND operator returns true if the truth values ​​on both sides are true, otherwise it returns false.

The OR operator returns true as long as one of the truth values ​​on both sides is not false, and returns false only if the truth values ​​on both sides of it are false.

The NOT operator simply converts true to false and false to true.

truth table

insert image description here

The truth table whose query condition is P AND (Q OR R)

insert image description here

true value if contains NULL

A truth value of NULL results in neither true nor false, since no such value is known.

How should that be expressed?

At this time, the truth value is the third value besides true and false— uncertainty (UNKNOWN). There is no such third value in general logical operations. Languages ​​other than SQL also basically use only two truth values, true and false. In contrast to the usual logical operations called binary logic, only the logical operations in SQL are called ternary logic.

The AND and OR truth tables for three-valued logic are:

insert image description here

Practice Questions - Part 1

2.1

productWrite a SQL statement to select regist_datethe products whose registration date ( ) is after April 28, product name2009 from the (commodity) table, and the query result should contain and regist_datetwo columns.
insert image description here

2.2

Please state the results returned when the following 3 SELECT statements are executed on the product table.

SELECT *
  FROM product
 WHERE purchase_price = NULL;

SELECT *
  FROM product
 WHERE purchase_price <> NULL;

SELECT *
  FROM product
 WHERE product_name > NULL;

According to my understanding, the picture below
insert image description here
shows an empty table after I run it. . .

2.3

2.2.3The SELECT statement in the chapter productcan retrieve the products whose "sales unit price ( sale_price) is more than 500 yen higher than the purchase unit price ( )" from the table. purchase_priceWrite two SELECT statements that get the same result. The execution result is as follows:

product_name | sale_price | purchase_price 
-------------+------------+------------
T恤衫        |   1000    | 500
运动T恤      |    4000    | 2800
高压锅       |    6800    | 5000

insert image description here
insert image description here

2.4

Please write a SELECT statement to select records from productthe table that meet 100the condition of "office supplies and kitchen utensils whose profit is higher than Japanese yen after a 10% discount on the sales unit price". The query result should include product_namecolumn , product_typecolumn and the profit after 10% discount of the sales unit price (alias is set to profit).

Tips: 10% discount on the sales unit price can be obtained by multiplying the value in sale_pricethe column by 0.9, and the profit can be purchase_priceobtained by subtracting the value in the column from this value.
insert image description here

2.4 Aggregate queries on tables

2.4.1 Aggregate functions

The functions used for aggregation in SQL are called aggregate functions. The following five are the most commonly used aggregate functions:

  • SUM: Calculate the total value in a numeric column in the table

  • AVG: calculates the average value in a numeric column in a table

  • MAX: Calculate the maximum value of data in any column in the table, including text and number types

  • MIN: Calculate the minimum value of data in any column in the table, including text and number types

  • COUNT: Calculate the number of records (rows) in the table

Please use shopthe database to execute the following SQL query statements to understand and master the general usage of aggregate functions:

-- 计算销售单价和进货单价的合计值
SELECT SUM(sale_price), SUM(purchase_price) 
  FROM product;
-- 计算销售单价和进货单价的平均值
SELECT AVG(sale_price), AVG(purchase_price)
  FROM product;
-- 计算销售单价的最大值和最小值
SELECT MAX(sale_price), MIN(sale_price)
  FROM product;
-- MAX和MIN也可用于非数值型数据
SELECT MAX(regist_date), MIN(regist_date)
  FROM product;
-- 计算全部数据的行数(包含 NULL 所在行)
SELECT COUNT(*)
  FROM product;
-- 计算 NULL 以外数据的行数
SELECT COUNT(purchase_price)
  FROM product;

insert image description here insert image description here
insert image description here insert image description here
insert image description here insert image description here

Use DISTINCT to perform aggregation operations to remove duplicate values

When performing aggregation operations on the entire table, there may be multiple rows of the same data in the table, such as product type (product_type column).

In some scenarios, aggregation functions cannot be used directly for aggregation operations, and must be used with DISTINCTfunctions .

For example: To calculate how many types of coffee are on sale in total, how to calculate it?

As mentioned above, DISTINCTthe function is used to delete duplicate data. Before applying the COUNT aggregation function, the requirement can be realized by adding DISTINCTthe keyword .

SELECT COUNT(DISTINCT product_type)
  FROM product;

insert image description here

2.4.2 Aggregation function application rules

  • The operation result of the COUNT aggregation function is related to the parameters. COUNT(*) / COUNT(1) gets all rows containing NULL values, and COUNT(<column name>) gets all rows not containing NULL values.

  • Aggregate functions do not handle rows containing NULL values, with the exception of COUNT(*).

  • MAX / MIN functions are available for both text type and numeric type columns, while SUM / AVG functions are only available for numeric type columns.

  • Use the DISTINCT keyword in the parameters of the aggregation function to get the aggregation result with duplicate values ​​removed.

2.5 Grouping tables

2.5.1 GROUP BY statement

Previously, the aggregation function was used to process the data of the entire table. When you want to group and summarize (ie: summarize the existing data according to a certain column), GROUP BY can help you:

SELECT <列名1>,<列名2>, <列名3>, ……
  FROM <表名>
 GROUP BY <列名1>, <列名2>, <列名3>, ……;

Take a look at the difference with and without the GROUP BY statement:

-- 按照商品种类统计数据行数
SELECT product_type, COUNT(*)
  FROM product
 GROUP BY product_type;
 -- 不含GROUP BY
SELECT product_type, COUNT(*)
  FROM product

insert image description here insert image description here

Segment the table by product type

insert image description here

In this way, the GROUP BY clause groups the table like a slice of cake. The columns specified in the GROUP BY clause are called aggregation keys or grouping columns .

When the aggregation key contains NULL

Take the purchase unit price (purchase_price) as an example of aggregation key:

SELECT purchase_price, COUNT(*)
  FROM product
 GROUP BY purchase_price;

At this time, NULL will be aggregated as a set of special data
insert image description here

GROUP BY writing position

There are strict requirements for the writing order of the clauses of GROUP BY. If the requirements are not followed, the SQL cannot be executed normally. The order of the clauses that have appeared so far is:

  1. SELECT ➡️ 2. FROM ➡️ 3. WHERE ➡️ 4. GROUP BY

Among them, the first three items are used to filter data, and GROUP BY processes the filtered data

Use GROUP BY in the WHERE clause

SELECT purchase_price, COUNT(*)
  FROM product
 WHERE product_type = '衣服'
 GROUP BY purchase_price;

insert image description here

2.5.2 Common errors

When using aggregate functions and GROUP BY clauses, common errors are:

  1. When a column other than the aggregate key is written in the SELECT clause of the aggregate function and an aggregate function such as COUNT is used, if the column name appears in the SELECT clause, it can only be the column name specified in the GROUP BY clause (that is, the aggregate key).
  2. Using the alias of the column in the GROUP BY clause The alias can be specified by AS in the SELECT clause, but the alias cannot be used in the GROUP BY. Because in DBMS, the SELECT clause is executed after the GROUP BY clause.
  3. The reason for using aggregate functions in WHERE is that the premise of using aggregate functions is that the result set has been determined, and WHERE is still in the process of determining the result set, so conflicts will cause errors. If you want to specify conditions, you can use aggregate functions in the SELECT, HAVING (discussed shortly below), and ORDER BY clauses.

2.6 Specify conditions for aggregation results

2.6.1 Get a specific group with HAVING

We learned how to get grouped aggregation results, now let’s think about it, how to get partial results of grouped aggregation results?

After the table is grouped by GROUP BY, how can I only take out two groups?

insert image description here

Here WHERE is not feasible, because the WHERE clause can only specify the conditions of records (rows), but cannot be used to specify the conditions of groups (for example, "the number of data rows is 2 rows" or "the average value is 500", etc.).

You can use the HAVING clause after the GROUP BY.

HAVING is used like WHERE.

It is worth noting that the HAVING clause must be used in conjunction with the GROUP BY clause, and it limits the grouping and aggregation results, while the WHERE clause limits the data rows (including the grouping columns). The two perform their respective duties and should not be confused.

2.6.2 Features of HAVING

The HAVING clause is used to filter the group, and you can use constants, aggregate functions, and column names (aggregate keys) specified in GROUP BY.

-- 常数
SELECT product_type, COUNT(*)
  FROM product
 GROUP BY product_type
HAVING COUNT(*) = 2;

-- 错误形式(因为product_name不包含在GROUP BY聚合键中)
SELECT product_type, COUNT(*)
  FROM product
 GROUP BY product_type
HAVING product_name = '圆珠笔';

insert image description here insert image description here

2.7 Sorting query results

2.7.1 ORDER BY

In some scenarios, it is necessary to obtain a sorted result, such as the scores of athletes in the Olympic Games. The organizing committee uses the reverse score results to determine who will win the gold, silver and bronze medals. The execution results of SQL statements are randomly arranged by default, and if you want to sort them in order, you need to use the ORDER BY clause.

SELECT <列名1>, <列名2>, <列名3>, ……
  FROM <表名>
 ORDER BY <排序基准列1> [ASC, DESC], <排序基准列2> [ASC, DESC], ……

Among them, the parameter ASC means sorting in ascending order, DESC means sorting in descending order, and the default is ascending order. At this time, the parameter ASC can be defaulted.

The following code will get the query results sorted by sales price in reverse order:

-- 降序排列
SELECT product_id, product_name, sale_price, purchase_price
  FROM product
 ORDER BY sale_price DESC;

insert image description here

If there are multiple column sorting requirements, just write the sorting column + sorting parameters in the ORDER BY clause in turn, see the following code for details:

-- 多个排序键
SELECT product_id, product_name, sale_price, purchase_price
  FROM product
 ORDER BY sale_price, product_id;

insert image description here

It needs to be specially noted: Since NULL cannot be compared using comparison operators, that is, it cannot be compared with text types, number types, date types, etc., when there is a NULL value in the sorting column, the NULL result will be displayed in the query result beginning or end.

-- 当用于排序的列名中含有NULL时,NULL会在开头或末尾进行汇总。
SELECT product_id, product_name, sale_price, purchase_price
  FROM product
 ORDER BY purchase_price;

insert image description here

2.7.2 Using aliases in the ORDER BY clause

As mentioned above in GROUP BY, the alias defined in the SELECT clause cannot be used in the GROUP BY clause, but the alias can be used in the ORDER BY clause. Why not in GROUP BY but in ORDER BY?

This is because the execution order of the SELECT statement when using the HAVING clause in SQL is:

FROM → WHERE → GROUP BY → SELECT → HAVING → ORDER BY

The execution sequence of SELECT is after the GROUP BY clause and before the ORDER BY clause.

When the alias is used in the ORDER BY clause, the alias set by the SELECT clause is already known, but the existence of the alias is not known when the GROUP BY clause is executed, so the alias can be used in the ORDER BY clause, but in the GROUP Aliases cannot be used in BY.

2.7.3 ORDER BY encounters NULL

In MySQL, NULLvalues ​​are considered lower than any other 非NULLvalue , so when the order is ASC (ascending), NULLthe value appears first, and when the order is DESC (descending), it sorts last.

If you want to specify that NULLexisting lines appear in the first or last line, special handling is required.

Build the example table using the following code:

CREATE TABLE user (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(50),
    date_login DATE,
    PRIMARY KEY (id)
);

INSERT INTO user(name, date_login) VALUES
(NULL,    '2017-03-12'), 
('john',   NULL), 
('david', '2016-12-24'), 
('zayne', '2017-03-02');

Since when sorting, NULLthe value of is lower than 非NULLthe value of (can be understood as 0or -∞), then we have to specialize this default case when sorting to achieve the desired effect.

Generally, there are two requirements:

  • NULLSorts the values ​​in the last row, and also 非NULLsorts all the values ​​in ascending order.

For numeric or date types, you can add a minus sign (minus) before the sort field to get reverse sorting. ( -1、-2、-3....-∞)

SELECT * FROM user 
 ORDER BY -date_login DESC;

insert image description here

For characters or character numbers, this method may not be able to get the expected sorting results, you can use IS NULLcomparison operators . Additionally ISNULL( )the function is equivalent to using IS NULLthe comparison operator.

-- IS NULL
SELECT * FROM user 
 ORDER BY name IS NULL ASC,name ASC;
 
-- ISNULL()
SELECT * FROM user 
 ORDER BY ISNULL(name) ASC,name ASC;

insert image description here insert image description here

The above statement first uses ISNULL(name)the field to sort in ascending order, and only when namethe column value NULLis , ISNULL(name)it is true, so it is sorted to the last row, name ASCand realizes 非NULLthe sorting of values ​​in ascending order.

You can also use COALESCEthe function to achieve the requirements

SELECT * FROM user 
 ORDER BY COALESCE(name, 'zzzzz') ASC;

insert image description here

  • NULLSorts the value in the first row and 非NULLsorts all the values ​​in descending order.

For numeric or date types, you can add a minus sign (minus) before the sort field to achieve. ( -∞...-3、-2、-1)

SELECT * FROM user 
 ORDER BY -date_login ASC;

insert image description here

For characters or character numbers, this method may not be able to get the expected sorting results, you can use IS NOT NULLcomparison operators . Additionally !ISNULL( )the function is equivalent to using IS NOT NULLthe comparison operator.

-- IS NOT NULL
SELECT * FROM user 
 ORDER BY name IS NOT NULL ASC,name DESC;

-- !ISNULL()
SELECT * FROM user 
 ORDER BY !ISNULL(name) ASC,name DESC;

insert image description here insert image description here

The above statement first uses !ISNULL(name)the field to sort in ascending order, and only when thename value of the column is not , it is true, so it is sorted to the row, and the value of is sorted in descending order.NULL!ISNULL(name)name DESC非NULL

You can also use COALESCEthe function to achieve the requirements

SELECT * FROM user 
 ORDER BY COALESCE(name, 'zzzzz') DESC;

insert image description here

Practice Questions - Part 2

2.5

Please point out any syntax errors in the following SELECT statements.

SELECT product_id, SUM(product_name)
--本SELECT语句中存在错误。
  FROM product 
 GROUP BY product_type 
 WHERE regist_date > '2009-09-01';

Error ①
The character field product_name cannot perform SUM aggregation
Error ②
The WHERE statement should be written before the GROUP BY statement (after the FROM statement)
Error ③
The GROUP BY field (product_type) is different from the SELECT field (product_id)

2.6

Please write a SELECT statement to find the commodity types whose total sales unit price ( sale_pricecolumn) is greater than 1.5 times the total purchase unit price ( column). purchase_priceThe execution result is as follows.

product_type | sum  | sum 
-------------+------+------
衣服         | 5000 | 3300
办公用品      |  600 | 320

insert image description here
insert image description here

2.7

Previously, we used the SELECT statement to select all the records in the product (commodity) table. At the time we used ORDER BYthe clause to specify the sort order, but I can't remember how to specify it now. Consider the contents of ORDER BYthe clause .

insert image description here

insert image description here

learning reference

Learning reference: https://github.com/datawhalechina/wonderful-sql

Guess you like

Origin blog.csdn.net/weixin_45735391/article/details/126804332