FETCH clause

The FETCH clause is used to retrieve related records or data from other tables in a single query. This is particularly useful when you want to gather data that is linked through relationships (record links or graph edges) without having to perform multiple separate queries.

The FETCH clause predates and is functionally identical to the ALL idiom which is used by appending a .* to a related record.

Suppose you have a person table and a post table, where each post is related to a person. You can use the FETCH clause to retrieve a person along with their posts in a single query:

-- Using FETCH syntax
SELECT * FROM person FETCH posts;

-- Using .*
SELECT *, posts.* FROM person;

In this example, posts would be a related field in the person table that links to the post table. The FETCH clause allows you to retrieve all posts associated with each person in the result set.

Overall, the FETCH clause in SurrealQL is a powerful tool for optimizing data retrieval and simplifying query logic when working with related data.

The following example shows querying using FETCH or .* compared to selecting individual fields of a related record.

Was this page helpful?