2

I am trying to order the results of a sql query of one table using the column of another table. Is it possible to include this column from a different table in my results?

For example, this query uses an inner join to order items in the Children table based on the Parent table's column "dateReceived":

SELECT c.* 
FROM children c
INNER JOIN parent p
ON c.parent_id = p.parent_id  
ORDER BY dateReceived ASC;

Results: results

The view would be more helpful if it could somehow include the "dateReceived" column from the Parent table in the results. I am new to SQL, is this possible?

Thank you.

4
  • ?? Does each child have only one parent? Parthenogenesis?
    – Akina
    Commented Mar 8, 2022 at 19:46
  • This is just how families are entered into this system, there may be two parents, but only one need fill out the entry form
    – Kapernski
    Commented Mar 8, 2022 at 19:54
  • I see. I just assumed that two links are possible, which will lead to duplicates in the output.
    – Akina
    Commented Mar 8, 2022 at 20:02
  • Hi, and welcome to dba.se! Please do not upload images of text for the reasons outlined in this link.
    – Vérace
    Commented Mar 9, 2022 at 6:11

2 Answers 2

3

Yes all you need to do is add that column to your SELECT list via the correct alias like so:

SELECT p.dateReceived, c.*
FROM children c
INNER JOIN parent p
ON c.parent_id = p.parent_id  
ORDER BY p.dateReceived ASC;

Notice that the alias p represents your parent table and c represents your children table in the context of this query.

Also you shouldn't use * in your SELECT list (most times) because it is bad practice for readability, maintainability, performance reasons, and is error prone. Instead you should explicitly list out each column that you want returned. For example:

SELECT 
    p.dateReceived, 
    c.child_id, 
    c.parent_id, 
    c.firstName, 
    c.gender, 
    c.age, 
    c.shoe, 
    c.notes
FROM children c
INNER JOIN parent p
ON c.parent_id = p.parent_id  
ORDER BY p.dateReceived ASC;
5
  • 1
    Awesome! Thank you :)
    – Kapernski
    Commented Mar 8, 2022 at 19:31
  • @Kapernski No problem! Please see my updated answer regarding using * and best practices too. Best of luck! 🙂
    – J.D.
    Commented Mar 8, 2022 at 19:34
  • 1
    @J.D. beat me by 45 seconds. Commented Mar 8, 2022 at 19:41
  • 2
    @BrendanMcCaffrey Yea, I'm one-handing it on my phone right now while eating lunch, lol. But have an upvote. 🙂
    – J.D.
    Commented Mar 8, 2022 at 19:42
  • @Kapernski please considering accepting J.D.'s answer, if it provided what you needed. Commented Mar 8, 2022 at 19:43
3

Replacing c.* with * would return the results from both tables. As you currently have it written, c is the alias for children table. So, you're only getting the columns from that table.

Try this...

SELECT c.*, p.dateReceived
FROM children c
INNER JOIN parent p
ON c.parent_id = p.parent_id  
ORDER BY dateReceived ASC;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.