Passionate programmer
Passionate programmer

Reputation: 5898

Find parent node?

There are two tables one with node names and another with connection details(child, parent) between the nodes find the node which has no parent i.e, root node. Using SQL query.

Upvotes: 1

Views: 432

Answers (2)

Brian Hooper
Brian Hooper

Reputation: 22044

I'd go for NOT EXISTS rather than NOT IN, as NOT IN can get slow.

SELECT *
    FROM nodes
    WHERE NOT EXISTS (SELECT *
                          FROM connectionTable
                          WHERE connectionTable.child_id = nodes.node_id)

Upvotes: 2

Oded
Oded

Reputation: 498972

Here is a way to do this with a subquery:

SELECT *
FROM nodes
WHERE node_id NOT IN
(SELECT child_id FROM connectionTable)

Upvotes: 2

Related Questions