sql must know must know (fifth edition) learning summary (two)-mysql retrieval and filtering data

This blog is a summary of the fifth edition of "SQL Must Know Must Know".

For the table name and column name of the article, please refer to the book "SQL Must Know and Know" and the previous blog

sql must know must know (fifth edition) learning summary (1) - mysql preliminary knowledge summary

table of Contents

1: Retrieve data

1.1 Retrieve a certain column

1.2 Retrieve multiple columns

1.3 Retrieve all columns

1.4 distinct keyword

1.5 limit keyword

1.6 order by clause (desc)

2: Advanced data filtering

2.1where clause

2.2 between

2.3 is null

2.4 or / and

2.5 in / not

3. Wildcards for filtering

3.1 Wildcards and search patterns

3.2 like operator and its wildcard

3.3 Tips for using wildcards

4. Calculated fields

4.1 Why use calculated fields

4.2 splicing fields

4.3 Aliases

5. Use functions to process data

5.1 The role of functions

6. Aggregate data

6.1 Aggregate functions

6.2 Commonly used aggregate functions

7. Group data

7.1 Create Group

7.2 GROUP BY clause rules:

7.3 Filter group

7.4 The difference between group by and order by

7.5 select clause order

8. Subqueries

9. Join table

9.1 Why use joins?

9.2 Create Connection-Inner Connection

9.3 Create a connection-self-connection

9.4 Create connection-external connection

9.5 Use connection conditions

10. Combination query


1: Retrieve data

1.1 Retrieve a certain column

select prod_name from Products;

Use the select statement to search, use the from keyword to specify the database.

1.2 Retrieve multiple columns

select prod_name , prod_id , prod_price from Products;

Use commas to separate table names

1.3 Retrieve all columns

select * from Products;

Use wildcards to return all columns in the table

1.4 distinct keyword

select distinct vend_id 
from Products;

Use the distinct keyword to instruct the database to return only different values . Note that using the distinct keyword, it must be placed directly in front of the column.

In addition, the distinct keyword applies to all columns, not just the columns following it . (Understand for yourself: first get the result of select, and then remove the duplicate from the result)

1.5 limit keyword

     The select statement returns all matching rows in the specified table. If you only want to return the first row or a certain number of rows, you need to use the limit clause .

select prod_name
from Products
limit 5;

If you want to get the next 5 rows of data, you need to specify the number of rows to be retrieved and where to start the retrieval.

limit 5 offset 3

Where 5 refers to the number of rows retrieved, and 3 refers to where to start.

1.6 order by clause (desc)

In order to clearly sort the data retrieved with select, you can use the order by clause, which takes the name of one or more columns, and sorts the output accordingly.

select prod_name 
from Products
order by prod_name;

[Note 1]: The position of the order by clause. When specifying an order by clause, you should ensure that it is the last clause of the select statement. If it is not the last clause, an error will occur

[Note 2]: It is often necessary to sort data by more than one column. To sort by multiple columns, you only need to specify these column names. Use commas to separate the column names, which means that you only need to write an order by.

select prod_id, prod_price, prod_name
from Products
order by prod_price  , prod_name;

[Note 3]: In addition to sorting by column name, order by also supports sorting by relative position

order by 2,3

[Note 4:] Data sorting is not limited to ascending sorting, this is only the default sorting order, you can also specify the desc keyword for descending sorting

             desc: The desc keyword is only applied to the column name directly in front of it. If you want to sort multiple columns in descending order, you must specify the desc keyword for each column

order by prod_price desc , prod_name;

2: Advanced data filtering

2.1where clause

(1) The data is filtered according to the search conditions specified in the where clause, and the where clause is given after the table name (from clause)

(2) When using order by and where clauses at the same time, order by should be placed after where.

(3) The operators of the where clause are: between, is null,>, <etc.

select vend_id , prod_name
from Products
where vend_id <> 'DLL01';

[Note]: Single quotation marks are used to limit the string. If you compare the value with a string type column, you need to limit the quotation marks.

For comparison with numeric columns, no quotes are required.

2.2 between

When using between, you must specify two values, the low-end value and the high-end value of the required range, and these two values ​​must be separated by the and keyword .

