Test order BY is executed before or after group by

Source: Internet
Author: User
Ask:
2ID2YR,S{2HD (I31957select ... from ... group by ...) by ...
Z7QL ({pdq:z@31957 In this statement whether the order by is executed before group by or after.)
|5s$c6uo31957 Phpchina Open Source Community Portal vghlm&@
For example, now you have this table test
6n (|/C9CLU ' _r31957id name age
D FRX. r-e319571 AAA Phpchina Open Source Community portal Y2y3? I@Z)}
2 BBB 25
3 ' 1|4KQ (Rz oo9auu319573 CCC 20
! TCMF "Ve&v" a&s_9v319574 ddd Phpchina Open Source Community Portal #s9h7c%l#ac! O
Now Phpchina Open Source Community Portal E,dh_by_k
SELECT * FROM test group by age Phpchina Open Source Community Portal +i+^oa1d ' _
The result is Phpchina Open source community Portal M3CHB4EXT2V
ID Name Age Phpchina open Source Community Portal B.o/ly (u1t+}~5v
1 AAA 20
5[7qb#zor! T319572 BBB 25
+f5q,mu#^ N31957 Phpchina Open Source Community portal (w3t "U6y mw3t
So Phpchina Open Source community portal J,hm ' A Q
Desc Phpchina Open Source Community Portal 6mk1mwc[, select * FROM Test group BY age order by ID
Whether it will be Phpchina open source community Portal mu,ycq8^ws$|
ID Name Age Phpchina open Source community portal V#wa ' Z
4 DDD Phpchina Open Source Community Portal%[uyu G+EWF
3 CCC Phpchina Open Source Community Portal 2n3{.{ Fjzh$h
Or the Phpchina open source community Portal bm~q/{~s| X
ID Name Age Phpchina open Source community Portal Cpt4r4n$n0z4x|w
2 BBB Phpchina Open Source Community portal) om}n*@ w.ha2v
1 AAA Phpchina Open Source Community portal, P1V:A$KQ Tq
Phpchina Open Source Community Portal (udo%{! RMV
Phpchina Open Source Community portal F#_!o {[H
Phpchina Open Source Community Portal P U,r ({8R ' Q ' Jy
Answer: Phpchina Open Source Community portal 9/*b,pdr] ' X,I6VW
ID Name Age
0d "rmq{$GK 31957 2 BBB Phpchina Open Source Community Portal d|,h| ( Q '])}[
1 AAA 20
*u0t}! a2^%i}d31957 Phpchina Open Source Community portal M:OS.O P ('
That is, the order by is executed after group by
P.a (A$QB$S-V1}HQ ' ~31957
n:l2b!h[t31957 There may be such a demand here that the table that is grouped by age is then taken out of the person with the largest ID in each group.
; | #w6A |m@k31957
4u8to8ft31957 so how to deal with this time.
j,xf2[[d#k6a x31957 Answer:
P&qw e4 ' 1R r2a31957select max (ID), age from test group by age Phpchina Open Source community Portal]x}^9y+p
ID Age
{2M) GO#IVBM _0hm319573 Phpchina Open Source Community portal y8z.at M3xh ' R
4 25
Z;o) wcd5o#n/u ' w}31957 Phpchina Open source Community Portal ggcd3z ' {

M}pl4j2^j31957, don't take it for granted that you can use the Phpchina open source community portal. G1p9ro0x$on
Select Max (ID), name, age from test group by age Phpchina Open Source community Portal!nr! id3wp,l7m
Because of Max (ID), age and name will not correspond to Phpchina open source community portal, Q1B#DV0H]Y|,ZF
For example, this statement will show the following results
VY) s%n.s1n4r31957 ID name age
-x8p1x*mj%_319573 AAA Phpchina Open Source Community portal Ttsf-u)]}+n
4 BBB Phpchina Open source Community portal%r#m) Rww:xo r:^r
We notice that id->3 and name->aaa are not the same data at all.
"ej8x0y/s-gt6/m31957 Phpchina Open Source Community portal x*@ (' Y[d K ' p9a

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.