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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:40:11+00:00 2026-05-31T12:40:11+00:00

My table is– create table mobile ( id integer, m_name varchar(20), cost integer )

  • 0

My table is–

create table mobile
(
  id integer,
  m_name  varchar(20),
  cost integer
)

and the values are —

insert into mobile values(10,'NOkia',100);
insert into mobile values(11,'Samsung',150);
insert into mobile values(12,'Sony',120);

I know how to calculate average on column cost, my code is–

 select avg(cost) from mobile;

and the result is 123

But i want to calculate average and then also show the difference.I was able to this but, I am not able to add avg column in the select query–

My code is —

SELECT id, m_name as "Mobile Name", cost as Price,avg(cost) as Average,
cost-(select avg(cost) from mobile) as Difference FROM mobile
group by id,m_name,cost;

and the output is —

id      Mobile Name   Price  Average  Difference 
10      Nokia         100    100      -23
11      Samsung       150    150       27
12      Sony          120    120      -3

what I wants is to correct this average column.. I wants this—

id      Mobile Name  Price  Average  Difference 
10      Nokia       100     123     -23
11      Samsung     150     123      27
12      Sony        120     123      -3

please 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-05-31T12:40:13+00:00Added an answer on May 31, 2026 at 12:40 pm

    Your group by is what aggregates your average, and it is grouping by the whole table (I am assuming you did this to allow the select for everything) Just move your avg into another subquery, remove the overarching group by and that should solve it.

    SELECT id, m_name AS "Mobile Name", cost AS Price,
        (SELECT AVG(cost) FROM mobile) AS Average,
        cost-(SELECT AVG(cost) FROM mobile) AS Difference 
    FROM mobile;
    

    When you run the basic SELECT AVG(cost) statement it is naturally grouping by the column specified (cost in this case) as that is what you are requesting. I would suggest reading up more on GROUP BY and aggregates to get a better grasp on the concept. That should help you more than just a simple solution.

    UPDATE:

    The answer below is actually from David’s answer. It makes use the analytical functions. Basically, what is happening is that on each AVG call, you are telling the engine what to use for the function (in this case, nothing). A decent writeup on analytical functions can be found here and here and more with a google on the matter.

    SELECT id, m_name AS "Mobile Name" cost AS Price, AVG(cost) OVER( ) AS Average, 
        cost - AVG(cost) OVER ( ) AS Difference
        FROM mobile
    

    However, if your SQL engine allows for variables, you could just as easily do the below answer. I actually prefer this for future maintainability/readability. The reason is that a variable with a good name can be very descriptive to future readers of the code, versus an analytical function that does require a little bit more work to read (especially if you do not understand the over function).

    Also, this solution duplicates the same query twice, so it might be worth storing your average in a SQL variable. Then you ca change your statement to simply use that global average

    This is variables in SQL-Server (you will have to adapt it for your own instance of SQL)

    DECLARE @my_avg INT;
    SELECT @my_avg = AVG(cost) FROM Mobile;
    
        SELECT id, m_name AS "Mobile Name", cost AS Price,
            @my_avg AS Average, cost-@my_avg AS Difference 
        FROM mobile;
    

    This solution will read a lot cleaner to future readers of your SQL, too

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

Sidebar

Related Questions

Table in question (striped unneeded cols): car_id INT(11) PK url VARCHAR(100) signup TIMESTAMP Default
Table defintion is create table users ( serial_no integer PRIMARY KEY DEFAULT nextval('serial'), uid
Table: CREATE TABLE Instrument ( Id INT IDENTITY , Name VARCHAR(50) , Tenor VARCHAR(10)
Table users id - Primary Key, AI username - varchar (50) Table logins login_time
Table in PostgreSQL database: CREATE TABLE pmss_recordmodel ( id serial NOT NULL, Name character
table A amount user_id 100 abc 200 cdf 300 def table B Idno user_id
Table locations contains locID , lat & long & name values of a certain
table: CREATE TABLE `deal` ( `id` int(11) NOT NULL default '0', `site` int(11) NOT
Table: I'm wondering if there is a way to convert all my timestamps into
Table layout: CREATE TABLE t_order (id INT, custId INT, order DATE) I'm looking for

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.