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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:17:51+00:00 2026-05-16T20:17:51+00:00

Currently I am performing a left join on two tables. The first table has

  • 0

Currently I am performing a left join on two tables. The first table has an id and a persons name, the second table has an id, the id of a person from table 1, and then a timestamp (of a flight).

People                             Flights
id   |  name             id   |   person_id   | time
------------             ---------------------------
1       Dave              1         1          1284762115
2       Becky             2         1          1284787352
                          3         2          1284772629
                          4         2          1286432934
                          5         1          1283239480

When I perform my left join, I get a list of people and their flight times, but what I would like is just the list of people with the flight time with the highest ID

I have been using

   SELECT p.id, p.name max(f.time) 
     FROM People p 
LEFT JOIN Flights f ON p.id = f.person_id
 GROUP BY p.id, p.name

However, this just gives me the LAST flight time, rather than the last flight time uploaded into the system (ie, highest ID).

1 Dave  1284787352
2 Becky 1286432934

So to reiterate, I would like to see the name of the person, along with the flight time of their last UPLOADED (highest ID) flight time.

1 Dave  1283239480
2 Becky 1286432934
  • 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-16T20:17:52+00:00Added an answer on May 16, 2026 at 8:17 pm

    Use:

    SELECT p.id,
           p.name,
           f.time
      FROM PEOPLE p
      JOIN FLIGHTS f ON f.person_id = p.id
      JOIN (SELECT f.person_id,
                   MAX(f.id) AS max_id
              FROM FLIGHTS f
          GROUP BY f.person_id) x ON x.person_id = f.person_id
                                 AND x.max_id = f.id
    

    If you are using a database that supports analytics:

    SELECT p.id,
           p.name,
           x.time
      FROM PEOPLE p
      JOIN (SELECT f.person_id,
                   f.time,
                   ROW_NUMBER() OVER(PARTITION BY f.person_id
                                         ORDER BY f.id DESC) AS rk
              FROM FLIGHTS f) x ON x.person_id = p.id
                               AND x.rk = 1
    

    If you want people, including those without flights:

       SELECT p.id,
              p.name,
              f.time
         FROM PEOPLE p
    LEFT JOIN FLIGHTS f ON f.person_id = p.id
         JOIN (SELECT f.person_id,
                      MAX(f.id) AS max_id
                 FROM FLIGHTS f
             GROUP BY f.person_id) x ON x.person_id = f.person_id
                                    AND x.max_id = f.id
    

    …and the analytic version:

       SELECT p.id,
              p.name,
              x.time
         FROM PEOPLE p
    LEFT JOIN (SELECT f.person_id,
                      f.time,
                      ROW_NUMBER() OVER(PARTITION BY f.person_id
                                            ORDER BY f.id DESC) AS rk
                 FROM FLIGHTS f) x ON x.person_id = p.id
                                  AND x.rk = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.