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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:22:57+00:00 2026-05-28T02:22:57+00:00

As everyone knows, you can’t return non-grouped non-aggregated columns in a GROUP BY ,

  • 0

As “everyone knows”, you can’t return non-grouped non-aggregated columns in a GROUP BY, in other words, “give me the ID, name and address of the employee with the highest salary in each department.” Of course this isn’t quite true: http://dev.mysql.com/doc/refman/5.1/en/group-by-hidden-columns.html But this contains a rather ominous warning:

The server is free to choose any value from each group, so unless they
are the same, the values chosen are indeterminate.

MySQL has another article on this problem: http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html But the technique recommended there doesn’t actually take advantage at all of hidden columns. There’s a comment in that article from Kasey Speakman, who recommends using an ordered subquery, like so:

select deptno, emp_id, address, name from
(select * from emp order by salary desc)
group by deptno

My questions are: a) Can I safely rely on MySQL to pick the “first” row from each group, since the subquery is ordered, and b) in general, and assuming appropriate indexes, is this likely to perform better than, say, the LEFT JOIN technique mentioned in the same article?

  • 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-28T02:22:57+00:00Added an answer on May 28, 2026 at 2:22 am

    There was recently a discussion on a similar question here: SQL: What is the default Order By of queries?

    But, nevertheless, I think ranking queries are the example of the queries in MySQL where it is quite useful to rely on the predicted order (predicted by using specific indexes).

    Look at my answer to the following question: Retrieving the last record in each group

    That is the answers to your questions:

    1. yes, sometimes you can rely on the order when you know the engine and the indexes used, though it is not usually friendly accepted

    2. when there are many items within each group the LEFT JOIN solution might take too long to execute, so that relying on the bare indexes might become almost the only solution. But the solution should not generate huge intermediate temporary tables.

    But your query:

    select deptno, emp_id, address, name from
    (select * from emp order by salary desc)
    group by deptno
    

    is the worst possible idea, since it generates an unindexed copy of your table and operates on it making no use of any optimizations.

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

Sidebar

Related Questions

Everyone knows the MessageBox.Show() method, that returns DialogResult value after dialog closed. How can
Everyone who's done any web application development in Scala knows that you can use
Well, as everyone knows there is no way you can assign permissions for Copy/Paste
Not everyone knows that php can handle variables that have first characters as numbers,
Ok, it seems everyone else knows this since I can't find it on the
Simple question: everyone knows you can POST data in a HTML form to the
Everyone knows the Run window that you can open with the shortcut Windows+R or
I have a problem about the font in iphone/ipad Everyone knows UILabel can't do
Everyone knows the = sign. SELECT * FROM mytable WHERE column1 = column2; However,
as everyone knows Windows does paths with backslashes where Unix does paths with forward

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.