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

  • SEARCH
  • Home
  • 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 6989017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:09:27+00:00 2026-05-27T19:09:27+00:00

Why can’t I group by cnt and have to group by count(*). This just

  • 0

Why can’t I group by “cnt” and have to group by count(*). This just seems redundant and I wish there was a better way.

SELECT count(*) as cnt
from table
group by count(*)
  • 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-27T19:09:28+00:00Added an answer on May 27, 2026 at 7:09 pm

    It has to do with the logical processing order of SQL queries. Specifically, the GROUP BY clause is evaluated before the SELECT clause, despite appearing at the bottom of the query. Hence, aliases used in the SELECT are not necessarily available to the query processor while it evaluates the logically-precedent parts of the query.

    Same reason you can’t use aliases in a WHERE clause: that clause is logically processed before the SELECT. ORDER BY, on the other hand, is the last part of the query to be processed – aliases (or even ordinal column positions) are typically supported there.

    Some implementations may choose to provide this support – see the MySQL link that @daghan posted, for example – but it cannot be relied on; SQL Server, as you can see, doesn’t support it.

    It’s worth pointing out that, were you desperate to refer to a complex expression by an alias, you could perhaps put that expression in a subquery, and refer to it from there. I’d call that overkill in most simple scenarios, though (such as the one in your question).

    Update: @MartinSmith raises a good point in his comment on the question: aliasing aside, grouping by an aggregate expression isn’t going to work. In fact, since the aggregate is determined by a grouping of non-aggregate columns, it doesn’t even really make sense. What are you actually trying to achieve with such a query? Perhaps we can help further.

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

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
Can this be done? It seems like this should be possible. In general, I
Can somebody help me with this. There is HTML code: <h3> <label> <input type=checkbox
Can someone help me with this scenario? *There is a button, which when tapped,
Can't work out a way to make an array of buttons in android. This
can I make my own headers in HTTP request ? e.g. This is normal
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
Can I run this in a Windows command prompt like I can run it
Can anybody help me? What should be the datatype for this type -07:00:00 of

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.