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 8405763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:54:56+00:00 2026-06-09T22:54:56+00:00

Having a little bit of trouble understanding how a query alias works in postgresql.

  • 0

Having a little bit of trouble understanding how a query alias works in postgresql.
I have the following:

SELECT DISTINCT robber.robberid,
                nickname,
                Count(accomplices.robberid) AS count1
FROM   robber
       INNER JOIN accomplices
               ON accomplices.robberid = robber.robberid
GROUP  BY robber.robberid,
          robber.nickname
ORDER  BY Count(accomplices.robberid) DESC;


 robberid |            nickname            | count1 
----------+--------------------------------+--------
       14 | Boo Boo Hoff                   |      7
       15 | King Solomon                   |      7
       16 | Bugsy Siegel                   |      7
       23 | Sonny Genovese                 |      6
        1 | Al Capone                      |      5
 ...

I can rename the “count1” column using the as command but I can’t seem to be able to refer to this again in the query? I am trying to include a HAVING command at the end of this query to query only objects who have a count less than half of the max.

This is homework but I am not asking for the answer only a pointer to how I can include the count1 column in another clause.

Can anyone help?

  • 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-06-09T22:54:57+00:00Added an answer on June 9, 2026 at 10:54 pm

    In general, you can’t refer to an aggregate column’s alias later in the query, and you have to repeat the aggregate

    If you really want to use its name, you could wrap your query as a subquery

    SELECT * 
    FROM
    (
        SELECT DISTINCT robber.robberid, nickname, count(accomplices.robberid)  
        AS count1 FROM robber                   
        INNER JOIN accomplices  
        ON accomplices.robberid = robber.robberid  
        GROUP BY robber.robberid, robber.nickname  
    ) v
    ORDER BY count1 desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a little bit trouble with the following. I have multiple form
I'm having a little bit of trouble implementing the following method while handling the
I'm having a little bit of trouble understanding what the problem is here. I
I am having a little bit of a trouble with the following: <script> $(document).ready(function()
I'm having a little bit of trouble understanding how to handle deadlocks. First of
I am having a little bit of trouble... I am trying to get a
I am having a little trouble understanding ServiceKnownType in WCF. Taken from this blog
I´m having a little bit of trouble sorting a way to manage automatic resolved
I'm having a little bit of trouble making OpenID work from within an iframe.
I'm having a little bit of trouble with my homework. I was supposed to

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.