# Part 10: Mastering MERGE, WITH, and RETURN Clauses in Cypher: A Comprehensive Guide

<figure><img src="https://159896965-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3LclXKUpANLoSt8DPdYV%2Fuploads%2Fuob1OXGCPyM8WwstVfde%2FCypherQuery.jpg?alt=media&#x26;token=7c84871b-5723-45df-8eca-2c4c43e981e5" alt=""><figcaption><p>Mastering MERGE, WITH, and RETURN Clauses in Cypher</p></figcaption></figure>

Welcome to our in-depth guide on using the MERGE, WITH, and RETURN clauses in Cypher! These clauses are essential for maintaining data integrity, chaining multiple queries, and controlling the output of your queries in a graph database like Neo4j. In this demo, we'll explore each clause with practical examples to make these concepts clear and actionable.

### MERGE Clause <a href="#viewer-q42i582315" id="viewer-q42i582315"></a>

The MERGE clause in Cypher combines the functionality of MATCH and CREATE. It ensures data integrity by avoiding duplicates. MERGE checks if a node or relationship exists; if it does, it returns the existing one. If not, it creates a new one.

#### Example: Using MERGE to Create or Update a Node <a href="#viewer-oz6u582319" id="viewer-oz6u582319"></a>

Let's start by merging a movie node with the title "The Matrix." If the node doesn't exist, it will be created with the specified properties. If it does exist, the existing node will be returned.

```
MERGE (m:Movie {title: 'The Matrix'})
ON CREATE SET m.released = 1999, m.duration = 136
RETURN m
```

#### Example: Using MERGE with Relationships <a href="#viewer-wc75k82342" id="viewer-wc75k82342"></a>

Next, we'll merge an actor node for Keanu Reeves and a movie node for "The Matrix." We'll create an ACTED\_IN relationship between them if it doesn't already exist.

```
MERGE (a:Person {name: 'Keanu Reeves'})
MERGE (m:Movie {title: 'The Matrix'})
ON CREATE SET m.released = 1999, m.duration = 136
MERGE (a)-[:ACTED_IN]->(m)
RETURN a, m
```

#### Example: Using MERGE with Patterns <a href="#viewer-maoon82375" id="viewer-maoon82375"></a>

We'll use MERGE with a pattern match to create a relationship between an actor and a movie. If the relationship already exists, nothing will be changed.

```
MATCH (a:Person {name: 'Keanu Reeves'})
MATCH (m:Movie {title: 'The Matrix'})
MERGE (a)-[:ACTED_IN]->(m)
RETURN a, m
```

#### Example: Using MERGE to Avoid Duplicates <a href="#viewer-kafx082398" id="viewer-kafx082398"></a>

MERGE ensures that a movie node for "The Matrix" is unique. If a node with this title already exists, no changes will be made. If it doesn't exist, a new node will be created.

```
MERGE (m:Movie {title: 'The Matrix'})
ON CREATE SET m.released = 1999, m.duration = 136
```

### WITH Clause <a href="#viewer-udj3782418" id="viewer-udj3782418"></a>

The WITH clause allows you to chain multiple queries together and perform operations like aggregation. It passes the results of one query as input to the next.

#### Example: Using WITH for Aggregation <a href="#viewer-ir7sh82422" id="viewer-ir7sh82422"></a>

Here, we're counting the number of movies each actor has acted in and returning the actor's name along with the count, ordered by the number of movies in descending order.

```
MATCH (a:Person)-[:ACTED_IN]->(m)
WITH a, count(m) as movies
RETURN a.name, movies
ORDER BY movies DESC
```

### RETURN Clause <a href="#viewer-32kbq82446" id="viewer-32kbq82446"></a>

The RETURN clause allows you to control what data is returned from a query. You can specify distinct results, limit the number of results, and order the results.

#### Example: Using RETURN with DISTINCT <a href="#viewer-0gcbj82450" id="viewer-0gcbj82450"></a>

We'll return the distinct names of actors who have acted in movies.

```
MATCH (a:Person)-[:ACTED_IN]->(m)
RETURN DISTINCT a.name
```

#### Example: Using RETURN with LIMIT <a href="#viewer-7llvi82465" id="viewer-7llvi82465"></a>

Here, we're returning the names of actors and the titles of movies they've acted in, limiting the results to five.

```
MATCH (a:Person)-[:ACTED_IN]->(m)
RETURN a.name, m.title
LIMIT 5
```

#### Example: Using RETURN with ORDER BY <a href="#viewer-4kqja82481" id="viewer-4kqja82481"></a>

In this query, we are returning the names of actors and the titles of movies they've acted in, ordered by the actor's name and then the movie's title.

```
MATCH (a:Person)-[:ACTED_IN]->(m)
RETURN a.name, m.title
ORDER BY a.name, m.title
```

### Conclusion <a href="#viewer-7m8vn82498" id="viewer-7m8vn82498"></a>

In this demo, we covered the MERGE clause, which ensures data integrity by avoiding duplicates when creating nodes and relationships. We also learned about the WITH clause, which allows us to chain multiple queries together and perform operations like aggregation. Finally, we looked at the RETURN clause, which allows us to control what data is returned from a query.

By mastering these clauses, you'll be well-equipped to handle complex queries and maintain the integrity of your graph data in Neo4j. Happy querying!
