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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:21:08+00:00 2026-06-17T11:21:08+00:00

I have a MySQL table having three columns id, name and height. I want

  • 0

I have a MySQL table having three columns id, name and height.

I want to run a query to return the name and height of all entries, along with the average height of all entries. All these things have to be done in a single query.

This is what I tried.

SELECT name, height, avg(height) as average_height FROM mytable

This query returns only a single row, but the average height is correct.

name    height    average_height
--------------------------------
Harry   165       169.5

then I tried this query

SELECT name, height, avg(height) as average_height FROM mytable GROUP BY name

then it returns all the rows, but the average is not correct

name    height    average_height
--------------------------------
Harry   165       165
Tom     170       170
George  180       180
Raj     163       163

How can I get the average as well as all the rows in a single query.

  • 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-17T11:21:09+00:00Added an answer on June 17, 2026 at 11:21 am

    Here is one way:

    SELECT name, height, average_height FROM mytable CROSS JOIN (
       select avg(height) as average_height FROM mytable
    ) as avg;
    

    SQLFiddle example (Updated example to reflect suggestion to use CROSS JOIN).

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

Sidebar

Related Questions

I have MySQL InnoDB table utf-8 encoded. This table has only id and name
I have a MySQL table and would like to return the rows based on
I have a MySQL table with an auto-incremented integer primary key. I want to
I have a mysql table structure like that: id int primary key name varchar
I have a table in mysql which stores (among other columns) a bitmask as
I have a table with 3 columns (A,B,C). I want to select some rows
I am having trouble searching multiple columns in a MySQL database table with user
I have a php script that process keywords from a mysql table keywords (columns:
I have a mysql table storing members messages with 4 columns : message_id (primary
I have a MySQL database table with two columns that interest me. Individually they

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.