# `result_scan`

**Applies to:** ✅ Data 360 SQL

Returns the result set of a completed query as a table. Use `result_scan` in the `FROM` clause to query cached results without rerunning the original query.

## Syntax

```sql
result_scan('<query_id>')
```

## Arguments

### Required

- `<query_id>`: The query identifier returned by the Data 360 Query API when the source query was submitted. Pass it as a string literal. You can't use a column reference or a computed expression.

## Returns

Returns a set of rows whose column names and data types match the result set of the referenced query. Use it anywhere a table reference is valid in a `FROM` clause.

## Considerations

`result_scan` is a set-returning function that must appear in the `FROM` clause, not in a `SELECT` list or `WHERE` clause.

### Execution and Behavior

- The referenced query must complete successfully and produce a result set before you can call `result_scan` against it. Queries that are still running, have failed, don't exist, or return no rows result in an error.
- When you submit a query using the [Data 360 Query API](/docs/data/data-cloud-query-guide/references/data-cloud-query-api-reference/c360a-api-queryservices-overview.md), store the returned `queryId` and pass it to subsequent `result_scan` calls. A single query can contain multiple calls referencing the same or different IDs.
- Rows returned by `result_scan` might not match the order of the original query. Use an explicit `ORDER BY` clause if sorting matters.

### Constraints and Retention

- Data 360 retains query results for up to 24 hours, though they may become unavailable sooner due to cache eviction. If results expire or are evicted, resubmit the original query to regenerate the result set.
- You can only scan results from queries that you submitted within your current data space. Referencing a query issued by another user or running in a different data space returns an error.

## Errors

| SQLSTATE | Condition                                               | Message                                                                                                                                                                                            |
| -------- | ------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 42704    | Query ID not found, not yours, not finished, or expired | `The result of the query '<queryId>' cannot be scanned. Either the query id does not exist, the query was issued by another user, the query execution has not finished yet or the result expired.` |
| 42704    | Query still running                                     | `The result of the query '<queryId>' cannot be scanned. The query execution has not finished yet.`                                                                                                 |
| 22023    | Source query produced no result set                     | `The query '<queryId>' did not produce a result.`                                                                                                                                                  |

## Examples

Because `result_scan` returns the schema of the source query, expected output depends on your data. The examples below show query structure only.

### Query a Cached Result Set

Submit an initial query and store the returned `queryId`. Use `result_scan` to aggregate the cached results without rerunning the source query. Pass the `queryId` value (for example, `'3442db1a-ecef-42b3-b0d8-971ee459efa4'`) returned by the Data 360 Query API.

```sql
SELECT region, sum(revenue) AS total_revenue
FROM result_scan('3442db1a-ecef-42b3-b0d8-971ee459efa4')
GROUP BY region
ORDER BY total_revenue DESC;
```

### Filter Results from a Large Cached Query

Filter results from a completed query without rescanning the source table.

```sql
SELECT customer_id, order_date, amount
FROM result_scan('3442db1a-ecef-42b3-b0d8-971ee459efa4')
WHERE amount > 500;
```

### Join Two Cached Result Sets

Reference two different cached result sets in a single query.

```sql
SELECT a.customer_id, a.segment, b.total_spend
FROM result_scan('3442db1a-ecef-42b3-b0d8-971ee459efa4') AS a
JOIN result_scan('7f9c21ba-ab12-4d3c-9e45-0b8c6d2f1a3e') AS b
  ON a.customer_id = b.customer_id
WHERE b.total_spend > 1000;
```

## Related Documentation

- [Query Data Using the Data 360 Query API](/docs/data/data-cloud-query-guide/references/data-cloud-query-api-reference/c360a-api-queryservices-overview.md)
- [Set Returning Functions](/docs/data/data-cloud-query-guide/references/dc-sql-reference/set-returning-functions.md)
- [`FROM` Clause](/docs/data/data-cloud-query-guide/references/dc-sql-reference/select-from.md)
