Skip to content
Course content preview

18: FOR loops

Another way to work with the items inside an array is to use a FOR loop. After FOR, choose a parameter name (like $item or $person) to be used every time the loop occurs, follow it with IN and the data, and then {} to open up a space in which you can write custom logic for each item in the loop.

Inside the loop in the next query, the parameter $person can be used to set the fields of each person record. Since each step of a FOR loop is isolated inside its own space, the output of a FOR loop will always be empty. To see the results of the person records created in this way, we will need to use a SELECT statement after the loop is done.

FOR $person IN [
{
age: 30,
name: 'Sara Bellum'
},
{
age: 25,
name: 'Lydia Wyndham'
},
{
age: 45,
name: 'Samm Schwartz'
}
] {
CREATE person SET
age = $person.age,
name = $person.name
};

SELECT * FROM person;


Output
[
  {
    age: 45,
    id: person:cj8wc617o1ea2vs1yv4n,
    name: 'Samm Schwartz'
  },
  {
    age: 30,
    id: person:e8z6pjqhuwzfalbku61n,
    name: 'Sara Bellum'
  },
  {
    age: 25,
    id: person:faovorn8ntkqwfw01638,
    name: 'Lydia Wyndham'
  }
]


This is the way that we will create these employees for the library, but we also want to link each one to the library at each step of the FOR loop after creating them (so run a quick DELETE person at this point if you are following along separately). This time the link will be something called a graph edge, which we will first need to learn to use starting on the next page.

THE PLATFORM

Everything an application and its agents know. Five surfaces, one engine.

IN PRODUCTION

Trusted at scale. Samsung, Nvidia, Verizon, Tencent, and Walmart run on SurrealDB.

14,000+

Developers building on SurrealDB Cloud

4M+

Developers building on SurrealDB worldwide

FROM THE TEAMS

SurrealDB gives us a foundation where we can unify semantic search, knowledge graphs, and AI-driven decision making without stitching together multiple systems. Collapsing responsibility into SurrealDB has become our default engineering posture.
Justin Foley

VP of Engineering, Later

SurrealDB

The context and memory layer for AI agents

Database. Graphs, vectors, documents and relational data in one engine, in a single ACID transaction.
Agent Memory. Connects and retrieves context wherever your data lives, every fact carrying its source.
Cloud. Fully managed, in the cloud provider and region you choose.

Explore with AI

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