Between matches all values ​​in the range, including the specified start value and end value .

select prod_name , prod_price
from Products
where prod_price between 5 and 10;

2.3 is null

 Check if there is a null column

select  prod_name 
from Products
where prod_price is null;

2.4 or / and

(1) In order to have stronger filtering control, sql allows multiple where clauses to be given. These where clauses can be used in two ways, namely in the and clause or the or clause.

(2) Like most languages, before the or operator is processed, the and operator is processed first. If you want to change the priority, you can use parentheses to group the operators explicitly.

select prod_id , prod_price , prod_name

from Products

where vend_id = 'DLL01' or vend_id = 'BRS01';

2.5 in / not

1) The in operator is used to specify the range of conditions, and each condition in the range can be matched. in takes a set of legal values ​​separated by commas and enclosed in parentheses .

where vend_id in ('DLL01','BRS01');

Observe carefully, the in operator performs the same function as or.

2) The role of not in sql: negate any conditions that follow it, because not is never used alone (it is always used with other operators)

The not in the where clause is used to negate the keywords in the subsequent conditions

select prod_name 
from  products
where not vend_id = 'DLL01'
order by prod_name;

The function of the above statement is to list all suppliers except DLL01.

3. Wildcards for filtering

3.1 Wildcards and search patterns

Wildcard: Part of the special characters used to match the value

Search mode: search conditions composed of literal values, wildcards or a combination of both

Wildcard search can only be used in text fields (strings), non-text data type fields cannot use wildcard search

3.2 like operator and its wildcard

Wildcards are characters with special meaning in the WHERE clause of SQL. The LIKE operator must be used when using wildcards in the clause

1) Percent sign (%) wildcard

%: Indicates that any character appears any number of times

select prod_id, prod_name
from Products
where prod_name like 'Fish%'

When this clause is executed, any word beginning with fish will be searched.

[Note] Wildcards can be used anywhere in the search mode.

select prod_id, prod_name
from Products
where prod_name like '%bean bag%';

[Note]: %: represents 0, 1 or more characters at a given position in the search mode

[Note]:% will not match the line whose name is null

2) Underscore (_) wildcard

    The underscore has the same purpose as %, but it only matches a single character, not multiple characters.

select prod_id , prod_name
from Products
where prod_name like '_inch teddy bear';

[Note]: One thing that is different from our thinking is: the words after _ and% must be complete with the database, eg:'_inch te' cannot match anything.

3) Square brackets [] wildcards are used to specify a character set.

SELECT cust_contact FROM Customers WHERE cust_contact REGEXP '[JM]' ORDER BY cust_contact;

Match any column with a string of JM.

3.3 Tips for using wildcards

    Don’t overuse wildcards, if other operations can achieve the same purpose, you should use other operators

    When you really need to use wildcards, try not to put them at the beginning of the search mode, because wildcards are placed at the beginning, and the search is the slowest.

    Pay careful attention to the position of wildcards

4. Calculated fields

4.1 Why use calculated fields

The calculated field is created in the SELECT statement at runtime.

Fields and columns have similar meanings and are often converted to each other. Fields are usually used in links to calculated fields.

The conversion and formatting are completed in SQL, and the processing is several times faster than in the client application .

4.2 splicing fields

Use the CONCAT() function in mysql to connect the item tables (note that other databases may use "+" to connect, but mysql cannot) , and || is equal to the operator OR and && is equal to the AND operator

SELECT CONCAT(vend_name, ' (', vend_country, ')')
FROM Vendors
ORDER BY vend_name;

4.3 Aliases

The spliced ​​address field does not have a name and cannot be applied to the client, so it needs a field 别名(alias), another way is called export column(derived column)

Aliases can be usedAS关键字赋予

SELECT CONCAT(vend_name, ' (', vend_country, ')') as vend_title
FROM Vendors
ORDER BY vend_name

5. Use functions to process data

5.1 The role of functions

Functions mainly provide convenience for data processing and conversion.

Most SQL-implemented functions

  • Used to process text strings (delete, recharge, case conversion)
  • Used to perform arithmetic (return absolute value, algebraic operations) operations on numerical data.
  • Used to process date and time values ​​and extract specific components from these values.
  • Return the special information that the DBMS is using (user login information)

