Ordering and Limits

Ordering results

You can order the results of a query using the ORDER BY clause.

This query finds all the people born in 1980 and orders them by their birth date:

cypher
MATCH (p:Person)
WHERE p.born.year = 1980
RETURN p.born AS birthDate
ORDER BY p.born

You can split dates into their components using the year, month, and day properties.

Ordering results

You can order by one or more columns, and specify the order as ascending (ASC) or descending (DESC).

cypher
MATCH (p:Person)
WHERE p.born.year = 1980
RETURN p.name AS name, p.born AS birthDate
ORDER BY p.born DESC, p.name ASC

Limiting results

The number of results returned by a query can be limited using the LIMIT clause.

This query finds the first 5 movies released in the year 2000:

cypher
MATCH (m:Movie)
WHERE m.year = 2000
ORDER BY m.released
RETURN m.title, m.released
LIMIT 5

Challenges

Complete the queries to find the following data:

  1. Steven Spielberg movies, ordered by the year they were released.

    cypher
    MATCH (p:Person)-[:DIRECTED]->(m:??????)
    WHERE p.???? = "Steven Spielberg"
    ????? ?? m.released
    RETURN m.title, m.released
  2. Return the top 10 most recently released movies

    cypher
    MATCH (m:Movie)
    ????? ?? m.released ????
    RETURN m.title, m.released
    ????? 10
Click to reveal the answers
  1. Return Steven Spielberg movies, ordered by the year they were released.

    cypher
    MATCH (p:Person)-[:DIRECTED]->(m:Movie)
    WHERE p.name = "Steven Spielberg"
    ORDER BY m.released
    RETURN m.title, m.released
  2. Return the top 10 most recently released movies

    cypher
    MATCH (m:Movie)
    ORDER BY m.released DESC
    RETURN m.title, m.released
    LIMIT 10

Summary

In this lesson, you learned how to order and limit the returned results.