Select nodes that don't have relationships

Hi,

Is it possible to select all the nodes that don’t have a relationship, within one query?

EG a one line query that does the job of:

allNodes = SELECT n FROM N:n;

nodesWithRelationships = SELECT nr FROM allNodes:nr-(MYREL)-OtherNodes

filteredNodes = allNodes MINUS nodesWithRelationships

Thanks,

Rene

Hi again Rene.

There is a vertex function called outdegree() , that returns the number of outgoing edges. If your vertex only has outgoing directed, or undirected edges, you can check for the outdegree of the vertices and select those with a return value of 0.

Here is the documentation for it : https://docs.tigergraph.com/dev/gsql-ref/querying/operators-functions-and-expressions#vertex-functions

create query queryname () for graph whatever {

  Start = {ANY};

  vSet = select s from Start:s 

         where s.outdegree() == 0;

}

OR

If your vertex has incoming edges, you can use something like this :

create query queryname () for graph whatever {

    OrAccum @visited;

    Start = {ANY};

    Start = select t from Start:s(:e)-:t

            accum t.@visited += true;

    vSet = select s from Start:s

           where s.outdegree() == 0 and s.@visited == false;

}

This will effectively mark the immediate neighbors of every node. The second select checks for those with 0 outgoing edges, and also if they have been marked during our first select statement.

-Kevin