5.2 Commonly used functions

Take the upper() function as an example:

SELECT vend_name, UPPER(vend_name) AS
vend_name_upcase
FROM Vendors
ORDER BY vend_name;

6. Aggregate data

6.1 Aggregate functions

  • Determine the number of rows in the table
  • Get the sum of the row groups in the table
  • Find the maximum, minimum, and average values ​​of the table columns (all rows, specific rows).

Aggregate functions need to summarize the data in the table, not the actual data itself, so there is no need to return time data, wasting resources

6.2 Commonly used aggregate functions

AVG(): Returns the average value of all columns or a certain column

MAX() returns the largest value in the specified column

MIN() returns the minimum value in the specified column

The COUNT() function counts the number of rows in the table or the number of culverts that meet specific conditions.

               Ignore the NULL and non-null values ​​contained in the table columns, and calculate the number in the table.

               Use COUNT(column) to calculate rows with values ​​in a specific column, ignoring NULL values

SUM() returns the sum of the specified column values

For example:

Returns the total number of customers in the Customers table, regardless of the value of each column in the row.

SELECT COUNT(*) AS num_cust
FROM Customers;

Calculate the average price of all products in the Products table

SELECT AVG(prod_price) AS avg_price
FROM Products;

Returns the count of customers with email addresses, the result is 3, which means that only 3 customers have email addresses

SELECT COUNT(cust_email) AS num_cust
FROM Customers;

 Sum all the order item_price price multiplied by the sum of quantity, the WHERE clause of an order item

SELECT SUM(item_price*quantity) AS total_price
FROM OrderItems
WHERE order_num = 20005;

Use select to combine aggregate functions

SELECT COUNT(*) AS num_items,
MIN(prod_price) AS price_min,
MAX(prod_price) AS price_max,
AVG(prod_price) AS price_avg
FROM Products;

7. Group data

7.1 Create Group

Use GROUP BY and HAVING clauses to group data to summarize a subset of table content

SELECT vend_id, COUNT(*) AS num_prods
FROM Products
GROUP BY vend_id;

上述代码:SELECTThe statement specifies two columns, vend_id including the supplier ID, which is the num_prods result of the calculation field, and the  GROUP BY clause indicates vend_id to sort and group the data

7.2 GROUP BY clause rules:

  • Contains any number of columns,
  • If a group is inserted in the GROUP BY clause, the data will be aggregated on the last specified group.
  • All columns listed in the GROUP BY clause must be retrieved columns, valid expressions, and cannot aggregate functions.
  • Most SQL does not allow GROUP BY with variable-length data types (text, memo fields)
  • In addition to the aggregate calculation statement, in the SELECT statement, each column must be given in the GROUP BY clause .
  • If the group has a NULL value, it will be returned as a group, if there are more than one, it will be a group.
  • The GROUP BY clause must appear after the WHERE clause

7.3 Filter group

The filter group specifies which groups to include and which groups to exclude, using HAVINGclauses, similar to the WHERE clause,

The only difference is that WHERE is used to filter rows, and HAVING filters groups.

It can also be said that HAVING filters after data grouping, and WHERE filters before data grouping.

SELECT cust_id, COUNT(*) AS orders
FROM  Orders
GROUP BY cust_id
HAVING COUNT(*) >= 2;

Filter out groups of more than two orders

SELECT vend_id, COUNT(*) AS num_prods
FROM Products
WHERE prod_price >= 4
GROUP BY vend_id
HAVING COUNT(*) >= 2;

The above code function: the first line uses an aggregate function, the WHERE clause filters out all prod_pricelines less than 4,

Group by vend_id, HAVING clause filter count 2 or more groups

7.4 The difference between group by and order by

  • GROUP BY

    • Sorted output
    • Any column can be used
    • You can choose whether to use with aggregate functions
  • ORDER BY

    • Grouping rows, the output may not be grouped sequentially
    • Only select columns or expressions may be used, and each column expression must be used
    • If used with aggregate functions, you must use

7.5 select clause order

8. Subqueries

