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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:01:19+00:00 2026-06-06T11:01:19+00:00

I have these two tables people ============ id, name and answer_sheets ============ id, person_id,

  • 0

I have these two tables

people
============
id, name

and

answer_sheets
============
id, person_id, answer, date_answered
  • person_id is a foreign key from people.id

Now, what I wanted to do is to sort people in such in order basing it on the latest answer_sheets.date_answered (we can derive that one people row can have many answer_sheets rows)

Say for example we have the tables

people  
============
id  name  
1   Person1  
2   Person2  
3   Person3  
4   Person4  
5   Person5  


answer_sheets  
=============
id  person_id   answer  date_answered  
1   1           string  JUN 13  
2   2           string  JUN 15  
3   3           string  JUN 17  
4   2           string  JUN 18  
5   1           string  JUN 19  
6   3           string  JUN 20  
7   2           string  JUN 25  

and I wanted to order people in ASC order based on a people row’s answer_sheets.date_answered

the output must be

=============
id  name      last_date_answered
4   Person4   NIL
5   Person5   NIL
1   Person1   JUN 19
3   Person3   JUN 20
2   Person2   JUN 25

You can observe that people with ids 4 and 5 does not have an answer_sheet and yet they should be included in the list.
Question: What must be the appropriate SQL query for this? Thanks.

  • 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-06T11:01:25+00:00Added an answer on June 6, 2026 at 11:01 am

    To get records to display even if there is no match, you can use a LEFT JOIN.

    SELECT p.id, p.name, MAX(a.date_answered)
    FROM people p
    LEFT JOIN answer_sheets a on p.id = a.personID
    GROUP BY p.id, p.name
    ORDER BY MAX(date_answered) ASC
    

    And, if you want to try it out, or play with it more, I made a SQLFiddle…

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

Sidebar

Related Questions

I have two tables, like these: Table People: VARCHAR Name INTEGER Age Table Message
I have these two tables. The connecting key is conID which is included in
I have two tables table: people id name goods_owned 1 john 1,4,3 2 Mike
If I have these two tables: <table> <tr> <td>Small Length Content</td> </tr> </table> <table>
We have two tables Family and Member, the relation between these two is Family
I have two tables, tbl_foo and tbl_bar , and I want to join these
I have two mysql tables and i want to merge the results of these
I have those two tables: client: id (int) #PK name (varchar) client_category: id (int)
So I have two tables in this simplified example: People and Houses. People can
I have two tables one named Person , which contains columns ID and Name

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.