openCypher Syntax Cheatsheet

February 28, 2023 ยท View on GitHub

The below are some common examples of openCypher Syntax based upon the air-routes dataset provided by Kelvin Lawrence.

Table of Contents

Read Queries

Pattern Matching

Pattern matching is the most basic actions one can do in openCypher and is the basis for all read queries.

Find Nodes with Label

MATCH (a:airport) RETURN a

Find Nodes by Label and Property

MATCH (a:airport {code: 'SEA'}) RETURN a
MATCH (a:airport) WHERE a.code='SEA' RETURN a

Find pattern

MATCH (a:airport {code: 'SEA'})-[:route]->(d) RETURN d
MATCH (a:airport {code: 'SEA'})<-[:route]-(d) RETURN d

Find path pattern

MATCH p=(a:airport {code: 'SEA'})-[:route]->(d) RETURN p

Find optional pattern

OPTIONAL MATCH (a:airport {code: 'SEA'})-[:route]->(d) RETURN d

Find if something exists

MATCH (a:airport {code: 'SEA'})-[:route]->(d) RETURN d

Looping

Looping through connections is a common pattern in property graphs and can be accomplished using either fixed or variable length paths.

Fixed number of loops

MATCH (a:airport {code: 'SEA'}*2)-[:route]->(d) RETURN d

Range of hops

MATCH (a:airport {code: 'SEA'})-[:route*1..3]->(d) RETURN d

Range of hops (minimum boundary only)

MATCH (a:airport {code: 'SEA'})-[:route*1..]->(d) RETURN d

Range of hops (maximum boundary only)

MATCH (a:airport {code: 'SEA'})-[:route*..3]->(d) RETURN d

Returning Values

Each read query must specify how to return values from the query.

Return everything

MATCH (a) RETURN *

Return property

MATCH (a:airport) RETURN a.city

Return property with alias

MATCH (a:airport) RETURN a.city AS dest

Return distinct values

MATCH (a:airport) RETURN DISTINCT a.region

Order results ascending

MATCH (a:airport) RETURN a ORDER BY a.elev

Order results descending

MATCH (a:airport) RETURN a ORDER BY a.elev DESC

Return count of results

MATCH (a:airport) RETURN count(a)

Limit results

MATCH (a:airport) RETURN a LIMIT 5

Paginate results

MATCH (a:airport) RETURN a SKIP 5 LIMIT 5

Join distinct set from two queries

MATCH (a:airport {code:'SEA'}) 
RETURN a.city
UNION 
MATCH (a:airport {code:'ANC'}) 
RETURN a.elev

Join combined set from two queries

MATCH (a:airport {code:'SEA'})-->(d) 
RETURN d.city
UNION ALL
MATCH (a:airport {code:'ANC'})-->(d) 
RETURN d.city

Write Queries

Creating Data

Create a node

CREATE (a)

Create a node with label and no properties

CREATE (a:airport)

Create a node with label and properties

CREATE (a:airport {code: 'FOO'})

Create relationship

MATCH (a:airport {code:'SEA'}), (b:airport {code:'ANC'})
CREATE (a)-[r:route]->(b)
RETURN r

Create relationship with properties

MATCH (a:airport {code:'SEA'}), (b:airport {code:'ANC'})
CREATE (a)-[r:route {dist: 1000}]->(b)
RETURN r

Create a path

CREATE p = (a:airport {code:'Foo'})-[:route]->(a:airport {code:'Bar'})
RETURN p

Create from a list

UNWIND [{code: 'foo'}, {code: 'bar'}] AS properties
CREATE (a:airport) SET a = properties

Updating Data

Set a property

MATCH (a:airport {code: 'SEA'})
SET a.foo='bar'

Remove a property

MATCH (a:airport {code: 'SEA'})
SET a.foo=null
MATCH (a:airport {code: 'SEA'})
REMOVE a.foo

Merge a node

MERGE (a:airport {code: 'SEA'})
  ON CREATE SET n.created = timestamp()
  ON MATCH SET
    n.counter = coalesce(n.counter, 0) + 1,
    n.accessTime = timestamp()

Deleting Data

Delete a node

DELETE (a:airport {code: 'SEA'})

Delete a node and relationships

MATCH (a:airport {code: 'SEA'})
DETACH DELETE a

Delete relationships

MATCH (a:airport {code:'SEA'})-[r]->(d) 
DELETE r

Operators

TypeOperators
GeneralDISTINCT, x.y (property access)
Math+, -, *, /, %, ^
Comparison=, >, <, <>, <=, >=, IS NULL, IS NOT NULL
BooleanAND, OR, NOT, XOR
StringSTARTS WITH, ENDS WITH, CONTAINS, +
LIST+, IN, []

Functions

TypeFunctions
Predicateexists()
Scalarcoalesce(), endNode(), head(), id(0, last(), length(), properties(), size(), startNode(), timestamp(), toBoolean(), toFloat(), toInteger(), type()
Aggregatingavg(), collect(), count(), max(), min(), percentileCont(), percentileDisc(), stDev(), stDevP(), sum()
Listkeys(), labels(), nodes(), range(), relationships(), reverse(), tail()
Math - numericabs(), ceil(), floor(), rand(), round(), sign()
Math - logarithmice(), exp(), log(), log10(), sqrt()
Math - trigonometricacos(), asin(), atan(), atan2(), cos(), cot(), degree(), pi(), radians(), sin(), tan()
Stringleft(), lTrim(), replace(), reverse(), right(), rTrim(), split(), substring(), toLower(), toString(), toUpper(), trim()