GQL Essentials / GQL Tutorial for Beginners: Your First Graph Query
PDF ↗

GQL learning guide · Dominik Tomaszuk

GQL Tutorial for Beginners: Your First Graph Query

This tutorial reads a small library graph. It assumes libraryGraph has been created and populated as in Chapter 4. The examples describe query behavior; running them requires a database that supports the GQL features used here.

1. Identify nodes and edges

A Loan node has a loanid. A Book node has a title. An INCLUDES edge connects a loan to a borrowed book and carries due_date and late_fee properties. One loan may include several books.

2. Match a connection

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

USE selects the graph. MATCH finds directed connections with the stated labels. RETURN chooses the output columns, and AS names them. A loan with two included books produces two matches, so its identifier can appear in two result rows.

3. Filter by an edge property

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 filter keeps only connections with a positive late fee. It reads i.late_fee because the fee belongs to the borrowing edge. A condition on b.late_fee would look for a property on a different element.

4. Check the result against the data

In the book's initial instance, loan L-2025-0001 includes two books. The edge to Graph Theory has a fee of 2.5, while the edge to Graph DB 101 has a fee of 0.0. The filtered query therefore returns the former connection. Later updates to the sample graph can change this result.

5. Continue learning

Try changing the fee threshold and explain which rows survive. Then use the query examples to practice ordering and aggregation. For the complete graph type, sample insertion and update statements, return to GQL by Example. The matching chapter explains how patterns produce binding tables.

Learn GQL

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

Search chapters, definitions and examples.