Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6885431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:38:03+00:00 2026-05-27T05:38:03+00:00

In a simple query the order of your GROUP BY fields makes no difference

  • 0

In a simple query the order of your GROUP BY fields makes no difference (ignoring developer legibility) to the final result.

EG: SELECT COUNT(*) FROM People GROUP BY Age, Gender will produce the same results as if the GROUP BY fields were flip-flopped.

Generally speaking, under what condition(s) does this apparent commutative property of the GROUP BY fields break down?

I’m looking for a general rule here (EG: “Any expression containing sub-expressions which depend upon one of the grouped fields”)

I’m having a hard time coming up with an example of where the ordering would matter – but my gut tells me it does in some situation(s).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:38:03+00:00Added an answer on May 27, 2026 at 5:38 am

    I think the only time it matters is when using ROLLUP to create subtotals

    http://msdn.microsoft.com/en-us/library/ms189305(v=sql.90).aspx

    CREATE TABLE #Prod
    (
        ID INT IDENTITY(1,1),
        Cat INT,
        Size Int
    )
    
    INSERT #Prod SELECT 1,1
    INSERT #Prod SELECT 1,1
    INSERT #Prod SELECT 1,2
    INSERT #Prod SELECT 1,3
    INSERT #Prod SELECT 1,3
    INSERT #Prod SELECT 1,3
    INSERT #Prod SELECT 2,1
    INSERT #Prod SELECT 2,2
    INSERT #Prod SELECT 2,2
    INSERT #Prod SELECT 2,3
    INSERT #Prod SELECT 2,3
    INSERT #Prod SELECT 2,3
    
    SELECT 
    COUNT(*)
    FROM #Prod
    GROUP BY Cat, Size WITH ROLLUP
    
    SELECT 
    COUNT(*)
    FROM #Prod
    GROUP BY Size , Cat WITH ROLLUP
    

    Results from Query 1

    2
    1
    3
    6
    1
    2
    3
    6
    12

    (9 row(s) affected)

    Results from Query 2

    2
    1
    3
    1
    2
    3
    3
    3
    6
    12

    (10 row(s) affected)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple query $this->db->select('id, title'); $this->db->order_by('id', 'DESC'); $query = $this->db->get('journal'); foreach($query->result()
I have a simple mysql select query in my PHP code: $result = mysql_query(SELECT
Say I have a simple query like this: SELECT * FROM topics ORDER BY
I have a simple query like this: select * from mytable where id >
Just a simple question, but does the order of your index matter when it
does the Hibernate Query Language preserve the row order in the query result when
The first question your probably asking is how many Group by / Order by
I'm doing a simple MySQL query to count the number of rows a query
Simple query, possibly impossible but I know there are some clever people out there
I am testing a simple query to get data from an AS400 database. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.