---
title: "28: Recursive queries: setup | SurrealDB University"
description: "Follow along with interactive SurrealDB lessons featuring hands-on exercises, code examples, and step-by-step instructions."
url: https://surrealdb.com/learn/tour/page-28
---

![Course content preview](https://surrealdb.com/assets/static/town.D95IYf2V.avif)

[Back to Courses](https://surrealdb.com/learn)

Course chapters

[Welcome to the Tour of SurrealDB!](https://surrealdb.com/learn/tour) [1: Connecting to a database](https://surrealdb.com/learn/tour/page-01) [2: Creating records](https://surrealdb.com/learn/tour/page-02) [3: Using the ONLY keyword](https://surrealdb.com/learn/tour/page-03) [4: Non-ASCII record IDs](https://surrealdb.com/learn/tour/page-04) [5: Parentheses, indexing, and accessing fields](https://surrealdb.com/learn/tour/page-05) [6: Deleting records](https://surrealdb.com/learn/tour/page-06) [7: Field order in objects](https://surrealdb.com/learn/tour/page-07) [8: Tips when selecting records](https://surrealdb.com/learn/tour/page-08) [9: Casting and aliases](https://surrealdb.com/learn/tour/page-09) [10: Affixes](https://surrealdb.com/learn/tour/page-10) [11: Updating records](https://surrealdb.com/learn/tour/page-11) [12: Linking records](https://surrealdb.com/learn/tour/page-12) [13: Viewing the database as a whole](https://surrealdb.com/learn/tour/page-13) [14: Schemafull and schemaless](https://surrealdb.com/learn/tour/page-14) [15: Viewing the schema](https://surrealdb.com/learn/tour/page-15) [16: Literal types and assertions](https://surrealdb.com/learn/tour/page-16) [17: INSERT and using JSON](https://surrealdb.com/learn/tour/page-17) [18: FOR loops](https://surrealdb.com/learn/tour/page-18) [19: Basic graph relations](https://surrealdb.com/learn/tour/page-19) [20: Creating graph edges](https://surrealdb.com/learn/tour/page-20) [21: Querying graph relations](https://surrealdb.com/learn/tour/page-21) [22: Graph relations at greater depth](https://surrealdb.com/learn/tour/page-22) [23: Adding and improving data](https://surrealdb.com/learn/tour/page-23) [24: Defining events](https://surrealdb.com/learn/tour/page-24) [25: Defining indexes](https://surrealdb.com/learn/tour/page-25) [26: Improving the schema](https://surrealdb.com/learn/tour/page-26) [27: Visualising graph paths](https://surrealdb.com/learn/tour/page-27) [28: Recursive queries: setup](https://surrealdb.com/learn/tour/page-28) [29: Recursive queries](https://surrealdb.com/learn/tour/page-29) [30: More on recursive queries](https://surrealdb.com/learn/tour/page-30) [31: AI vector search](https://surrealdb.com/learn/tour/page-31) [Done the tour!](https://surrealdb.com/learn/tour/page-32)

# 28: Recursive queries: setup

With the town and library part of the database working well, we'll finish off the tour of SurrealDB by showing another one of its newest features: [recursive queries](https://surrealdb.com/docs/learn/data-models/graph/recursive-traversals) and [algorithms](https://surrealdb.com/docs/reference/query-language/language-primitives/idioms#path-and-unique-node-collection-shortest-path). Recursive queries let you move down multiple depths, while algorithms let you tell the database to follow all possible paths with a certain objective.

To make this happen, we'll first fill out the downtown section of Riverdale which you've seen throughout this tour in the image at the top. SurrealDB has three algorithms that help answer questions like this:

- What is the shortest path from Event Junction to Vector Park?
- What are all the paths within two steps from Event Junction? e.g. Event Junction to Index Avenue to Surreal Square, or Event Junction to Index Avenue to Statement Street...
- What are all the unique paths within two steps from Event Junction? e.g. Index Avenue, Surreal Square, Statement Street, but not Vector Park, which is much more than two steps away.

Try looking at the image on top as you think about what the answers might be.

To set the stage for this, we'll first need to add all of these streets and places. This could be done using `RELATE` statements, but simple record links work just as well so we will go with record links this time.

```
CREATE street:bowler_hat_alley    SET name = "Bowler Hat Alley",    connected_to = [street:index_avenue, street:rust_row];CREATE street:graph_query_lane    SET name = "Graph Query Lane",    connected_to = [street:push_to_main_street, street:recursive_crossing, street:statement_street];CREATE street:index_avenue        SET name = "Index Avenue",        connected_to = [place:event_junction, place:surreal_square, place:surrealdb_university, street:bowler_hat_alley,street:record_link_way, street:rust_row, street:statement_street];CREATE street:push_to_main_street SET name = "Push to Main Street", connected_to = [street:graph_query_lane, street:record_link_way,place:vector_park];CREATE street:transaction_trail   SET name = "Transaction Trail",   connected_to = [street:rust_row];CREATE street:record_link_way     SET name = "Record Link Way",     connected_to = [place:surreal_square, place:surrealdb_university, place:vector_park, street:index_avenue,street:push_to_main_street, street:statement_street];CREATE street:recursive_crossing  SET name = "Recursive Crossing",  connected_to = [street:recursive_crossing, street:schema_boulevard, street:statement_street];CREATE street:rust_row            SET name = "Rust Row",            connected_to = [place:idiom_tower, street:transaction_trail, street:bowler_hat_alley, street:index_avenue, street:schema_boulevard];CREATE street:schema_boulevard    SET name = "Schema Boulevard",    connected_to = [place:access_court, street:rust_row, street:recursive_crossing];CREATE street:statement_street    SET name = "Statement Street",    connected_to = [place:record_store, place:idiom_tower, street:index_avenue, street:record_link_way, street:recursive_crossing, street:graph_query_lane];CREATE place:access_court         SET address = "100 Schema Boulevard",   name = "Access Court",         place_type = "building", connected_to = [street:rust_row, street:schema_boulevard];CREATE place:event_junction       SET address = "50 Index Avenue",        name = "Event Junction",       place_type = "building", connected_to = [street:index_avenue, street:rust_row];CREATE place:idiom_tower          SET address = "100 Statement Street",   name = "Idiom Tower",          place_type = "tower",    connected_to = [street:bowler_hat_alley, street:rust_row, street:schema_boulevard];CREATE place:record_store         SET address = "120 Statement Street",   name = "Record Store",         place_type = "store",    connected_to = [street:record_link_way, street:statement_street]; CREATE place:surreal_square       SET address = "100 Index Avenue",       name = "Surreal Square",       place_type = "park",     connected_to = [place:surrealdb_university, street:index_avenue, street:record_link_way];CREATE place:surrealdb_university SET address = "150 Index Avenue",       name = "SurrealDB University", place_type = "school",   connected_to = [place:surreal_square, street:index_avenue, street:record_link_way]; CREATE street:transaction_trail    SET address = "60 Rust Row",            name = "Transaction Trail",    place_type = "park",     connected_to = [street:rust_row];CREATE place:vector_park          SET address = "10 Push to Main Street", name = "Vector Park",          place_type = "park",     connected_to = [street:push_to_main_street, street:record_link_way];UPDATE town:riverdale SET    streets = SELECT VALUE id FROM street,    buildings = [place:event_junction, place:access_court, place:record_store, place:idiom_tower],    schools = [place:surrealdb_university];
```

![Surrealist Icon](https://surrealdb.com/assets/static/fdfe2c20f5941d5e.D0guSOhZ.webp) Run Query

The street called Bowler Hat Alley, by the way, is the little one that provides a shortcut from Rust Row to Index Avenue and has a building with an actual bowler hat on the top.

Also note that one of the streets is an interesting one, a set of bridges and stairs that cross the river at the bottom of the image. This mysterious location is a bit of a maze. Sometimes it takes you to where you want to go, but other times you can't find your way out, and other times you end up back at the place where you entered. That's why locals call it Recursive Crossing.

That's why this one is connected not only to two streets, but also to itself, and the value `NONE`.

```surql
CREATE street:recursive_crossing SET
  connected_to = [street:recursive_crossing, street:schema_boulevard, street:statement_street, NONE];
```

Previous

27: Visualising graph paths

[Previous](https://surrealdb.com/learn/tour/page-27)

Next lesson

29: Recursive queries

[Next lesson](https://surrealdb.com/learn/tour/page-29)

```json
{"@context":"https://schema.org","@type":"Course","name":"Welcome to the Tour of SurrealDB!","description":"Welcome to the Tour of SurrealDB!","url":"https://surrealdb.com/learn/tour","inLanguage":"en","isAccessibleForFree":true,"provider":{"@type":"Organization","name":"SurrealDB","url":"https://surrealdb.com"},"hasPart":[{"@type":"LearningResource","name":"Welcome to the Tour of SurrealDB!","url":"https://surrealdb.com/learn/tour"},{"@type":"LearningResource","name":"1: Connecting to a database","url":"https://surrealdb.com/learn/tour/page-01"},{"@type":"LearningResource","name":"2: Creating records","url":"https://surrealdb.com/learn/tour/page-02"},{"@type":"LearningResource","name":"3: Using the ONLY keyword","url":"https://surrealdb.com/learn/tour/page-03"},{"@type":"LearningResource","name":"4: Non-ASCII record IDs","url":"https://surrealdb.com/learn/tour/page-04"},{"@type":"LearningResource","name":"5: Parentheses, indexing, and accessing fields","url":"https://surrealdb.com/learn/tour/page-05"},{"@type":"LearningResource","name":"6: Deleting records","url":"https://surrealdb.com/learn/tour/page-06"},{"@type":"LearningResource","name":"7: Field order in objects","url":"https://surrealdb.com/learn/tour/page-07"},{"@type":"LearningResource","name":"8: Tips when selecting records","url":"https://surrealdb.com/learn/tour/page-08"},{"@type":"LearningResource","name":"9: Casting and aliases","url":"https://surrealdb.com/learn/tour/page-09"},{"@type":"LearningResource","name":"10: Affixes","url":"https://surrealdb.com/learn/tour/page-10"},{"@type":"LearningResource","name":"11: Updating records","url":"https://surrealdb.com/learn/tour/page-11"},{"@type":"LearningResource","name":"12: Linking records","url":"https://surrealdb.com/learn/tour/page-12"},{"@type":"LearningResource","name":"13: Viewing the database as a whole","url":"https://surrealdb.com/learn/tour/page-13"},{"@type":"LearningResource","name":"14: Schemafull and schemaless","url":"https://surrealdb.com/learn/tour/page-14"},{"@type":"LearningResource","name":"15: Viewing the schema","url":"https://surrealdb.com/learn/tour/page-15"},{"@type":"LearningResource","name":"16: Literal types and assertions","url":"https://surrealdb.com/learn/tour/page-16"},{"@type":"LearningResource","name":"17: INSERT and using JSON","url":"https://surrealdb.com/learn/tour/page-17"},{"@type":"LearningResource","name":"18: FOR loops","url":"https://surrealdb.com/learn/tour/page-18"},{"@type":"LearningResource","name":"19: Basic graph relations","url":"https://surrealdb.com/learn/tour/page-19"},{"@type":"LearningResource","name":"20: Creating graph edges","url":"https://surrealdb.com/learn/tour/page-20"},{"@type":"LearningResource","name":"21: Querying graph relations","url":"https://surrealdb.com/learn/tour/page-21"},{"@type":"LearningResource","name":"22: Graph relations at greater depth","url":"https://surrealdb.com/learn/tour/page-22"},{"@type":"LearningResource","name":"23: Adding and improving data","url":"https://surrealdb.com/learn/tour/page-23"},{"@type":"LearningResource","name":"24: Defining events","url":"https://surrealdb.com/learn/tour/page-24"},{"@type":"LearningResource","name":"25: Defining indexes","url":"https://surrealdb.com/learn/tour/page-25"},{"@type":"LearningResource","name":"26: Improving the schema","url":"https://surrealdb.com/learn/tour/page-26"},{"@type":"LearningResource","name":"27: Visualising graph paths","url":"https://surrealdb.com/learn/tour/page-27"},{"@type":"LearningResource","name":"28: Recursive queries: setup","url":"https://surrealdb.com/learn/tour/page-28"},{"@type":"LearningResource","name":"29: Recursive queries","url":"https://surrealdb.com/learn/tour/page-29"},{"@type":"LearningResource","name":"30: More on recursive queries","url":"https://surrealdb.com/learn/tour/page-30"},{"@type":"LearningResource","name":"31: AI vector search","url":"https://surrealdb.com/learn/tour/page-31"},{"@type":"LearningResource","name":"Done the tour!","url":"https://surrealdb.com/learn/tour/page-32"}]}
```

```json
{"@context":"https://schema.org","@type":"LearningResource","name":"28: Recursive queries: setup","description":"Recursive queries: setup","url":"https://surrealdb.com/learn/tour/page-28","learningResourceType":"lesson","isPartOf":{"@type":"Course","name":"Welcome to the Tour of SurrealDB!","url":"https://surrealdb.com/learn/tour"},"position":29}
```

```json
{"@context":"https://schema.org","@type":"Organization","name":"SurrealDB","url":"https://surrealdb.com","logo":"https://surrealdb.com/assets/static/logo.BG7_TG2b.svg","description":"SurrealDB is the unified data layer for AI. A multi-model database for documents, graphs, vectors, and time-series.","foundingDate":"2022","hasCertification":[{"@type":"Certification","name":"SOC 2 Type 2"},{"@type":"Certification","name":"GDPR"},{"@type":"Certification","name":"Cyber Essentials Plus"},{"@type":"Certification","name":"ISO 27001"}],"owns":[{"@type":"SoftwareApplication","name":"SurrealDB","url":"https://surrealdb.com/surrealdb"},{"@type":"SoftwareApplication","name":"Agent Memory","url":"https://surrealdb.com/agent-memory"}],"knowsAbout":["multi-model databases","document databases","graph databases","vector search","time-series databases","SurrealQL","Agent Memory","real-time databases","embedded databases","context layer","graph ontology","distributed database","knowledge graphs","distributed transaction protocols","highly-scalable databases"],"sameAs":["https://www.wikidata.org/wiki/Q124316308","https://github.com/surrealdb/surrealdb","https://twitter.com/surrealdb","https://www.youtube.com/@surrealdb","https://www.linkedin.com/company/surrealdb","https://discord.gg/surrealdb","https://www.reddit.com/r/surrealdb","https://www.instagram.com/surrealdb","https://medium.com/surrealdb","https://dev.to/surrealdb"]}
```

```json
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://surrealdb.com"},{"@type":"ListItem","position":2,"name":"Learn","item":"https://surrealdb.com/learn"},{"@type":"ListItem","position":3,"name":"Page 28","item":"https://surrealdb.com/learn/tour/page-28"}]}
```
