Cypher Query Language - Basics

 Perfect 👍 let’s go deep into MATCH — it’s one of the most important Cypher commands in Neo4j because it’s how you query existing data.


1. What is MATCH?

  • MATCH is like SELECT in SQL.

  • It looks for nodes and relationships in your graph based on the pattern you describe.

  • You can then return, filter, or use those matches.


2. Basic Syntax

MATCH (alias:Label {propertyKey: value}) RETURN alias

3. Examples

a) Match a node by label

MATCH (u:User) RETURN u

✅ Returns all nodes with label User.


b) Match a node by property

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

✅ Finds all User nodes where name = "Alice".


c) Match nodes with multiple properties

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

✅ Finds Alice who is also a Developer.


d) Match with relationships

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

✅ Finds all projects Alice works on.


e) Match in both directions

MATCH (u:User)-[:WORKS_ON]-(p:Project) RETURN u, p

✅ Finds all users and projects connected by WORKS_ON (regardless of direction).


f) Match with filtering

MATCH (u:User) WHERE u.role = "Developer" RETURN u.name

✅ Finds all Developers and returns their names.


g) Match with multiple hops (traversal)

MATCH (u:User {name: "Alice"})-[:WORKS_ON]->(p:Project)<-[:WORKS_ON]-(other:User) RETURN other.name

✅ Finds other users working on the same project as Alice.


4. Key Things to Remember

  • () → nodes.

  • [] → relationships.

  • -[]-> → directed relationship.

  • -[]- → undirected relationship.

  • Use WHERE for filtering conditions.

  • RETURN decides what data to show.


👉 You can think of MATCH as drawing a pattern of your graph, and Neo4j will find all parts of the graph that match that pattern.

Great — let’s cover RETURN in Cypher.
If MATCH is like SQL’s SELECT … FROM, then RETURN is like SELECT in SQL — it decides what part of the matched data you want to see.


🔹 1. Basic Usage

MATCH (u:User) RETURN u

✅ Returns all User nodes (with all their properties).


🔹 2. Returning Specific Properties

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

✅ Only returns the name and role of users.
(Like choosing specific columns in SQL).


🔹 3. Aliasing (Renaming Columns)

MATCH (u:User) RETURN u.name AS username, u.role AS job

✅ Shows username and job instead of property names.
(Helpful when building reports or APIs).


🔹 4. Returning Relationships

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

✅ Returns Alice’s name, when the relationship started (since), and project title.


🔹 5. Returning Calculations

MATCH (u:User) RETURN count(u) AS totalUsers

✅ Returns the number of users.


🔹 6. Distinct Values

MATCH (u:User) RETURN DISTINCT u.role

✅ Returns all unique roles among users.


🔹 7. Ordering Results

MATCH (u:User) RETURN u.name, u.role ORDER BY u.name DESC

✅ Returns users ordered by name (descending).


🔹 8. Limiting Results

MATCH (u:User) RETURN u.name LIMIT 5

✅ Returns only the first 5 users.


🔹 9. Collecting Data into Lists

MATCH (p:Project)<-[:WORKS_ON]-(u:User) RETURN p.title, collect(u.name) AS teamMembers

✅ For each project, returns a list of team members.


⚡ Key Idea:

  • MATCH = finds the data (pattern).

  • RETURN = decides what part of that matched data to show (projection).

Perfect 👍 now let’s go into WHERE in Cypher.

Think of WHERE like the filter condition in SQL (WHERE …).
It does not find data by itself — it just restricts the results of a MATCH, OPTIONAL MATCH, or even WITH.


🔹 1. Basic Filtering

MATCH (u:User) WHERE u.role = "Developer" RETURN u.name, u.role

✅ Returns only users whose role is "Developer".


🔹 2. Multiple Conditions

MATCH (u:User) WHERE u.role = "Developer" AND u.experience > 3 RETURN u.name, u.role, u.experience

✅ Developers with more than 3 years of experience.

MATCH (u:User) WHERE u.role = "Developer" OR u.role = "Admin" RETURN u.name, u.role

✅ Developers or Admins.


🔹 3. String Matching

MATCH (u:User) WHERE u.name STARTS WITH "A" RETURN u.name

✅ Names starting with A.

MATCH (u:User) WHERE u.name CONTAINS "li" RETURN u.name

✅ Names containing "li" (like Alice).

MATCH (u:User) WHERE u.name ENDS WITH "n" RETURN u.name

✅ Names ending with n.


🔹 4. IN Operator (like SQL IN)

MATCH (u:User) WHERE u.role IN ["Admin", "Manager"] RETURN u.name, u.role

✅ Returns users who are Admin or Manager.


🔹 5. Filtering Relationships

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

✅ Only returns relationships where the user started working on the project in or after 2022.


🔹 6. Filtering by Node Labels

MATCH (n) WHERE n:User RETURN n

✅ Returns only nodes with the User label.


🔹 7. Using NOT

MATCH (u:User) WHERE NOT u.role = "Admin" RETURN u.name, u.role

✅ All users except Admins.


🔹 8. Combining with WITH

Sometimes you filter after aggregation:

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

✅ Returns only projects that have more than 3 team members.


Summary:

  • MATCH = finds patterns

  • WHERE = applies conditions

  • RETURN = shows results

Comments