How to return results
You can use the methods return_()
, limit()
, skip()
and order_by()
to
construct queries that will return data from the database.
return_(results: Optional[Union[str, Tuple[str, str], Dict[str, str], List[Union[str, Tuple[str, str]]], Set[Union[str, Tuple[str, str]]]]] = None)
- Return data from the database with aliases formatted askey AS value
.limit(integer_expression: Union[str, int])
- Limits the number of returned results equal tointeger_expression
.skip(integer_expression: Union[str, int])
- Skip the number of results to be returned equal tointeger_expression
.order_by(properties: Union[str, Tuple[str, Order], List[Union[str, Tuple[str, Order]]]])
- Order the returned results either descending or ascending.
Return all variables from a query
To return all the variables from a query, just use the return_()
method at the
end of your query:
- GQLAlchemy
- Cypher
from gqlalchemy import Match
query = Match().node(labels="Person", variable="p").return_().execute()
MATCH (p:Person) RETURN *;
Return specific variables from a query
To return only a subset of variables from a query, specify them in the
return()
method:
- GQLAlchemy
- Cypher
from gqlalchemy import Match
query = Match()
.node(labels="Person", variable="p1")
.to()
.node(labels="Person", variable="p2")
.return_(results=[("p1", "first"), "p2"])
.execute()
MATCH (p1:Person)-[]->(p2:Person) RETURN p1 AS first, p2;
Limit the number of returned results
To limit the number of returned results, use the limit()
method after the
return_()
method:
- GQLAlchemy
- Cypher
from gqlalchemy import Match
query = match().node(labels="Person", variable="p").return_().limit(10).execute()
MATCH (p:Person) RETURN * LIMIT 10;
Order the returned results
You can order the returned results by one or more
values in an ascending or descending order. The
default ordering in the Cypher query language is ascending (ASC
or
ASCENDING
), and if you want the descending order, you need to add the DESC
or DESCENDING
keyword to the ORDER BY
clause.
Order by one value
To order the return results by one value, use the order_by(properties)
method,
where properties
can be a string (a property) or a tuple of two strings (a
property and an order).
The following query will order the results in an ascending (default) order by
the property id
of a node.
- GQLAlchemy
- Cypher
from gqlalchemy import Match
query = Match().node(variable="n").return_().order_by(properties="n.id").execute()
MATCH (n) RETURN * ORDER BY n.id;
You can also emphasize that you want an ascending order:
- GQLAlchemy
- Cypher
from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Order
query = Match().node(variable="n").return_().order_by(properties=("n.id", Order.ASC).execute()
MATCH (n) RETURN * ORDER BY n.id ASC;
The same can be done with the keyword ASCENDING
:
- GQLAlchemy
- Cypher
from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Order
query = Match().node(variable="n").return_().order_by(properties=("n.id", Order.ASCENDING).execute()
MATCH (n) RETURN * ORDER BY n.id ASCENDING;
Order
is an enumeration class defined in the
query_module.py
.
It will help you in adding the correct order. If you don't want to import it,
you can use strings: "ASC"
, "ASCENDING"
, "DESC"
or "DESCENDING"
.
To order the query results in descending order, you need to specify the DESC
or DESCENDING
keyword. Hence, the argument of the order_by()
method must be
a tuple.
- GQLAlchemy
- Cypher
from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Order
query = Match().node(variable="n").return_().order_by(properties=("n.id", Order.DESC).execute()
MATCH (n) RETURN * ORDER BY n.id DESC;
Similarly, you can use Order.DESCENDING
to get DESCENDING
keyword in ORDER BY
clause.
Order by a list of values
To order the returned results by more than one value, use the
order_by(properties)
method, where properties
can be a list of strings or
tuples of strings (list of properties with or without order).
The following query will order the results in ascending order by the property
id
, then again in ascending (default) order by the property name
of a node.
After that, it will order the results in descending order by the property
last_name
, then in ascending order by the property age
of a node. Lastly,
the query will order the results in descending order by the node property
middle_name
.
- GQLAlchemy
- Cypher
from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Order
query = Match()
.node(variable="n")
.return_()
.order_by(
properties=[
("n.id", Order.ASC),
"n.name",
("n.last_name", Order.DESC),
("n.age", Order.ASCENDING),
("n.middle_name", Order.DESCENDING),
]
)
MATCH (n) RETURN * ORDER BY n.id ASC, n.name, n.last_name DESC, n.age ASCENDING, n.middle_name DESCENDING;
Hopefully, this guide has taught you how to return the query results. If you have any more questions, join our community and ping us on Discord.