1) Subqueries are generally used with the in operator

SELECT cust_id
FROM Orders
WHERE order_num IN (SELECT order_num
FROM OrderItems
WHERE prod_id = 'RGAN01');

The above code: the subquery is processed from the inside out

Execute first SELECT order_num FROM OrderItems WHERE prod_id = 'RGAN01'

Pass the returned order number, 20007 and 20008 to the external query in comma format with the IN operator,

ReuseSELECT cust_id FROM orders WHERE order_num IN (20007,20008)

2)作为计算字段使用子查询

SELECT cust_name,
cust_state,
(SELECT COUNT(*)
FROM Orders
WHERE cust_id = cust_id) AS orders
 FROM Customers
ORDER BY cust_name;

9. Join table

9.1 Why use joins?

Because of the relational database design:

  • Avoid multiple occurrences of the same data
  • Information is broken down into a kind of data, a table
  • The tables are related to each other through some common values

and so:

Decomposing multiple tables is convenient for storage, convenient for processing, and highly scalable.

Use links to associate multiple tables in a SELECT to return a set of outputs.

Note: When designing a relational database, avoid inserting an illegal ID in another relational table, you can set the value in the relational table, only legal values ​​appear

9.2 Create Connection-Inner Connection

Inner join is also called equivalent join, which is based on the equality test between two tables

Internal join: Join the records in the record set whose fields in the two tables match the join relationship.

There are two syntaxes: the second is recommended

1) Equality test performed by the where statement

2) When using INNER JOIN connection, the connection condition uses a specific ON clause

[Note]: The result returned by the table relationship without the join condition is the Cartesian product

SELECT vend_name, prod_name, prod_price
FROM Vendors, Products
WHERE Vendors.vend_id = Products.vend_id;

The above code:

SELECT vend_name, prod_name, prod_price Specify the retrieved columns prod_name, prod_price in the same table. vend_name On another table

From specifies to join two tablesVendors, Products

WHERE clause qualified  Vendors.vend_id = Products.vend_id fully qualified name

It is recommended to use the following inner join .... on this grammatical structure.


--用inner join明确联结类型
SELECT vend_name, prod_name, prod_price 
FROM vendors 
INNER JOIN products ON vendors.vend_id = products.vend_id 
ORDER BY vend_name, prod_name;

9.3 Create a connection-self-connection

SELECT c1.cust_id, c1.cust_name, c1.cust_contact
FROM Customers AS c1, Customers AS c2
WHERE c1.cust_name = c2.cust_name
AND c2.cust_contact = 'Jim Jones';

Connect yourself and yourself by using table aliases.

9.4 Create connection-external connection

The join contains rows that are not related in the related table

SELECT Customers.cust_id, Orders.order_num
FROM Customers 
LEFT OUTER JOIN Orders
ON Customers.cust_id = Orders.cust_id;

OUTER JOIN specifies the type of join, and the difference between the two tables associated with the internal join is,

The outer join also contains rows that are not related. Use the RIGHT and LEFT keywords to specify whether the table containing all its rows is left or right.

9.5 Use connection conditions

  • Main connection type, generally use internal connection
  • Different DBMS connection methods are different.
  • Ensure that the correct connection conditions are used
  • To use multiple connections, first test each connection separately.

10. Combination query

Union executes multiple queries and returns the results as a single query result.

Generally need to use combined query

  • Similar return result data from different tables in a single query
  • Execute multiple queries in a single table, and return data according to a single query
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state IN ('IL','IN','MI')
UNION
SELECT cust_name, cust_contact, cust_email
WHERE cust_name = 'Fun4All';

Use UNION rules

  • There must be a combination of two or more SELECT statements, and the statements are directly separated by the keyword UNION.
  • Each query in UNION must contain the same columns, forms, and aggregate functions.
  • The data in the columns must be compatible,

With duplicate rows

UNION removes duplicate rows by default

If you want all rows, you can use UNION ALL instead of UNION.

[Note]: When using UNION to combine queries, only one ORDER BY clause can be used, and it must appear after the last SELECT statement.

 

 

Guess you like

Origin blog.csdn.net/yezonghui/article/details/112344229