Skip to content
New

Introducing Scale: SurrealDB Cloud for high availability and scale

Learn more

1/4

Course content preview

21: Querying graph relations

Querying a relation is usually done using the same arrow syntax used in the RELATE statement to create it.

Here is a query on a single person record which returns its name as well as the path ->works_at->place. The graph query checks to see if the person is linked to the works_at table (the in field, because the arrow at ->works_at is going in to works_at) and if there is a place at the other end (the out field, because the arrow at works_at->place goes out from works_at and looks for a place record at that point).

SELECT 
name,
->works_at->place
FROM person LIMIT 1;
Response
[
  {
    "->works_at": {
      "->place": [
        place:surreal_library
      ]
    },
    name: 'Lydia Wyndham'
  }
]

Since the place point in the query will look for place records, the .{} destructuring operator can be used to access the fields, such as name and address. We will also give it the alias workplace for readability, and remove the LIMIT to show all the person records.

SELECT 
name,
->works_at->place.{
name,
address
} AS workplace
FROM person;
Response
[
  {
    name: 'Sara Bellum',
    workplace: [
      {
        address: '2025 Statement Street, Riverdale',
        name: 'Surreal Library'
      }
    ]
  },
  {
    name: 'Lydia Wyndham',
    workplace: [
      {
        address: '2025 Statement Street, Riverdale',
        name: 'Surreal Library'
      }
    ]
  },
  {
    name: 'Samm Schwartz',
    workplace: [
      {
        address: '2025 Statement Street, Riverdale',
        name: 'Surreal Library'
      }
    ]
  }
]

You can also use a graph query directly on a record instead of using a SELECT statement.

-- Get Samm's record ID
LET $samm = SELECT id FROM ONLY person WHERE name = 'Samm Schwartz' LIMIT 1;

-- Then just query from the record ID, no SELECT
$samm->works_at->place.{ address, floors, name };
Response
[
  {
    address: '2025 Statement Street, Riverdale',
    floors: 8,
    name: 'Surreal Library'
  }
]

Turning the `->` arrow around to `works_at->person` path because it's not the `place` that works at the `person`, it's the `person` that works at the `place`. Turning the arrows around will accomplish this.

The relation is still visually easy to follow, because place<-works_at<-person still shows that person leads into works_at and then into place. The only difference is that place is being used as the starting point now.

place:surreal_library<-works_at<-person.name;

Output: the names of the people that work at the library.

Response
[
  'Samm Schwartz',
  'Sara Bellum',
  'Lydia Wyndham'
]

Previous

20: Creating graph edges

Next lesson

22: Graph relations at greater depth

SurrealDB

The context layer for AI agents.

Documents, graphs, vectors, time-series, and memory.
One transaction, one query, one deployment.

Explore with AI

Stay in the loop

Tutorials, AI agent recipes, and product updates, every two weeks.

Independently verified

SOC 2 Type 2

GDPR

Cyber Essentials Plus

ISO 27001

Trust Centre

Copyright © 2026 SurrealDB Ltd. Registered in England and Wales. Company no. 13615201

Registered address: 3rd Floor 1 Ashley Road, Altrincham, Cheshire, WA14 2DT, United Kingdom

Trading address: Huckletree Oxford Circus, 213 Oxford Street, London, W1D 2LG, United Kingdom