P: General Queries
This page contains a selection of general queries which can be used to explore the graph database. The queries are designed to provide insights into the data and relationships between nodes.
List all nodes
The following query lists all nodes in the graph.
Consider size of graph and limits in settings before running this query.
MATCH (n)
RETURN n
datatype of property
Properties in a graph have datatypes which will enable different operations and there for insights. The query below returns the datatype of a node property.
/* return datatype of actStartTime on activity node */
MATCH (a:activity)
RETURN DISTINCT apoc.meta.cypher.type(a.actStartTime) as actStartTimeType
unique properties
A node or relationship can potentially have many properties. The query below lists the properties of a node - in this case, activity.
// List unique properties for a Node
MATCH (a:activity)
UNWIND keys(a) AS propertyKey
RETURN COLLECT(DISTINCT propertyKey) AS propertyKeys
//RETURN DISTINCT propertyKey as propertyKeys
node labels without relationships
Graph databases are all about the relationships between nodes. It can be useful identifying nodes without relationships as they could indicate a problem with the data, data loading mechanism or be the outliers you want to identify.
For example, in a timetabling scenario, we would expect all nodes to be related to another node. However, we can see that several node labels are orphans. In the proof-of-concept, these results are expected or deliberate, due to the source data.
// Find node labels without relationships and their count
MATCH (n)
WHERE NOT EXISTS(()-[]-(n)) AND NOT EXISTS((n)-[]-())
RETURN DISTINCT labels(n) AS nodeLabels, count(n) AS nodeCount
nodes without relationships - aka orphans
Instead of returning a count of nodes without relationships per node label we can return the nodes as a graph or a table:
// Find nodes without relationships
MATCH (n)
WHERE NOT EXISTS(()-[]-(n)) AND NOT EXISTS((n)-[]-())
RETURN n
students without activities
In the timetabling context, we would expect students to be allocated to activities. It turns out that we have 219 students without activities. A bit more investigation indidates that they are all from a particular programme of study run.
// Students without Activities
MATCH (s:student)
WHERE NOT (s)-[:ATTENDS]->()
RETURN s
activityType without activity
Activities can have a activity type - the graph model could have activity type as a property or as a relationship. The query below finds activity typewithout activity. The decision may be to delete these orphaned nodes as they may cause problems with some calculations. They would be created if they become required in the future.
// Activities without Rooms
MATCH (at:activityType)
WHERE NOT (at)<-[:HAS_TYPE]-()
RETURN at;
activities without rooms
The graph has over 1500 activity instances without rooms. Most of these will be deliberate - online, virtual sessions - but we may want to query the graph to identify those where a room is expected.
// Activities without Rooms
MATCH (a:activity)
WHERE NOT (a)-[]->(:room)
RETURN a;







