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
3. Examples
a) Match a node by label
✅ Returns all nodes with label User
.
b) Match a node by property
✅ Finds all User
nodes where name = "Alice"
.
c) Match nodes with multiple properties
✅ Finds Alice who is also a Developer.
d) Match with relationships
✅ Finds all projects Alice works on.
e) Match in both directions
✅ Finds all users and projects connected by WORKS_ON
(regardless of direction).
f) Match with filtering
✅ Finds all Developers and returns their names.
g) Match with multiple hops (traversal)
✅ 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
✅ Returns all User
nodes (with all their properties).
🔹 2. Returning Specific Properties
✅ Only returns the name
and role
of users.
(Like choosing specific columns in SQL).
🔹 3. Aliasing (Renaming Columns)
✅ Shows username
and job
instead of property names.
(Helpful when building reports or APIs).
🔹 4. Returning Relationships
✅ Returns Alice’s name, when the relationship started (since
), and project title.
🔹 5. Returning Calculations
✅ Returns the number of users.
🔹 6. Distinct Values
✅ Returns all unique roles among users.
🔹 7. Ordering Results
✅ Returns users ordered by name (descending).
🔹 8. Limiting Results
✅ Returns only the first 5 users.
🔹 9. Collecting Data into Lists
✅ 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
✅ Returns only users whose role is "Developer"
.
🔹 2. Multiple Conditions
✅ Developers with more than 3 years of experience.
✅ Developers or Admins.
🔹 3. String Matching
✅ Names starting with A
.
✅ Names containing "li"
(like Alice).
✅ Names ending with n
.
🔹 4. IN Operator (like SQL IN
)
✅ Returns users who are Admin or Manager.
🔹 5. Filtering Relationships
✅ Only returns relationships where the user started working on the project in or after 2022.
🔹 6. Filtering by Node Labels
✅ Returns only nodes with the User
label.
🔹 7. Using NOT
✅ All users except Admins.
🔹 8. Combining with WITH
Sometimes you filter after aggregation:
✅ Returns only projects that have more than 3 team members.
⚡ Summary:
-
MATCH
= finds patterns -
WHERE
= applies conditions -
RETURN
= shows results
Comments
Post a Comment