6-SQL subqueries

(1) What is the correlated subquery, any associated non subqueries (nested query)
Subqueries query data from a table in the data results, if the results of this data only once, and then this data as a result of the condition of the main query execution, then this sub-query is called a non-correlated subquery.

If you need to perform multiple sub-queries, which uses a circular manner, starting with the outer query began, every incoming subquery to query, and then the results back to the outside, this implementation is called nested sub-association Inquire.

Which player is the tallest, the highest height is the number of ways you can use sub-query:
- Non-related sub-query examples
select player_name, height FROM player where height = (select max(height) from player);
Firstly select max (height) from player to give the maximum height value, then the value in the player-match lookup table, to see who meet this value is outputted.

Query each team is greater than the average height of the players what, and show their player name, height, and where the team ID.
- correlated subquery example
select player_name, height, team_id from player as a where height > (select avg(height) from player as b where a.team_id = b.team_id);
If you perform a subquery depends on the outer query, usually because of the sub-query table uses external tables and related conditions, so each outer query execution time, the sub-query must be re-evaluated once, such subqueries it is called correlated subqueries

(2) EXISTS subqueries
Want to see which players have ever played, and displays their name, player ID and the team ID. In this statistics, whether the appearance is played by the performance of the players in this table player_score to statistics, if a player has played in the record in player_score appearances on his behalf, here to use the EXISTS subquery,
select player_name, player_id, team_id from player where exists (select player_id from player_score where player.player_id = player_score.player_id);
Queries players do not exist in the data table player_score
select player_name, player_id, team_id from player where not exists (select player_id from player_score where player.play_id = player_score.player_id);

(3) comparing the set of subqueries - compared to another query result set
The main query operators and definitions:
IN determines whether the set
ANY designed for use with the operator, and any value returned by the subquery comparison
ALL designed for use with the operator, and the sub-query returns all the values ​​compared
SOME ANY actually an alias, the same effect, generally try to use ANY

Want to see which players have ever played, and displays their name, player ID and the team ID.
select player_name, player_id, team_id from player where player_id in (select player_id from player_score where player.player_id = player_score.player_id);

Not difficult to see the results of the query EXISTS IN operator and the operator is consistent, then how to choose the IN and EXISTS?

SELECT * FROM A WHERE cc IN (SELECT cc FROM B)
SELECT * FROM A WHERE EXIST (SELECT cc FROM B WHERE B.cc=A.cc)
        In fact, the query process, in the case of our index of cc column, we also need to determine the size of the Tables A and B, respectively. Here among the examples in Table A refers to the player table, Table B refers player_score table. If Table A is larger than the table B, then the efficiency of sub-EXIST IN subquery high query efficiency, because when the B table if cc column is indexed, then the efficiency of IN subquery will be higher than that. Similarly, if Table A is smaller than the table B, then use the EXISTS subquery will be more efficient, because we can use to index the cc A table columns, rather than query cc column from B, respectively.

ANY and ALL need to use comparison operators, comparison operators include (>) (=) (<) (> =) (<=) and (<>) and the like.
Players query table than the Indiana Pacers (corresponding team_id 1002) in a player information any high height of the players, and the output of their players ID, the player name and player height
select player_name, player_id, team_id, height from player where height > any (select height from player where team_id = 1002);

Queries than Indiana Pacers (corresponding team_id 1002) information for all the players are high height of the players, and the output of the player ID, player name and player height
select team_id, player_id, player_name, height from player where height > all (select height from player where team_id = 1002);

Note: ANY, ALL the keywords must be used with a comparison operator. Because if you do not use comparison operators, would not achieve the role of a set of comparisons, then use ANY and ALL make no sense

(4) as a calculated field subqueries

In fact subquery can also be used as a calculated field in the main query.
Query each team's number of players, that is, the corresponding team this table, I need to query the same team_id player in this table for all the number of players is.
select team_name, (select count(*) from player where player.team_id = team.team_id) as player_num from team;

Only in the player table and Detroit Pistons Indiana data player, they player_num is not 0, and the Atlanta Hawks player_num equal to 0. When queried, I will subquery SELECT count (*) FROM player WHERE player.team_id = team.team_id as a calculated field, usually we need to give this a calculated field an alias, here I use player_num, because the subquery the long statement, using the alias easier to understand.

Queries been averaging greater than 20 players. Player_score scoring average taken from the table, and you need to output the player's ID, ID information of where the team name and player
- Using IN
select team_id, player_id, player_name from player where player_id in (select player_id from player_score GROUP BY player_id HAVING(avg(score) > 20));
- Use EXISTS
select team_id, player_id, player_name from player where exists (select player_id from player_score GROUP BY player_id HAVING(avg(score) > 20) and player.player_id = player_score.player_id);

select team_id, player_id, player_name from player where exists (select player_id from player_score where player.player_id = player_score.player_id GROUP BY player_id HAVING avg(score) > 20);
- Use JOIN
SELECT
t2.player_id,
t2.player_name,
t2.team_id,
t3.v
FROM
player AS t2
JOIN (
SELECT
t1.player_id,
avg (t1.total) AS v
FROM
(
SELECT
player_id,
sum( score ) AS total
FROM
player_score
WHERE
game_id IN ( SELECT game_id FROM player_score GROUP BY game_id )
GROUP BY
player_id # grouping of players, calculate the total score in every game of
) AS t1
GROUP BY
t1.player_id # grouping of players, calculate the average of race
HAVING
v> 20 # scoring greater than 20
) AS t3 ON t2.player_id = t3.player_id;

1, I understand scoring greater than 20, scored 40 points the first field, second field scored 2 points, 21 points are the field satisfies the condition
2, a game, a player can appear multiple times
Analytical thinking, to draw players total score in every game, and then to divide the players calculate the average number of players participating in the game



Guess you like

Origin www.cnblogs.com/Tom-tao/p/11871142.html