neo4j
  1. neo4j-limit-clause

Limit Clause - (Neo4j General Clauses)

The LIMIT clause is a general clause in Neo4j used to limit the number of return results from a query. It allows you to specify how many results should be returned by the query and can be used with other clauses like MATCH, WHERE, RETURN, and ORDER BY.

Syntax

The syntax for using the LIMIT clause is as follows:

MATCH n
LIMIT x
RETURN n
  • n represents the node(s) being matched.
  • x represents the number of results to return.

You can also use the SKIP and LIMIT clauses together to specify a range of results to return:

MATCH n
SKIP y
LIMIT x
RETURN n
  • y represents the number of results to skip before beginning to return results.

Example

Suppose we have the following nodes representing employees in a company:

(:Employee {name: 'John Smith', salary: 50000})
(:Employee {name: 'Jane Doe', salary: 75000})
(:Employee {name: 'Bob Johnson', salary: 60000})
(:Employee {name: 'Alice Brown', salary: 90000})
(:Employee {name: 'Mark White', salary: 80000})

If we want to retrieve the first 3 employees from the list, we can use the LIMIT clause as follows:

MATCH (e:Employee)
LIMIT 3
RETURN e.name, e.salary

This will return the following results:

╒══════════════╤═════════╕
│"e.name"      │"e.salary"│
╞══════════════╪═════════╡
│"John Smith"  │50000    │
├──────────────┼─────────┤
│"Jane Doe"    │75000    │
├──────────────┼─────────┤
│"Bob Johnson" │60000    │
╘══════════════╧═════════╛

Output

The LIMIT clause limits the number of results returned by the query and can be useful when dealing with large datasets. It returns a specified number of nodes/rows from the query result.

Explanation

The LIMIT clause specifies a limit on the number of records returned by the MATCH clause. Without the LIMIT clause, Neo4j will return all records that meet the criteria specified by the MATCH clause.

The LIMIT clause can be used in combination with other clauses like WHERE, RETURN, and ORDER BY.

Use

The LIMIT clause is useful when working with large datasets and you only need to return a specific number of records from the result set. It ensures that only the most relevant and critical data is returned, which speeds up the query and reduces the amount of memory required to process the query.

Important Points

  • The LIMIT clause limits the number of records returned by a query.
  • It is used in combination with other clauses like MATCH, WHERE, RETURN, and ORDER BY.
  • It is useful when dealing with large datasets and you only need to return a specific number of records from the result set.

Summary

The LIMIT clause is a general clause in Neo4j used to limit the number of records returned by a query. It is useful when only a specific number of records are required from a large dataset. The syntax is simple, comprising of the MATCH and LIMIT clauses. It can be used in combination with other clauses and is an effective way to optimize queries.

Published on: