sql - How does GROUP BY work on Ambiguous columns? -


while practicing sql, noticed strange group bys. working on practice problem need obtain users friends in social network. straightforward.

we have nested sub-query computes number of friends each user. (this done group by). match our original users table. following query that.

select name, grade (select id1 id, count(*) c        friend        group id1) join highschooler using (id) order c desc limit 10; 

what don't understand is: why following query return same thing?

select highschooler.name, count(*) friendcount friend join highschooler on (friend.id1 = highschooler.id) group highschooler.id order friendcount desc limit 10; 

there multiple values of highschooler.name, sql pick 1 group? secondly, of above 2 queries preferred?

both queries return same results in first query grouping first , joining on id. in second 1 joining first , grouping. make no difference.

however, sub-queries logically correct way solve problems of form, "get facts a, conditional on facts b". query engines prefer join, prefer subquery. per history join better subquery.

in joins rdbms can create execution plan better query , can predict data should loaded processed , save time, unlike sub-query run queries , load data processing.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -