GQL Essentials / GQL Query Examples: Filters, Ordering and Aggregation
PDF ↗

GQL learning guide · Dominik Tomaszuk

GQL Query Examples: Filters, Ordering and Aggregation

These examples use the populated libraryGraph from GQL by Example. Each query answers a different question about loans and books. Begin with the tutorial if the pattern notation is new to you.

Which borrowed books have a late fee?

GQL
USE libraryGraph
MATCH (l:Loan)-[i:INCLUDES]->(b:Book)
WHERE i.late_fee > 0.0
RETURN l.loanid AS loan, b.title AS title,
       i.late_fee AS fee

The condition applies to an edge property. A book can appear in several loans, each with its own fee. Returning the loan identifier alongside the title keeps those borrowings distinguishable.

Which due dates come first?

GQL
USE libraryGraph
MATCH (l:Loan)-[i:INCLUDES]->(b:Book)
RETURN l.loanid AS loan, b.isbn AS isbn,
       b.title AS title, i.due_date AS due_date
ORDER BY due_date, loan, isbn

The first sort key is the due date. The loan and ISBN columns break ties between different loan-book pairs in the sample. Without an explicit ordering, a result table should not be treated as a sorted sequence. Read Ordering and Pagination before adding a row limit.

What is the total late fee for each author?

GQL
USE libraryGraph
MATCH (l:Loan)-[i:INCLUDES]->(b:Book)
LET authorName = b.author
RETURN authorName AS author, SUM(i.late_fee) AS total_late_fee
GROUP BY author
ORDER BY total_late_fee DESC
LIMIT 10

This example from Chapter 4 groups matched borrowings by author and sums their edge fees. The amount measures fees across loan-book connections, rather than the number of distinct books. If a more complex pattern produces additional matches for the same connection, check that multiplicity before summing.

How to adapt an example

Check the graph's labels, edge direction and property names first. Then verify the database's supported syntax, including grouping and graph selection. The book explains aggregation semantics and provides update examples separately, so you can understand reads before changing the dataset.

Learn GQL

Start with a short guide, then follow links to the relevant chapters.

Search chapters, definitions and examples.