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

  • Home
  • SEARCH
  • 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 119099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:35:58+00:00 2026-05-11T03:35:58+00:00

I have a database table like this: id version_id field1 field2 1 1 texta

  • 0

I have a database table like this:

id    version_id    field1    field2 1     1             texta      text1 1     2             textb      text2 2     1             textc      text3 2     2             textd      text4 2     3             texte      text5 

If you didn’t work it out, it contains a number of versions of a row, and then some text data.

I want to query it and return the version with the highest number for each id. (so the second and last rows only in the above).

I’ve tried using group by whilst ordering by version_id DESC – but it seems to order after its grouped, so this doesn’t work.

Anyone got any ideas? I can’t believe it can’t be done!

UPDATE:

Come up with this, which works, but uses a subquery:

SELECT * FROM (SELECT * FROM table ORDER BY version_id DESC) t1 GROUP BY t1.id 
  • 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. 2026-05-11T03:35:58+00:00Added an answer on May 11, 2026 at 3:35 am

    It’s called selecting the group-wise maximum of a column. Here are several different approaches for mysql.

    Here’s how I would do it:

    SELECT * FROM (SELECT id, max(version_id) as version_id FROM table GROUP BY id) t1 INNER JOIN table t2 on t2.id=t1.id and t1.version_id=t2.version_id 

    This will be relatively efficient, though mysql will create a temporary table in memory for the subquery. I assume you already have an index on (id, version_id) for this table.

    It’s a deficiency in SQL that you more or less have to use a subquery for this type of problem (semi-joins are another example).

    Subqueries are not well optimized in mysql but uncorrelated subqueries aren’t so bad as long as they aren’t so enormous that they will get written to disk rather than memory. Given that in this query only has two ints the subquery could be millions of rows long before that happened but the select * subquery in your first query could suffer from this problem much sooner.

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

Sidebar

Ask A Question

Stats

  • Questions 80k
  • Answers 80k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Itay, A very good question. Fortunately for you the answer… May 11, 2026 at 4:24 pm
  • Editorial Team
    Editorial Team added an answer You need a kick start with SSRS? I can help… May 11, 2026 at 4:24 pm
  • Editorial Team
    Editorial Team added an answer There isn't a good, Pythonic way to do this with… May 11, 2026 at 4:24 pm

Related Questions

I am intentionally leaving this quite vague at first. I'm looking for discussion and
Here is a simplified version of my database model. I have two tables: Image,
We had a meeting this morning about how would should store our ID for
I have an existing database with the table Transactions in it. I have added

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.