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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:39:01+00:00 2026-05-25T01:39:01+00:00

Given the SQL code below: create table A(a integer, b integer, c double); insert

  • 0

Given the SQL code below:

create table A(a integer, b integer, c double);

insert into A(a, b, c)
values
(0, 0, 1.1),(1, 0, 1.2),(2, 0, 1.3),
(0, 1, 1.4),(1, 1, 1.5),(2, 1, 1.6),
(0, 2, 1.7),(1, 2, 1.8),(2, 2, 1.9),
(0, 0, 1.9),(1, 0, 1.8),(2, 0, 1.7),
(0, 1, 1.6),(1, 1, 1.5),(2, 1, 1.4),
(0, 2, 1.3),(1, 2, 1.2),(2, 2, 1.1)

mysql> select * from A where a = 0;
+------+------+------+
| a    | b    | c    |
+------+------+------+
|    0 |    0 |  1.1 |
|    0 |    1 |  1.4 |
|    0 |    2 |  1.7 |
|    0 |    0 |  1.9 |
|    0 |    1 |  1.6 |
|    0 |    2 |  1.3 |
+------+------+------+

mysql> select * from A where a = 0 group by b;
+------+------+------+
| a    | b    | c    |
+------+------+------+
|    0 |    0 |  1.1 |
|    0 |    1 |  1.4 |
|    0 |    2 |  1.7 |
+------+------+------+
3 rows in set (0.00 sec)

Why 3 rows? I would have thought both queries should return the same result since unique combinations of c exist.

Further

mysql> select *, sum(c) from A group by b;
+------+------+------+--------+
| a    | b    | c    | sum(c) |
+------+------+------+--------+
|    0 |    0 |  1.1 |      9 |
|    0 |    1 |  1.4 |      9 |
|    0 |    2 |  1.7 |      9 |
+------+------+------+--------+
3 rows in set (0.00 sec)

the sum seems to be the sum of all the rows rather than the sum of the groupings

Any ideas why I’m seeing this strange behaviour?

  • 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-25T01:39:02+00:00Added an answer on May 25, 2026 at 1:39 am

    Here is clearly mentioned

    In standard SQL, a query that includes a GROUP BY clause cannot refer
    to nonaggregated columns in the select list that are not named in the
    GROUP BY clause. For example, this query is illegal in standard SQL
    because the name column in the select list does not appear in the
    GROUP BY:

    SELECT o.custid, c.name, MAX(o.payment)
      FROM orders AS o, customers AS c
      WHERE o.custid = c.custid
      GROUP BY o.custid;
    

    For the query to be legal, the name column must be omitted from the
    select list or named in the GROUP BY clause.

    MySQL extends the use of GROUP BY so that the select list can refer to
    nonaggregated columns not named in the GROUP BY clause.

    References:

    http://rpbouman.blogspot.com/2007/05/debunking-group-by-myths.html

    http://explainextended.com/2011/03/30/mysql-group-by-in-union/

    http://20bits.com/articles/10-tips-for-optimizing-mysql-queries-that-dont-suck/

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

Sidebar

Related Questions

What is the SQL to define DEFAULT values in MySQL? In the code below
Given this SQL: SELECT * FROM mytable ORDER BY mycolumn, RAND() Assuming that mycolumn
I have a organization name table with the following structure given below: CREATE TABLE
I have the below SQL I am trying to use to create a table
Given the following table in SQL Server 2005: ID Col1 Col2 Col3 -- ----
I want to create a veiw and then select from it in one query,
Can I refactor the below SQL CASE statements into single for each case ?
I have the below SQL which works just fine: SELECT Message, CreateDate, AccountId, AlertTypeId
The below given code of is taking infinitely long time to load in a
I want to take back up of mysql database table in sql file through

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.