result
Back to DuckDB Data Engineering Glossary
The result
keyword in SQL is not commonly used across all database systems, but in DuckDB, it has a specific meaning within the context of recursive queries. When writing a recursive Common Table Expression (CTE), result
is used to reference the output of the previous iteration of the recursive part. This allows you to build upon the results of each recursive step.
Here's an example using DuckDB to generate a sequence of numbers:
Copy code
WITH RECURSIVE countdown(n) AS (
SELECT 5 AS n -- Base case
UNION ALL
SELECT n - 1 -- Recursive case
FROM result -- 'result' refers to the previous iteration
WHERE n > 0
)
SELECT * FROM countdown;
This query will produce:
Copy code
n
---
5
4
3
2
1
The result
keyword helps create powerful recursive queries for tasks like traversing hierarchical data structures or generating sequences. It's important to note that not all database systems use result
in this way, so this usage is specific to DuckDB's implementation of recursive CTEs.