Limit
LIMIT
controls the number of tuples returned from a query. It is often used within in an ORDER BY
clause to fetch the top-k tuples from the query result. Note that LIMIT
accepts any expression that can be evaluated to an integer.
We will use the example database for demonstration, whose schema and data import commands are given here.
For example, the following query returns the top three oldest users.
MATCH (u:User)RETURN u.nameORDER BY u.age DESCLIMIT 3;
Result:
┌─────────┐│ u.name ││ STRING │├─────────┤│ Zhang ││ Karissa ││ Adam │└─────────┘
If you omit the ORDER BY
, you would get some k tuples in a LIMIT k
query
but you have no guarantee about which ones will be selected.
The number of rows to limit can either be:
- A parameter expression when used with prepared statement:
Prepare:
auto prepared = conn->prepare("MATCH (u:User) RETURN u.name limit $lt")
Execution: The number of rows to limit can be given at the time of execution.
conn->execute(prepared.get(), std::make_pair(std::string{"lt"}, 1))
Result:
┌────────┐│ u.name ││ STRING │├────────┤│ Adam │└────────┘
- A literal expression which can be evaluated at compile time.
MATCH (u:User)RETURN u.namelimit 1+2
Result:
┌─────────┐│ u.name ││ STRING │├─────────┤│ Adam ││ Karissa ││ Zhang │└─────────┘