Intermediate Cypher-1

Let’s talk about aliases in Neo4j Cypher.


Aliases are just variable names you assign to nodes, relationships, or returned values so you can refer to them later in the query.


🔹 1. Aliases for Nodes

MATCH (u:User) RETURN u.name, u.role

Here:

  • u is the alias (variable) for each User node.

  • You can access its properties as u.name, u.role.


🔹 2. Aliases for Relationships

MATCH (u:User)-[r:WORKS_ON]->(p:Project) RETURN u.name, p.title, r.since

Here:

  • u → alias for User

  • p → alias for Project

  • r → alias for WORKS_ON relationship


🔹 3. Aliases in RETURN

You can rename columns in the output (like SQL AS).

MATCH (u:User) RETURN u.name AS userName, u.role AS userRole

✅ The result will show columns userName and userRole instead of u.name and u.role.


🔹 4. Aliases in Aggregations

MATCH (u:User)-[:WORKS_ON]->(p:Project) RETURN p.title AS projectName, count(u) AS teamSize

✅ The output columns are projectName and teamSize.


🔹 5. Aliases with WITH (for chaining queries)

MATCH (u:User)-[:WORKS_ON]->(p:Project) WITH p.title AS projectName, count(u) AS teamSize WHERE teamSize > 2 RETURN projectName, teamSize
  • First part: create aliases projectName and teamSize

  • Then filter using those aliases.


Key Idea:

  • Inside patterns (u:User) → alias for node

  • Inside RETURN/WITH → alias for results

MERGE vs CREATE

Perfect 👌 this is one of the most important differences to understand in Neo4j. Let’s break it down step by step with examples.


🔹 CREATE

  • Always creates new nodes or relationships, even if an identical one already exists.

  • It never checks for duplicates.

Example

CREATE (u:User {name: "Alice"}) CREATE (u:User {name: "Alice"})

👉 You now have two different Alice nodes, even though they look the same.


🔹 MERGE

  • Think of it as "match if exists, otherwise create".

  • It searches for a node/relationship that matches the entire pattern.

  • If it finds one → reuses it.

  • If it doesn’t → creates it.

Example

MERGE (u:User {name: "Alice"}) MERGE (u:User {name: "Alice"})

👉 Only one Alice node exists, because the second MERGE matches the first one.


🔹 Important Detail

MERGE matches the exact properties you give.
So this can create duplicates if you’re not careful:

MERGE (u:User {name: "Alice"}) MERGE (u:User {name: "Alice", role: "Developer"})

👉 Two Alice nodes get created:

  • First has {name: "Alice"}

  • Second has {name: "Alice", role: "Developer"}

✅ To avoid this, you can match on fewer properties and then set others:

MERGE (u:User {name: "Alice"}) ON CREATE SET u.role = "Developer" ON MATCH SET u.lastSeen = date()

Let’s go over how to update data in Neo4j using SET and REMOVE.


🔹 1. SET – Add or Update Properties

  • SET is used to add new properties or update existing ones on nodes/relationships.

  • If the property exists → it’s updated.

  • If it doesn’t exist → it’s created.

Example – Update a User’s role

MATCH (u:User {name: "Alice"}) SET u.role = "Developer"

👉 If Alice has no role, it will be created. If she already has one, it will be replaced.

Example – Add multiple properties

MATCH (u:User {name: "Alice"}) SET u.location = "India", u.active = true

Example – Copy all properties from a map

MATCH (u:User {name: "Alice"}) SET u += {department: "Engineering", level: 2}

👉 Adds department and level without removing existing ones.


🔹 2. REMOVE – Delete Properties or Labels

  • REMOVE is used to delete a property from a node/relationship.

  • It can also remove labels from nodes.

Example – Remove a property

MATCH (u:User {name: "Alice"}) REMOVE u.location

👉 Deletes the location property.

Example – Remove a label

MATCH (u:User {name: "Alice"}) REMOVE u:User

👉 Alice is no longer a User node (but still exists as a node in the graph).


🔹 3. SET vs REMOVE – Quick Comparison

OperationCommandEffect
Add/update propertySET u.role = "Admin"Adds or updates role
Add multiple propsSET u += {a:1, b:2}Adds both a and b
Remove propertyREMOVE u.roleDeletes role property
Remove labelREMOVE u:UserRemoves User label

⚡ Tip:

  • Use SET when you want to add or update information.

  • Use REMOVE when you want to delete properties or labels but keep the node/relationship itself.


In Cypher, when you use

SET u += {department: "Engineering", level: 2}

it works like a merge (upsert) of properties, not like replacing the whole property map.


🔹 What actually happens:

  • If a property does not exist → it gets added.

  • If a property already exists → its value gets overwritten (updated).

  • Other properties stay unchanged.


Example:

Suppose Alice has this node:

{ "name": "Alice", "department": "Finance", "location": "India" }

After running:

MATCH (u:User {name: "Alice"}) SET u += {department: "Engineering", level: 2}

👉 Result:

{ "name": "Alice", "department": "Engineering", // ✅ updated "location": "India", // ✅ unchanged "level": 2 // ✅ new property added }

🔹 Key difference vs direct SET

If you did:

SET u = {department: "Engineering", level: 2}

👉 Then Alice’s node would keep only those two properties, and name + location would be lost.

So += is like safe update/merge, while = is replace everything.

deleting in Neo4j has two flavors, and the difference is very important.


🔹 DELETE

  • Removes nodes or relationships, but only if the node has no relationships.

  • If a node is still connected, Neo4j will throw an error.

Example

// Suppose Alice works on ProjectX MATCH (u:User {name: "Alice"}) DELETE u

👉 ❌ Error: Cannot delete node <id> because it still has relationships.
Because Alice is linked to ProjectX.


🔹 DETACH DELETE

  • Deletes a node and all its relationships in one step.

  • Think of it as force delete.

Example

MATCH (u:User {name: "Alice"}) DETACH DELETE u

👉 ✅ Alice is deleted, and her relationships to projects, apps, etc. are also removed automatically.


🔹 Deleting Relationships

You can delete relationships separately too:

MATCH (u:User {name: "Alice"})-[r:WORKS_ON]->(p:Project) DELETE r

👉 Only the relationship is deleted, Alice and ProjectX remain.


⚡ Quick Summary

CommandWhat it does
DELETEDeletes a node/relationship if no other relationships exist
DETACH DELETEDeletes a node and all its connected relationships
DELETE rDeletes a relationship only

Comments