DEV Community

Talha Munir 🇵🇸
Talha Munir 🇵🇸

Posted on • Edited on

Description of important clauses in SQL and Apache age query format part 2

In this blog we will discuss some of the important clauses in Apache age here. For part 1 you can visit here

Match:

The MATCH clause allows you to specify the patterns Cypher will search for in the database.
Cypher:

SELECT * FROM cypher('graph_name', $$
MATCH (v)
RETURN v
$$) as (v agtype);
Enter fullscreen mode Exit fullscreen mode

Above query will return all the vertices in the graph.

Return:

Return statement returns a node
Cypher:

SELECT *
FROM cypher('graph_name', $$
    MATCH (n {name: 'B'})
    RETURN n
$$) as (n agtype);
Enter fullscreen mode Exit fullscreen mode

Above query will return node n.

Order by:

Order by is used for sorting of output on basis of properties of the output.
Cypher:

SELECT *
FROM cypher('graph_name', $$
    MATCH (n)
    WITH n.name as name, n.age as age
    ORDER BY n.name
    RETURN name, age
$$) as (name agtype, age agtype);
Enter fullscreen mode Exit fullscreen mode

The query returns the nodes sorted by the name.

Limit:

Limit is used to constraint the number of output records.
Cypher:

SELECT * 
FROM cypher('graph_name', $$
    MATCH (n)RETURN n.name
    ORDER BY n.name
    LIMIT 3
$$) as (names agtype);
Enter fullscreen mode Exit fullscreen mode

The query returns the name of first 3 nodes.

Create:

Create clause is used to create graph vertices and edges.
Cypher for creating a vertex:

SELECT * 
FROM cypher('graph_name', $$
    CREATE (n)
$$) as (v agtype);
Enter fullscreen mode Exit fullscreen mode

Above query will return nothing.

Cypher for creating an edge between 2 nodes:

SELECT * 
FROM cypher('graph_name', $$
    MATCH (a:Person), (b:Person)
    WHERE a.name = 'Node A' AND b.name = 'Node B'
    CREATE (a)-[e:RELTYPE]->(b)
    RETURN e
$$) as (e agtype);
Enter fullscreen mode Exit fullscreen mode

Above query will return the created edge.

Delete:

The delete clause is used to delete the graph and vertices. You cannot delete a node without also deleting edges that start or end on said vertex. Either explicitly delete the vertices or use DETACH DELETE.
Cypher:

SELECT * 
FROM cypher('graph_name', $$
    MATCH (v:Useless)
    DELETE v
$$) as (v agtype);
Enter fullscreen mode Exit fullscreen mode

Above query will delete the selected vertex.

Remove

Remove and delete are completely different clauses. Remove is used to remove properties from vertex and edges.
Cypher:

SELECT * 
FROM cypher('graph_name', $$
    MATCH (andres {name: 'Andres'})
    REMOVE andres.age
    RETURN andres
$$) as (andres agtype);
Enter fullscreen mode Exit fullscreen mode

The returned node has no age property in it.
References:

  1. https://age.apache.org

Top comments (0)