Information about youngest child with inner subquery Consider a table like this :

CREATE TABLE people (
  id INTEGER NOT NULL PRIMARY KEY,
  motherId INTEGER REFERENCES people(id),
  fatherId INTEGER REFERENCES people(id),
  name VARCHAR(30) NOT NULL,
  age INTEGER NOT NULL
);


Populated with data like this :

INSERT INTO people(id, motherId, fatherId, name, age) VALUES(1, NULL, NULL, 'John', 50);
INSERT INTO people(id, motherId, fatherId, name, age) VALUES(2, NULL, NULL, 'Marry', 50);
INSERT INTO people(id, motherId, fatherId, name, age) VALUES(3, 2, 1, 'Bob', 30);
INSERT INTO people(id, motherId, fatherId, name, age) VALUES(4, 2, 1, 'Alice', 20);


The expectation is to have output like this (order does not matter) :

 name  | age
-------+-----
 John  | 20
 Marry | 20


The explanation for the output is John and Marry are parents and have two children: Bob and Alice. Alice is younger and is 20 years old.

This is my solution :

select parents.name as name, kids.age as age from people kids
  inner JOIN
  (
    SELECT id, name FROM people where motherId is null and fatherId is null
  ) parents
  ON (parents.id = kids.motherId OR parents.id = kids.fatherId)
  group by parents.id


However this trips when several generations of parents have the same name.  Any ideas why this might be happening?
You should use minimum age in the select clause so that group by is meaningful. Also, why do you need the motherId and fatherId to be null in nested subquery? If you do not add these conditions then you do not need a nested subquery:

select parents.name as name, min(kids.age) as age 
from
people kids
inner JOIN
people  parents
ON (parents.id = kids.motherId OR parents.id = kids.fatherId)
group by parents.id


By the way, does the query you posted really works? In PostgreSQL 9.3.23 I am getting an error:


  column "parents.name" must appear in the GROUP BY clause or be used in
  an aggregate function