SQL study notes 02 Geek time SQL must know 50 lectures

04 丨 What should I pay attention to when using DDL to create a database & data table?

 

05 丨 Retrieve data: Are you still SELECT *?

 

First hand in homework select name, mp_max from heros order by hp_max desc limit 5;
 Then is a question asked by a classmate downstairs, I am also puzzled, it is this
SELECT DISTINCT player_id, player_name, count (*) as num # order 5
FROM player JOIN team ON player.team_id = team.team_id # sequence 1
WHERE height> 1.80 # sequence 2
GROUP BY player.team_id # sequence 3
HAVING num> 2 # sequence 4
ORDER BY num DESC # sequence 6
LIMIT 2 # sequence 7

for this statement , I still have a little doubt: Since the execution of HAVING is before SELECT, it is reasonable to say that the count (*) in SELECT should not be calculated when executing HAVING, why is it used directly in HAVING What about num> 2?
I hope the teacher can take the time to explain during his busy schedule, thank you teacher

The author replied: A good question, actually between Step4 and Step5, there is also an aggregate function calculation.
If this calculation process is added, the complete sequence is:
1. FROM clause assembly data
2. WHERE clause for conditional screening
3. GROUP BY grouping
4. Use aggregate function for calculation;
5. HAVING screening grouping;
6. Calculate all Expressions;
7, SELECT fields;
8, ORDER BY sorting
9, LIMIT filtering,
so there are two processes in the middle that need to be calculated: aggregate functions and expressions. The rest is the execution order of keywords, as shown in the article.

06 丨 Data filtering: What are the methods of SQL data filtering?

      A very important way to improve query efficiency is to restrict the number of returned results. There is also a very effective way to specify filtering conditions and perform filtering. Filtering can filter the results that meet the conditions and return them, reducing unnecessary data rows.

Thanks  https://time.geekbang.org/column/article/102541

78 original articles published · 32 praised · 120,000 views

Guess you like

Origin blog.csdn.net/caofengtao1314/article/details/105384323