Three common SQL Advanced Usage (Advanced chapter)

On the basis of the statement of core learning on, let's look at how to use SQL statements to handle more complex analysis problems. In practice, the master SQL statements, and three common core Advanced use may need to use SQL to cover 80% of the work. As data analysts, we need the ability to construct a multilayer complex logic statement, the skilled in using SQL statements based on the optimization consciously, thereby simplifying the working process more efficient output data analysis.

Here Insert Picture Description

This verse Summary:

1. subquery
2. conditional statement
3.SQL statement optimization

learning target:

1. Build multi-layered, complex logic statements, to streamline workflow throughput data analysis
2. Obtain SQL statement optimization awareness, become more efficient and professional analysts

(A) SQL Advanced Usage - Subqueries

Subqueries role: to build complex logic, simplifying the process needs to answer

What is a subquery?

  1. We will build a query for a query statement further filter criteria or data source
  2. Used to build complex SQL statements using SQL to simplify the process of problem-solving

usage:

1. The clause is a complete SQL statement
2. Use parentheses () surrounding the clause
3. Use multiple sub-statement solutions to complex problems

DESCRIBE `产品信息`
__

产品号码	int(11)
产品名称	varchar(256)
供应商号码	int(11)
产品单价	decimal(12,2)
产品描述	varchar(256)

# 示例:单行单列过滤条件子查询
SELECT
	*
FROM `产品信息`

WHERE `产品单价` >= 0.5 * (SELECT MAX('产品单价') as `最高单价` FROM `产品信息`) 

Subqueries Category:

  • Single separate filter condition subqueries: single separate sub-query would return the value as a result

  • Rows and columns filter conditions sub-query: Sub-query returns a single column value as a result of multiple rows

  • Pro table subquery: Sub-query returns a temporary list (rows and columns) as a result

(1) separate sub-query filter

What is the single separate sub-query filter condition?

  • Clause returns only a single value (the one row of data) as a result of operation
  • SQL clause demerit for subsequent filters

Why single separate sub-query filter conditions?

  • Using the data table to construct the filter conditions
  • SQL reduce operational steps to simplify the process of answering questions

——————

Case background of the demand:

Case : List all products priced higher than or equal to the product price details information highest 50% of product
demand dismantling:

  • The final result is returned: Product Details
  • Filtering data columns: Unit Price
  • Filter: equal to or higher than the highest unit price 50%

Determine data location: Product Information Sheet
Here Insert Picture Description


# 普通SQL语句解答过程和思路

## 第一步:查询产品最高售价,手动记录最高推荐售价
SELECT
	MAX(`产品单价`) AS `最高单价`
FROM `产品信息`

## 第二步:使用记住的最高推荐售价构建SQL语句解答问题
SELECT *
FROM `产品信息` WHERE `产品单价` >= 0.5 *97

# 单行单列过滤条件子查询解答思路&过程

## 使用查询产品最高价作为子句来直接构建主句中的WHERE过滤条件
## 单个SQL语句解决问题

SELECT
	* 
FROM
	`产品信息` 
WHERE
	`产品单价` >= 0.5 * ( SELECT MAX( `产品单价` ) AS '最高单价' FROM `产品信息` )

# 主查询语句/子查询语句

(2) rows and columns filter conditions Subqueries
Here Insert Picture Description

Case: lists all the Guangdong suppliers to supply product information details

Demand dismantling:

  • The final result is returned: Product Details Information
  • Filter data columns: Vendor Information
  • Filter: Guangdong Suppliers

Locate data:

  • Product Information Sheet
    • Product Information Sheet only vendor number, no province information
  • Supplier data information table
    • Supplier information table contains information supplier number and provinces

Solutions : Use a separate multi-line sub-query builder statement to filter the data in the product information sheet

  • Information using the vendor information table in the provinces of Guangdong Province to obtain vendor supplier number
  • The resulting number is the supplier of multi-line single data
SELECT
`供应商号码`
FROM `供应商信息` WHERE `省份`= '广东省'

——————————
供应商号码
1
3
10
11
...
________
# 使用多行单列子查询构建语句来过滤产品信息表中的数据

SELECT
	* 
FROM
	`产品信息` 
WHERE
	`供应商号码` IN ( SELECT `供应商号码` FROM `供应商信息` WHERE `省份` = '广东省' )

