当前位置:网站首页>SQL learning notes 2

SQL learning notes 2

2022-07-01 06:35:00 Small dollar

Is a function that summarizes a set of data , The input is a set of data , The output is a single value , There are multiple arguments , There are multiple dependent variables

Common aggregate functions

sum()avg() Fields of type are not applicable to functions
max()min() Type applies to both string and time
count The number of statistics does not contain a null value

avg = sum / count
Aggregate function filtering null, If you query the average bonus rate of the company , use avg The function is wrong , Because the null value is not counted . Right should SELECT SUM(commission_pct) / COUNT(IFNULL(commission_pct,0)) FROM employees;

If you need to get accurate data in the table , those count More accurate ?
count(“*”),counut(1),count( Specific fields )
Different engines have different efficiency , however count(" *" )=count(1) This is the most efficient

group by Use

orderby Sort ,group by Grouping operation
A column to group , Group multiple columns
group by The statement in from Back
oderby front ,limit front

group by with rollup If it is to calculate the average salary of each department, add it to calculate the average salary of all the data at last
withrollup Unsuitable and orderby Sort , The results don't show , Because when sorting the Department salaries after grouping ,rollup Not at a level is not suitable for sorting

having Use

If an aggregate function is used in the filter condition , Then the condition must use having To replace where, Otherwise, the report will be wrong , If it does having Words ,having Request to put in group by Behind
Used in development having The premise is that group by
When there is an aggregate function in the filter condition, the filter condition must be declared in having in
If there is no aggregate function in the filter condition, the filter condition must be declared in where

where And having Comparison of
When querying multiple tables where More efficient
SQL The underlying query principle :
sql Statement execution :
First of all from 》 where》group by》having》select》distinct》orderby》limit

SQL The underlying principle of execution

Subquery :
Title : External query , Internal query , Main query ( External query ), Subquery ( Internal query )
Classification : Single line sub query , Multi line sub query
Whether the internal query is executed more than once : Correlation subquery , Uncorrelated subqueries

Symbols for multiline queries :

in Equal to any value in the list
any: Need to be used with single line , And any value
all: And all the values
some:any Another name for

原网站

版权声明
本文为[Small dollar]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207010620432093.html