SQL study notes 03 Geek time SQL must know 50 lectures

07 丨 What is SQL function? Why might using SQL functions cause problems?

08 丨 What are SQL aggregate functions and how to use them to summarize the data in the table?

09 丨 Subqueries: What are the types of subqueries? How to improve the performance of subqueries?

I explained the use of subqueries today, which talked about the comparison of the efficiency of EXISTS and IN subqueries. When the query field is indexed, the main table A is larger than the slave table B. Using the IN subquery is more efficient. On the contrary, the main table A is less From table B, using the EXISTS subquery is more efficient. Similarly, if you use the NOT IN subquery and NOT EXISTS subquery, under what circumstances, which is more efficient?

Today's homework is too low to understand

1. SELECT
player_id,
team_id,
player_name
FROM
player a
WHERE
EXISTS ( SELECT b.player_id FROM player_score b GROUP BY b.player_id HAVING AVG( b.score ) > 20 and a.player_id = b.player_id);
2.SELECT
player_id,
team_id,
player_name
FROM
player a
WHERE
EXISTS ( SELECT b.player_id FROM player_score b WHERE a.player_id = b.player_id GROUP BY b.player_id HAVING AVG( b.score ) > 20);
3.SELECT
player_id,
team_id,
player_name
FROM
player
WHERE
player_id IN ( SELECT player_id FROM player_score GROUP BY player_id HAVING AVG( score ) > 20 );
Recommendation 3, because the subquery will only be executed once. 2 is better than 1, because where will first filter the data rows, then group, and then filter the grouping.

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

Guess you like

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