SurrealDB Docs Logo

Enter a search query

SPLIT clause

The SPLIT clause in SurrealQL is used to split the results of a query based on a specific field, particularly when dealing with arrays. This is useful in scenarios where you want to treat each element of an array as a separate row in the result set. It can be particularly helpful in data analysis contexts where you need to work with individual elements of an array separately.

Syntax

Clause Syntax
SPLIT [ON] @field

Suppose you have a user table with a field emails that contains an array of email addresses for each user. You want to list each email address as a separate record.

Here’s how you can use the SPLIT clause in SurrealQL:

CREATE user SET name = "John Doe", emails = ["john@example.com", "doe@example.com"]; -- Split the results by each value in the emails array SELECT * FROM user SPLIT emails;

Explanation:

  • CREATE user SET ...: This creates a user record with a name and an array of email addresses.
  • SELECT * FROM user SPLIT emails: This query selects all fields from the user table and splits the results based on the emails field. Each email address in the emails array is returned as a separate row in the result set.

Output: The output of the query will be:

[ { "emails": "john@example.com", "id": "user:some_id", "name": "John Doe" }, { "emails": "doe@example.com", "id": "user:some_id", "name": "John Doe" } ]