Cypher cannot be used in an expression— the query must exist in the FROM clause of a query. However, if the cypher query is placed in a subquery, it will behave as any SQL style query.

Using Cypher with ‘=’

When writing a cypher query that is known to return one column and one row, the ‘=’ comparison operator may be used.

  1. SELECT t.name FROM schema_name.sql_person AS t
  2. where t.name = (
  3. SELECT a
  4. FROM cypher('graph_name', $$
  5. MATCH (v)
  6. RETURN v.name
  7. $$) as (name varchar(50))
  8. ORDER BY name
  9. LIMIT 1);

Results:

name age
‘Andres’ 36
3 row(s) returned

Working with Postgres’s IN Clause

When writing a cypher query that is known to return one column, but may have multiple rows. The IN operator may be used.

Query:

  1. SELECT t.name, t.age FROM schema_name.sql_person as t
  2. where t.name in (
  3. SELECT *
  4. FROM cypher('graph_name', $$
  5. MATCH (v:Person)
  6. RETURN v.name
  7. $$) as (a agtype));

Results:

name age
‘Andres’ 36
‘Tobias’ 25
‘Peter’ 35
3 row(s) returned

Working with the Postgres EXISTS Clause

When writing a cypher query that may have more than one column and row returned. The EXISTS operator may be used.

Query:

  1. SELECT t.name, t.age
  2. FROM schema_name.sql_person as t
  3. WHERE EXISTS (
  4. SELECT *
  5. FROM cypher('graph_name', $$
  6. MATCH (v:Person)
  7. RETURN v.name, v.age
  8. $$) as (name agtype, age agtype)
  9. WHERE name = t.name AND age = t.age
  10. );

Results:

name age
‘Andres’ 36
‘Tobias’ 25
3 row(s) returned

Querying Multiple Graphs

There is no restriction to the number of graphs an SQL statement can query. Users may query multiple graphs simultaneously.

  1. SELECT graph_1.name, graph_1.age, graph_2.license_number
  2. FROM cypher('graph_1', $$
  3. MATCH (v:Person)
  4. RETURN v.name, v.age
  5. $$) as graph_1(col_1 agtype, col_2 agtype, col_3 agtype)
  6. JOIN cypher('graph_2', $$
  7. MATCH (v:Doctor)
  8. RETURN v.name, v.license_number
  9. $$) as graph_2(name agtype, license_number agtype)
  10. ON graph_1.name = graph_2.name

Results:

name age license_number
‘Andres’ 36 1234567890
3 row(s) returned