CS343 Querying Graph
CS343 Querying Graph
Spring 2024
Slides are intended to be filled during the lectures. Certain details are intentionally omitted for in-class discussions. These slides are not meant to be used as reading material.
Courses: Graph Model
Graph Query Languages: Gremlin vs Cypher
• Gremlin:
– Part of the Apache TinkerPop graph computing framework
– Compatible with various graph databases.
– A graph traversal language with an imperative style.
– User specifies the sequence of steps to traverse the graph and retrieve information.
Graph Query Languages: Gremlin vs Cypher
• Cypher:
– Associated with Neo4j
– Declarative Language
– User define patterns to find in the graph
– focuses on expressing relationships and patterns between nodes
Patterns as ASCII Art
(emil)<-[:KNOWS]-(jim)-[:KNOWS]->(ian)-[:KNOWS]->(emil)
Patterns as ASCII Art
• (ahmed) –[:KNOWS]->(basit)<-[:KNOWS]-(bilal)
• (ahmed) –[:WORKS]->(PNS)<-[:LOCATED]-(KARACHI)
• (:Person{name:”Karim”}) -[:DRIVES]->(:Car{number:”ABC123”})
Cypher Clauses:
• MATCH: Specifies the patterns to match in the graph. It is used to find nodes, relationships, and paths
that meet certain criteria.
• OPTIONAL MATCH: Similar to MATCH, but it allows for patterns that may not exist, and it does not
affect the overall query result if the pattern is not matched.
• RETURN: Specifies what data to include in the result set. It is used to define the structure of the
output, including nodes, relationships, properties, etc.
• WHERE: Filters the results based on specified conditions. It is used to include only the data that
satisfies the specified criteria.
• WITH: Breaks the query into multiple parts. It is used to pass results from one part of the query to
another, allowing for more complex queries.
• ORDER BY: Sorts the result set based on specified criteria. It is used to control the order of the output.
• LIMIT: Limits the number of results returned by the query. It is used to reduce the size of the result
set.
• SKIP: Skips a specified number of results in the result set. It is often used in conjunction with LIMIT for
pagination.
Movie Database
RETURN
WITH 10 as x
RETURN x
WITH 10 as x, 20 as y
RETURN x,y
WITH 10 as x, 20 as y
RETURN *
MATCH
Specifies the patterns to match in the graph. It is used to find nodes, relationships, and
paths that meet certain criteria.
MATCH (n)
RETURN n
MATCH (p:Person)
RETURN p