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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:26:53+00:00 2026-05-30T04:26:53+00:00

I am struggling with a SQL query that I initially thought should be quite

  • 0

I am struggling with a SQL query that I initially thought should be quite simple.

Imagine a table Users that uses a UserID as the PK and a column Age for the users age:

    UserID     Age
    1          22
    2          34
    3          23
    4          19
    etc.

I’d like to be able to specify a UserID and return that user’s age as well as the average age of all other users. For example, if I specify UserID 1 then I’d like to see return set as:

   UserID      Age      AvgAge
   1           22       24.5

The following does not work: (as the WHERE is performed before GROUP BY)

  Select UserID, Age, Avg(Age) as 'AvgAge'
  From Users
  Where UserID = 1
  Group By UserId, Age

  UserID      Age      AvgAge    //Result set
  1           22       22

Can anybody nudge me in the right direction?

By the way, in an ideal world the average age should not include the user that has been specified as the idea is to show their age relative to the average age of everybody else.

Given that there are 1000+ users then taking an average over all users will make no practical difference to the AvgAge number, though if anybody would like to show off their SQL prowess with a solution to that then I’d be interested to see it.

Thanks

  • 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-30T04:26:54+00:00Added an answer on May 30, 2026 at 4:26 am
    declare @T table
    (
      UserID int,
      Age int
    )
    
    insert into @T values
        (1,          22),
        (2,          34),
        (3,          23),
        (4,          19)
    
    declare @UserID int = 1
    
    select Age, (select avg(Age*1.0)
                 from @T
                 where UserID <> @UserID) as AvgAge
    from @T
    where UserID = @UserID
    

    Result:

    Age         AvgAge
    ----------- ---------------------------------------
    22          25.333333
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am struggling to write a query based on the table below that should
I've been struggling with this one SQL query requirement today that I was wondering
I've been struggling to create an Oracle SQL query that will tell me if
I am struggling to find a working query in SQLite, that will return my
I have a model which contains a link to sql query that fetches information
I'm really struggling with this query. I have 4 tables (http://oberto.co.nz/db-sql.png): Invoice_Payement, Invoice, Client
I'm struggling with creating a SQL query involving aggregates using PostgreSQL. Consider the following
I am struggling to create a query using the Criteria API. The following SQL
I've been struggling to get a Java program to connect to MS SQL Server,
Been struggling with this simple selector problem a couple of hours now and must

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.