No SQL Tutorial: Cypher WHERE, RETURN, ORDER BY 🎯

beginner
24 min

No SQL Tutorial: Cypher WHERE, RETURN, ORDER BY 🎯

Welcome to our deep dive into the world of No SQL databases! Today, we'll be focusing on Cypher - the query language for Neo4j, a popular No SQL database. We'll explore three essential Cypher commands: WHERE, RETURN, and ORDER BY. Let's get started! 📝

What is Cypher? 💡

Cypher is a graph-based query language for Neo4j. It allows you to create, read, update, and delete nodes and relationships in a graph database. Cypher is easy to learn and understand, making it an ideal choice for beginners and experienced developers alike.

Understanding the Basics 📝

Before we dive into the Cypher commands, let's quickly review some basic concepts:

  • Nodes: Represent individual items in the graph. Examples include people, movies, or cars.
  • Relationships: Connect nodes in the graph. Relationships define how nodes are connected and provide context.
  • Properties: Attributes associated with nodes and relationships. Properties can store additional information about nodes and relationships.

The WHERE Clause 💡

The WHERE clause in Cypher filters the results based on a specific condition. It allows you to retrieve only the nodes and relationships that match the specified condition.

cypher
MATCH (n:Person) WHERE n.name = 'John Doe' RETURN n

In the above example, we're matching all nodes with the Person label and filtering the results to only include nodes where the name property is equal to 'John Doe'.

The RETURN Statement 💡

The RETURN statement is used to specify the output of a Cypher query. By default, Cypher returns all properties of the matched nodes and relationships. You can also explicitly specify which properties to return using the RETURN statement.

cypher
MATCH (n:Person) WHERE n.name = 'John Doe' RETURN n.name, n.age

In the above example, we're returning only the name and age properties of the matching Person nodes.

The ORDER BY Clause 💡

The ORDER BY clause in Cypher sorts the results based on a specified property. By default, the ORDER BY clause sorts the results in ascending order. You can also specify descending order by prefixing the property with DESC.

cypher
MATCH (n:Person) WHERE n.age > 30 RETURN n.name, n.age ORDER BY n.age DESC

In the above example, we're returning the names and ages of all Person nodes where the age is greater than 30, and sorting the results in descending order based on the age property.

Practical Example 💡

Let's create a simple graph and perform a query using WHERE, RETURN, and ORDER BY.

First, let's create a graph with some data:

cypher
CREATE (:Person {name: 'John Doe', age: 35}) CREATE (:Person {name: 'Jane Smith', age: 28}) CREATE (:Movie {title: 'Movie A', releaseYear: 2010}) CREATE (:Movie {title: 'Movie B', releaseYear: 2005}) CREATE (:Person {name: 'John Doe'})-[:WATCHED]->(:Movie {title: 'Movie A'}) CREATE (:Person {name: 'Jane Smith'})-[:WATCHED]->(:Movie {title: 'Movie B'})

Now, let's perform a query to find the movies watched by people older than 30 and sort them by release year:

cypher
MATCH (p:Person)-[:WATCHED]->(m:Movie) WHERE p.age > 30 RETURN m.title, m.releaseYear ORDER BY m.releaseYear ASC

This query will return the movies watched by people older than 30, sorted by release year in ascending order.

Quiz 📝

Quick Quiz
Question 1 of 1

Which Cypher command is used to filter results based on a specific condition?

We hope you found this lesson helpful! In our next lesson, we'll dive deeper into Cypher and explore more advanced features. Stay tuned! 🎯