More accurate SQL query
Device is now responsible of building his own parents