This manual enumerates common syntax elements of GQL along with reference prompts, enabling users to formulate GQL statements by referring to the provided example queries.
Syntax | Query Example | Result |
---|---|---|
Find Vertex | Locate vertices of type person | match(a:person) return a |
Find Edge | Return all edges labeled as knows | match(a)-[e:knows]->(b) return e |
Find Relationships | Query 10 universities located in Beijing Identify 5 students related to Teacher Xiao Zhang | match(a:city where a.name = ‘北京’)<-[:belong]-(b:university) return b limit 10 match(a:teacher where a.name=‘Xiao Zhang’)-[e]-(b:student) return b limit 5 |
Find Multi-Degree Relationships | Find people known by friends of Student Xiao Wang Retrieve departments connected to universities, then students linked to those departments, and courses chosen by those students Identify software co-created by Tencent and Google, return 5 results | match(a:student where a.name = ‘Xiao Wang’)-[e:friend]->(b)-[e2:knows]->(c:person) return c match(a:university)-[e:has]->(b:department)-[e2:has]->(c:student)-[e3:selects]->(d:course) return d match(a:company where a.name=‘Tencent’)-[e:creates]->(b:software)<-[e2:creates]-(c:company where c.name=‘Google’) return b.name limit 5 |
Loop | From person Zhang Siqi, traverse through pay edges, reach vertices within 2 to 4 degrees | match(a:person where a.name=‘Zhang Siqi’)-[e:pay]->{2,4}(b:person) return b |
Loop | Identify 3-hop cycles involving persons who know Li Hong | match(a:person where name = ‘Li Hong’)-[e:knows]->{1,2}(b)->(a) return a.id, b.id as b_id |
Filter Criteria | Find people known by Xiaohong, aged over 20, earning more than 5000 Fetch 10 nodes not female, shorter than 160cm, or with an id greater than 5 | match(a:person where a.name=‘Xiaohong’)-[e:knows]->(b:person where b.age > 20 and b.salary > 5000) return b match(a where (a.gender <> ‘female’ and a.height < 160) or a.id > 5) return a limit 10 |
Let Single Value | Query software created by Ant Group, set minPrice of software equal to its minimum price, return company id and software's minPrice | match(a:company where a.name = ‘Ant Group’)-[e:creates]->(b:software) let b.minPrice = MIN(b.price) return a.id, b.minPrice |
Let Subquery | Find employees of Ant Group, set their countSalary equal to the sum of salaries of those who know them, then find the software they purchase Identify the country that city id 10 belongs to, assign the average count of companies related to the country as avgCnt | match(a:company where a.name = ‘Ant Group’)-[e:employee]->(b:person) let b.countSalary = SUM((b:person)<-[e2:knows]-(c:person) => c.salary) match(b:person)-[e3:buy]->(d:software) return b.countSalary, d match(a:city where id = ‘10’)-[e:belong]->(b:country)<-[e2:belong]-(c:company) let b.avgCnt = AVG(c.peopleNumber) return b |
Function Call | Invoke SSSP function with ‘arg1’, 10 as inputs, return id and distance | match(a:person) call sssp(a, 10) yield (id, distance) return id, distance |
order | Return software created by companies, sorted by company scale descending and software price ascending | match(a:company)-[e:creates]->(b:software) return a.scale,b.price order by a.scale desc, b.price asc |
group by | Find people known by Xiaohong, grouped by gender, return max salary For Peking University affiliates, return the average count of people per company, grouped by company scale | match(a:person where person.name = ‘Xiaohong’)-[e:knows]->(b:person) return MAX(b.salary) group by b.gender match(a:university where a.name=‘北京大学’)-[e]-(b:company) return AVG(b.peopleNumber) group by b.scale |
join | Find all people liked by Zheng Wei and all who know him, return together Find schools related to person Alice, denote as X, further find companies and persons associated with X | match(a:person where a.name = ‘Zheng Wei’)-[e:likes]->(b:person),(a:person where a.name = ‘Zheng Wei’)<-[e2:knows]-(c:person) return a, b, c match(a:person where a.name = ‘alice’)-[e]-(b:school), (b:school)-[e2]-(c:company),(b:school)-[e3]-(d:person) return a, b, c, d |
Schema Query with Graph (Automatically appended in Console) | Using this graph schema:CREATE GRAPH g ( Vertex film ( id int ID, name varchar, category varchar, value int ), Vertex cinema ( id int ID, name varchar, address varchar, size int ), Vertex person ( id int ID, name varchar, age int, gender varchar, height int, salary int ), Vertex comment ( id int ID, name varchar, createTime bigint, wordCount int ), Vertex tag ( id int ID, name varchar, value int ), Edge person_likes_comment ( srcId int FROM person SOURCE ID, targetId int FROM comment DESTINATION ID, weight double, f0 int, f1 boolean ), Edge cinema_releases_film ( srcId int FROM cinema SOURCE ID, targetId int FROM film DESTINATION ID, weight double, f0 int, f1 boolean ), Edge person_watch_film ( srcId int FROM person SOURCE ID, targetId int FROM film DESTINATION ID, weight double, f0 int, f1 boolean, timeStamp bigint ), Edge film_has_tag ( srcId int FROM film SOURCE ID, targetId int FROM tag DESTINATION ID, weight double, f0 int, f1 boolean ), Edge person_creates_comment ( srcId int FROM person SOURCE ID, targetId int FROM comment DESTINATION ID, weight double, f0 int, f1 boolean, timeStamp bigint ), Edge comment_belong_film ( srcId int FROM comment SOURCE ID, targetId int FROM film DESTINATION ID, weight double, f0 int, f1 boolean ));Find comments created by Sun Mei and liked by Sun Jiancong, return all | match(a:person where a.name = ‘Sun Mei’)-[e:person_creates_comment]->(b:comment),(c:person where c.name = ‘Sun Jiancong’)-[e2:person_likes_comment]->(d:comment)return a, b, c, d |
Multi-Query with Graph Schema | Using this graph schema:CREATE GRAPH g ( Vertex book ( id int ID, name varchar, id int ID, name varchar, category varchar, price int, wordCount int, createTime bigint ), Vertex publisher ( id int ID, name varchar, age int, gender varchar, height int, salary int ), Vertex reader ( id int ID, name varchar, age int, gender varchar, height int, salary int ), Vertex author ( id int ID, name varchar, age int, gender varchar, height int, salary int ), Edge author_write_book ( srcId int FROM author SOURCE ID, targetId int FROM book DESTINATION ID, weight double, f0 int, f1 boolean, timeStamp bigint ), Edge publisher_publish_book ( srcId int FROM publisher SOURCE ID, targetId int FROM book DESTINATION ID, weight double, f0 int, f1 boolean, timeStamp bigint ), Edge book_refers_book ( srcId int FROM book SOURCE ID, targetId int FROM book DESTINATION ID, weight double, f0 int, f1 boolean ), Edge reader_likes_book ( srcId int FROM reader SOURCE ID, targetId int FROM book DESTINATION ID, weight double, f0 int, f1 boolean ), Edge author_knows_author ( srcId int FROM author SOURCE ID, targetId int FROM author DESTINATION ID, weight double, f0 int, f1 boolean ));Execute 4 queries: 1. Writers known by Huang Jiacong; 2. Edges labeled author_knows_author; 3. IDs of books related to “Computer Networks”; 4. 152 books related to both He Xue and Zhang Jiancong | Queries:1: match(a:author)<-[e:author_knows_author]-(b:author where b.name=‘Huang Jiacong’) return a, b;2: match(a:author)-[e:author_knows_author]->(b:author) return e;3: match(a:book where a.name=‘Computer Networks’)-[e]-(b:book) return b.id;4: match(a where a.name=‘He Xue’)-[e]->(b:book)<-[e2]-(c where c.name=‘Zhang Jiancong’) return b limit 152; |