(3) Pro table subqueries

Introduction:

  • What is a temporary table subqueries

    • Result clause constitutes a temporary list
    • Provisional list of SQL statements for subsequent temporary table subqueries
  • Why should the temporary table subqueries

    • Complete the multi-step calculation logic in a statement
    • Constraints final return results

Case needs Background: list up to the number of products supplier number

Demand dismantling:

  • The final result is returned: Supplier Number
  • Filtering data: Number of Products
  • Filter: Up

Verify the data locations:

  • Product Data Sheet

Solutions:

  • Calculating the number of product suppliers polymerization (GROUP BY) and aggregate functions (COUNT)
  • Can get the largest number of products supplier number and the number of products through the sorting (ORDER BY) and select (LIMIT)
  • Requirements SQL statement returns only vendor number

Code:

# 业务目标:列出产品数目最多的供应商号码

## 使用临时子查询语句选择供应商号码
### 使用聚合语句作为子语句与AS关键字创建临时列表 'a'
### 使用SELECT语句选择供应商号码

SELECT
`供应商号码`
FROM
(
SELECT 
`供应商号码`,
COUNT(`产品号码`) AS `产品数目`

FROM `产品信息`
GROUP BY `供应商号码`
ORDER BY `产品数目` DESC
LIMIT 1
) AS a  # Every derived table must have its own alias

Here Insert Picture Description

Subqueries Summary:

  • Subqueries role : to build complex logic, simplifying the process needs to answer
  • Subqueries single separate filter conditions: sub-query would return single separate value as a result of
  • Multi-line single sub-query filter criteria: return multiple rows of a single column value as a result
  • Pro table subquery: sub-query returns the temporary list ( rows and columns ) as a result

(Two), SQL Advanced Usage - conditional statement

Introduction:
What is conditional?

  • If ... then ... (example: customer total consumption amount is greater than 5000, the client is VIP customers)
  • In the same judgment condition statement, there may be a plurality of determination conditions to

Conditional statement

The CASE
the WHEN determination result of the determination condition THEN
[THEN the WHEN determination condition determination result]
the ELSE default value
END

** Example: ** calculated level of consumption based on the total amount of consumption of all customers

Step 1: Determine the required data tables (you can find the needed data table based on the data dictionary and ER diagram) - "order information table"

Step two: define different levels of consumption

According to the interval where the customers total spending, calculate the customer's consumption level:

Consumer range Consumer level
Greater than equal to 5000 VIP3
2000-4999 VIP2
1000-1999 VIP1
Less than 1000 ordinary

The third step: Write a conditional statement to obtain results

# 计算顾客总消费金额

SELECT
	`顾客号码`,
	SUM( `消费金额` ) AS `总消费金额` 
FROM
	`订单信息` 
GROUP BY
	`顾客号码`

-------

顾客号码 总消费金额
1	4596.2
2	1402.95
3	7515.35
4	13806.5
5	26968.15
6	3239.8

————————————————

# 根据总消费

# 计算总消费金额
SELECT
	`顾客号码`,
CASE	
	WHEN `总消费金额` >= 5000 THEN 'VIP3' 
	WHEN `总消费金额` >= 2000  THEN 'VIP2' 
	WHEN `总消费金额` >= 1000 THEN 'VIP1'
	ELSE '普通' 
END AS `消费级别` 
FROM
	( SELECT `顾客号码`, SUM( `消费金额` ) AS `总消费金额` FROM `订单信息` GROUP BY `顾客号码` ) AS a #Every derived table must have its own alias

------
顾客号码 消费级别

1	VIP2
2	VIP1
3	VIP3
4	VIP3
5	VIP3

NOTE: conditional statement used to convert the data, create new data based on the existing data column

(Three), SQL Advanced Usage - SQL statement optimization

  • Avoid using in the query returns all data *
  • Statements in the filter, if possible, instead of using IN BETWEEN
  • In the polymerization statement WHERE clause using the priority data of the original data in the table the polymerization was filtered off and then
  • In sub-query statement, as far as possible in the final execution (ORDER BY)
Published 17 original articles · won praise 10 · views 1674

Guess you like

Origin blog.csdn.net/weixin_44976611/article/details/104838146